| Lorenz Brun | e8beaed | 2025-02-05 22:03:50 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // Copyright 2019 The Kubernetes Authors. |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | // Package metricsprovider provides a Prometheus registry for code in K8s |
| 6 | // client-go capable of providing metrics. Currently it registers itself |
| 7 | // as a metrics backend for workqueues, more can be added in the future. |
| 8 | // The registry with all the metrics is available as `Registry`. |
| 9 | package metricsprovider |
| 10 | |
| 11 | import ( |
| 12 | "github.com/prometheus/client_golang/prometheus" |
| 13 | "k8s.io/client-go/util/workqueue" |
| 14 | ) |
| 15 | |
| 16 | // Metrics subsystem and keys used by the workqueue. |
| 17 | const ( |
| 18 | WorkQueueSubsystem = "workqueue" |
| 19 | DepthKey = "depth" |
| 20 | AddsKey = "adds_total" |
| 21 | QueueLatencyKey = "queue_duration_seconds" |
| 22 | WorkDurationKey = "work_duration_seconds" |
| 23 | UnfinishedWorkKey = "unfinished_work_seconds" |
| 24 | LongestRunningProcessorKey = "longest_running_processor_seconds" |
| 25 | RetriesKey = "retries_total" |
| 26 | ) |
| 27 | |
| 28 | var Registry = prometheus.NewRegistry() |
| 29 | |
| 30 | var ( |
| 31 | depth = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 32 | Subsystem: WorkQueueSubsystem, |
| 33 | Name: DepthKey, |
| 34 | Help: "Current depth of workqueue", |
| 35 | }, []string{"name"}) |
| 36 | |
| 37 | adds = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 38 | Subsystem: WorkQueueSubsystem, |
| 39 | Name: AddsKey, |
| 40 | Help: "Total number of adds handled by workqueue", |
| 41 | }, []string{"name"}) |
| 42 | |
| 43 | latency = prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 44 | Subsystem: WorkQueueSubsystem, |
| 45 | Name: QueueLatencyKey, |
| 46 | Help: "How long in seconds an item stays in the workqueue before being requested.", |
| 47 | Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10), |
| 48 | }, []string{"name"}) |
| 49 | |
| 50 | workDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 51 | Subsystem: WorkQueueSubsystem, |
| 52 | Name: WorkDurationKey, |
| 53 | Help: "How long in seconds processing an item from workqueue takes.", |
| 54 | Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10), |
| 55 | }, []string{"name"}) |
| 56 | |
| 57 | unfinished = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 58 | Subsystem: WorkQueueSubsystem, |
| 59 | Name: UnfinishedWorkKey, |
| 60 | Help: "How many seconds of work has done that " + |
| 61 | "is in progress and hasn't been observed by work_duration. Large " + |
| 62 | "values indicate stuck threads. One can deduce the number of stuck " + |
| 63 | "threads by observing the rate at which this increases.", |
| 64 | }, []string{"name"}) |
| 65 | |
| 66 | longestRunningProcessor = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 67 | Subsystem: WorkQueueSubsystem, |
| 68 | Name: LongestRunningProcessorKey, |
| 69 | Help: "How many seconds has the longest running " + |
| 70 | "processor for workqueue been running.", |
| 71 | }, []string{"name"}) |
| 72 | |
| 73 | retries = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 74 | Subsystem: WorkQueueSubsystem, |
| 75 | Name: RetriesKey, |
| 76 | Help: "Total number of retries handled by workqueue", |
| 77 | }, []string{"name"}) |
| 78 | ) |
| 79 | |
| 80 | func init() { |
| 81 | Registry.MustRegister(depth, adds, latency, workDuration, unfinished, longestRunningProcessor, retries) |
| 82 | workqueue.SetProvider(&promProvider{}) |
| 83 | } |
| 84 | |
| 85 | type promProvider struct { |
| 86 | } |
| 87 | |
| 88 | func (promProvider) NewDepthMetric(name string) workqueue.GaugeMetric { |
| 89 | return depth.WithLabelValues(name) |
| 90 | } |
| 91 | |
| 92 | func (promProvider) NewAddsMetric(name string) workqueue.CounterMetric { |
| 93 | return adds.WithLabelValues(name) |
| 94 | } |
| 95 | |
| 96 | func (promProvider) NewLatencyMetric(name string) workqueue.HistogramMetric { |
| 97 | return latency.WithLabelValues(name) |
| 98 | } |
| 99 | |
| 100 | func (promProvider) NewWorkDurationMetric(name string) workqueue.HistogramMetric { |
| 101 | return workDuration.WithLabelValues(name) |
| 102 | } |
| 103 | |
| 104 | func (promProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric { |
| 105 | return unfinished.WithLabelValues(name) |
| 106 | } |
| 107 | |
| 108 | func (promProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric { |
| 109 | return longestRunningProcessor.WithLabelValues(name) |
| 110 | } |
| 111 | |
| 112 | func (promProvider) NewRetriesMetric(name string) workqueue.CounterMetric { |
| 113 | return retries.WithLabelValues(name) |
| 114 | } |