Documentation
¶
Overview ¶
Package metrics provides Prometheus-style metrics for the Kubernetes client.
There are two steps to set up exporting:
- Register metrics with a storage via RegisterKubernetesClientMetrics.
- 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
- func NewRateLimiterLatency(storage Storage, prefix string) clientgometrics.LatencyMetric
- func NewRequestResult(storage Storage, extraLabels map[string]string, prefix string) clientgometrics.ResultMetric
- func RegisterKubernetesClientMetrics(storage Storage, extraLabels map[string]string, prefix string)
- func ReplacePrefix(metricName, prefix string) string
- type RateLimiterLatency
- type RequestResult
- type Storage
Constants ¶
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.
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 ¶
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 ¶
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.
type RequestResult ¶
type RequestResult struct {
// contains filtered or unexported fields
}
RequestResult counts client-go request results by code, method and host.
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.