github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/scrape/discovery/kubernetes/client_metrics.go (about) 1 // Copyright 2018 The Prometheus Authors 2 // Copyright 2021 The Pyroscope Authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package kubernetes 17 18 import ( 19 "context" 20 "net/url" 21 "time" 22 23 "github.com/prometheus/client_golang/prometheus" 24 "k8s.io/client-go/tools/metrics" 25 "k8s.io/client-go/util/workqueue" 26 ) 27 28 const workqueueMetricsNamespace = metricsNamespace + "_workqueue" 29 30 var ( 31 // Metrics for client-go's HTTP requests. 32 clientGoRequestResultMetricVec = prometheus.NewCounterVec( 33 prometheus.CounterOpts{ 34 Namespace: metricsNamespace, 35 Name: "http_request_total", 36 Help: "Total number of HTTP requests to the Kubernetes API by status code.", 37 }, 38 []string{"status_code"}, 39 ) 40 clientGoRequestLatencyMetricVec = prometheus.NewSummaryVec( 41 prometheus.SummaryOpts{ 42 Namespace: metricsNamespace, 43 Name: "http_request_duration_seconds", 44 Help: "Summary of latencies for HTTP requests to the Kubernetes API by endpoint.", 45 Objectives: map[float64]float64{}, 46 }, 47 []string{"endpoint"}, 48 ) 49 50 // Definition of metrics for client-go workflow metrics provider 51 clientGoWorkqueueDepthMetricVec = prometheus.NewGaugeVec( 52 prometheus.GaugeOpts{ 53 Namespace: workqueueMetricsNamespace, 54 Name: "depth", 55 Help: "Current depth of the work queue.", 56 }, 57 []string{"queue_name"}, 58 ) 59 clientGoWorkqueueAddsMetricVec = prometheus.NewCounterVec( 60 prometheus.CounterOpts{ 61 Namespace: workqueueMetricsNamespace, 62 Name: "items_total", 63 Help: "Total number of items added to the work queue.", 64 }, 65 []string{"queue_name"}, 66 ) 67 clientGoWorkqueueLatencyMetricVec = prometheus.NewSummaryVec( 68 prometheus.SummaryOpts{ 69 Namespace: workqueueMetricsNamespace, 70 Name: "latency_seconds", 71 Help: "How long an item stays in the work queue.", 72 Objectives: map[float64]float64{}, 73 }, 74 []string{"queue_name"}, 75 ) 76 clientGoWorkqueueUnfinishedWorkSecondsMetricVec = prometheus.NewGaugeVec( 77 prometheus.GaugeOpts{ 78 Namespace: workqueueMetricsNamespace, 79 Name: "unfinished_work_seconds", 80 Help: "How long an item has remained unfinished in the work queue.", 81 }, 82 []string{"queue_name"}, 83 ) 84 clientGoWorkqueueLongestRunningProcessorMetricVec = prometheus.NewGaugeVec( 85 prometheus.GaugeOpts{ 86 Namespace: workqueueMetricsNamespace, 87 Name: "longest_running_processor_seconds", 88 Help: "Duration of the longest running processor in the work queue.", 89 }, 90 []string{"queue_name"}, 91 ) 92 clientGoWorkqueueWorkDurationMetricVec = prometheus.NewSummaryVec( 93 prometheus.SummaryOpts{ 94 Namespace: workqueueMetricsNamespace, 95 Name: "work_duration_seconds", 96 Help: "How long processing an item from the work queue takes.", 97 Objectives: map[float64]float64{}, 98 }, 99 []string{"queue_name"}, 100 ) 101 ) 102 103 // Definition of dummy metric used as a placeholder if we don't want to observe some data. 104 type noopMetric struct{} 105 106 func (noopMetric) Inc() {} 107 func (noopMetric) Dec() {} 108 func (noopMetric) Observe(float64) {} 109 func (noopMetric) Set(float64) {} 110 111 // Definition of client-go metrics adapters for HTTP requests observation 112 type clientGoRequestMetricAdapter struct{} 113 114 func (f *clientGoRequestMetricAdapter) Register(registerer prometheus.Registerer) { 115 metrics.Register( 116 metrics.RegisterOpts{ 117 RequestLatency: f, 118 RequestResult: f, 119 }, 120 ) 121 registerer.MustRegister( 122 clientGoRequestResultMetricVec, 123 clientGoRequestLatencyMetricVec, 124 ) 125 } 126 127 func (clientGoRequestMetricAdapter) Increment(_ context.Context, code, _, _ string) { 128 clientGoRequestResultMetricVec.WithLabelValues(code).Inc() 129 } 130 131 func (clientGoRequestMetricAdapter) Observe(_ context.Context, _ string, u url.URL, latency time.Duration) { 132 clientGoRequestLatencyMetricVec.WithLabelValues(u.EscapedPath()).Observe(latency.Seconds()) 133 } 134 135 // Definition of client-go workqueue metrics provider definition 136 type clientGoWorkqueueMetricsProvider struct{} 137 138 func (f *clientGoWorkqueueMetricsProvider) Register(registerer prometheus.Registerer) { 139 workqueue.SetProvider(f) 140 registerer.MustRegister( 141 clientGoWorkqueueDepthMetricVec, 142 clientGoWorkqueueAddsMetricVec, 143 clientGoWorkqueueLatencyMetricVec, 144 clientGoWorkqueueWorkDurationMetricVec, 145 clientGoWorkqueueUnfinishedWorkSecondsMetricVec, 146 clientGoWorkqueueLongestRunningProcessorMetricVec, 147 ) 148 } 149 150 func (*clientGoWorkqueueMetricsProvider) NewDepthMetric(name string) workqueue.GaugeMetric { 151 return clientGoWorkqueueDepthMetricVec.WithLabelValues(name) 152 } 153 154 func (*clientGoWorkqueueMetricsProvider) NewAddsMetric(name string) workqueue.CounterMetric { 155 return clientGoWorkqueueAddsMetricVec.WithLabelValues(name) 156 } 157 158 func (*clientGoWorkqueueMetricsProvider) NewLatencyMetric(name string) workqueue.HistogramMetric { 159 return clientGoWorkqueueLatencyMetricVec.WithLabelValues(name) 160 } 161 162 func (*clientGoWorkqueueMetricsProvider) NewWorkDurationMetric(name string) workqueue.HistogramMetric { 163 return clientGoWorkqueueWorkDurationMetricVec.WithLabelValues(name) 164 } 165 166 func (*clientGoWorkqueueMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric { 167 return clientGoWorkqueueUnfinishedWorkSecondsMetricVec.WithLabelValues(name) 168 } 169 170 func (*clientGoWorkqueueMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric { 171 return clientGoWorkqueueLongestRunningProcessorMetricVec.WithLabelValues(name) 172 } 173 174 func (clientGoWorkqueueMetricsProvider) NewRetriesMetric(_ string) workqueue.CounterMetric { 175 // Retries are not used so the metric is omitted. 176 return noopMetric{} 177 }