metrics

package
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package metrics provides Prometheus-style metrics for the Kubernetes client.

There are two steps to set up exporting:

  1. Register metrics with a storage via RegisterKubernetesClientMetrics.
  2. Register the produced backends with client-go via the k8s.io/client-go/tools/metrics package.

Backends implement the interfaces from https://github.com/kubernetes/client-go/blob/master/tools/metrics/metrics.go.

The naming pattern is modelled after https://github.com/flant/shell-operator/blob/main/pkg/metrics/metrics.go: metric name constants contain a {PREFIX} placeholder. Call ReplacePrefix to obtain the concrete name for a given prefix. Unlike the shell-operator approach, prefix resolution is per-instance rather than global, so multiple Client objects with different prefixes can coexist safely.

Index

Constants

View Source
const (
	LabelVerb   = "verb"
	LabelURL    = "url"
	LabelCode   = "code"
	LabelMethod = "method"
	LabelHost   = "host"
)

Metric label keys used across the Kubernetes client metrics. Centralising them as constants prevents typos and keeps the label set obvious at call sites.

View Source
const (
	// ============================================================================
	// Kubernetes Client Metrics
	// ============================================================================
	// KubernetesClientRequestLatencySeconds measures Kubernetes API request
	// latency per verb and URL.
	KubernetesClientRequestLatencySeconds = "{PREFIX}kubernetes_client_request_latency_seconds"
	// KubernetesClientRequestResultTotal counts Kubernetes API request results
	// per code, method and host.
	KubernetesClientRequestResultTotal = "{PREFIX}kubernetes_client_request_result_total"
	// KubernetesClientRateLimiterLatencySeconds measures client-go rate
	// limiter latency per verb and URL.
	KubernetesClientRateLimiterLatencySeconds = "{PREFIX}kubernetes_client_rate_limiter_latency_seconds"
)

Metric name templates. The {PREFIX} placeholder is replaced at construction time by passing a prefix to the constructors or to ReplacePrefix. These constants are intentionally read-only; use ReplacePrefix to build the concrete name for a specific client instance.

Variables

This section is empty.

Functions

func NewRateLimiterLatency

func NewRateLimiterLatency(storage Storage, prefix string) clientgometrics.LatencyMetric

NewRateLimiterLatency returns a client-go metrics.LatencyMetric backed by the given storage. The metric name is resolved from the template using prefix at construction time, so the returned backend is independent of any global state.

func NewRequestResult

func NewRequestResult(storage Storage, extraLabels map[string]string, prefix string) clientgometrics.ResultMetric

NewRequestResult returns a client-go metrics.ResultMetric backed by the given storage. The metric name is resolved from the template using prefix at construction time. extraLabels are added to each emitted counter sample.

func RegisterKubernetesClientMetrics

func RegisterKubernetesClientMetrics(storage Storage, extraLabels map[string]string, prefix string)

RegisterKubernetesClientMetrics registers all Kubernetes client metrics with the provided storage. prefix is substituted for {PREFIX} in the metric name templates. extraLabels are merged into the label set of each metric and let callers attach custom dimensions (for example "operator").

func ReplacePrefix

func ReplacePrefix(metricName, prefix string) string

ReplacePrefix replaces the {PREFIX} placeholder in a metric name template with the provided prefix. Useful in tests or when constructing metric names for external use.

Types

type RateLimiterLatency

type RateLimiterLatency struct {
	// contains filtered or unexported fields
}

RateLimiterLatency observes client-go rate limiter latency.

func (RateLimiterLatency) Observe

func (r RateLimiterLatency) Observe(_ context.Context, verb string, u url.URL, latency time.Duration)

type RequestResult

type RequestResult struct {
	// contains filtered or unexported fields
}

RequestResult counts client-go request results by code, method and host.

func (RequestResult) Increment

func (r RequestResult) Increment(_ context.Context, code, method, host string)

type Storage

type Storage interface {
	RegisterCounter(metric string, labels map[string]string) *prometheus.CounterVec
	CounterAdd(metric string, value float64, labels map[string]string)
	RegisterHistogram(metric string, labels map[string]string, buckets []float64) *prometheus.HistogramVec
	HistogramObserve(metric string, value float64, labels map[string]string, buckets []float64)
}

Storage models the subset of flant/shell-operator's metric storage that this package depends on.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL