github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/promutils/workqueue.go (about) 1 // Source: https://raw.githubusercontent.com/kubernetes/kubernetes/3dbbd0bdf44cb07fdde85aa392adf99ea7e95939/pkg/util/workqueue/prometheus/prometheus.go 2 /* 3 Copyright 2016 The Kubernetes Authors. 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 */ 17 package promutils 18 19 import ( 20 "fmt" 21 22 "k8s.io/client-go/util/workqueue" 23 24 "github.com/prometheus/client_golang/prometheus" 25 ) 26 27 // Package prometheus sets the workqueue DefaultMetricsFactory to produce 28 // prometheus metrics. To use this package, you just have to import it. 29 30 func init() { 31 var provider interface{} //nolint 32 provider = prometheusMetricsProvider{} 33 if p, casted := provider.(workqueue.MetricsProvider); casted { 34 workqueue.SetProvider(p) 35 } else { 36 // This case happens in future versions of client-go where the interface has added methods 37 fmt.Println("Warn: No metricsProvider set for the workqueue") 38 } 39 } 40 41 type prometheusMetricsProvider struct{} 42 43 func (prometheusMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric { 44 unfinishedWork := prometheus.NewGauge(prometheus.GaugeOpts{ 45 Subsystem: name, 46 Name: "unfinished_work_s", 47 Help: "How many seconds of work in progress in workqueue: " + name, 48 }) 49 prometheus.MustRegister(unfinishedWork) 50 return unfinishedWork 51 } 52 53 func (prometheusMetricsProvider) NewLongestRunningProcessorMicrosecondsMetric(name string) workqueue.SettableGaugeMetric { 54 unfinishedWork := prometheus.NewGauge(prometheus.GaugeOpts{ 55 Subsystem: name, 56 Name: "longest_running_processor_us", 57 Help: "How many microseconds longest running processor from workqueue" + name + " takes.", 58 }) 59 prometheus.MustRegister(unfinishedWork) 60 return unfinishedWork 61 } 62 63 func (prometheusMetricsProvider) NewDepthMetric(name string) workqueue.GaugeMetric { 64 depth := prometheus.NewGauge(prometheus.GaugeOpts{ 65 Subsystem: name, 66 Name: "depth", 67 Help: "Current depth of workqueue: " + name, 68 }) 69 prometheus.MustRegister(depth) 70 return depth 71 } 72 73 func (prometheusMetricsProvider) NewAddsMetric(name string) workqueue.CounterMetric { 74 adds := prometheus.NewCounter(prometheus.CounterOpts{ 75 Subsystem: name, 76 Name: "adds", 77 Help: "Total number of adds handled by workqueue: " + name, 78 }) 79 prometheus.MustRegister(adds) 80 return adds 81 } 82 83 func (prometheusMetricsProvider) NewLatencyMetric(name string) workqueue.SummaryMetric { 84 latency := prometheus.NewSummary(prometheus.SummaryOpts{ 85 Subsystem: name, 86 Name: "queue_latency_us", 87 Help: "How long an item stays in workqueue" + name + " before being requested.", 88 }) 89 prometheus.MustRegister(latency) 90 return latency 91 } 92 93 func (prometheusMetricsProvider) NewWorkDurationMetric(name string) workqueue.SummaryMetric { 94 workDuration := prometheus.NewSummary(prometheus.SummaryOpts{ 95 Subsystem: name, 96 Name: "work_duration_us", 97 Help: "How long processing an item from workqueue" + name + " takes.", 98 }) 99 prometheus.MustRegister(workDuration) 100 return workDuration 101 } 102 103 func (prometheusMetricsProvider) NewRetriesMetric(name string) workqueue.CounterMetric { 104 retries := prometheus.NewCounter(prometheus.CounterOpts{ 105 Subsystem: name, 106 Name: "retries", 107 Help: "Total number of retries handled by workqueue: " + name, 108 }) 109 prometheus.MustRegister(retries) 110 return retries 111 }