2018-08-22 19:33:23 +00:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2021-04-13 08:00:00 +00:00
|
|
|
"context"
|
2018-08-22 19:33:23 +00:00
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"k8s.io/client-go/tools/metrics"
|
|
|
|
"k8s.io/client-go/util/workqueue"
|
|
|
|
)
|
|
|
|
|
2019-11-29 10:56:34 +00:00
|
|
|
const workqueueMetricsNamespace = metricsNamespace + "_workqueue"
|
2018-08-22 19:33:23 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
// Metrics for client-go's HTTP requests.
|
|
|
|
clientGoRequestResultMetricVec = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "http_request_total",
|
|
|
|
Help: "Total number of HTTP requests to the Kubernetes API by status code.",
|
|
|
|
},
|
|
|
|
[]string{"status_code"},
|
|
|
|
)
|
|
|
|
clientGoRequestLatencyMetricVec = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "http_request_duration_seconds",
|
|
|
|
Help: "Summary of latencies for HTTP requests to the Kubernetes API by endpoint.",
|
|
|
|
Objectives: map[float64]float64{},
|
|
|
|
},
|
|
|
|
[]string{"endpoint"},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Definition of metrics for client-go workflow metrics provider
|
|
|
|
clientGoWorkqueueDepthMetricVec = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: workqueueMetricsNamespace,
|
|
|
|
Name: "depth",
|
|
|
|
Help: "Current depth of the work queue.",
|
|
|
|
},
|
|
|
|
[]string{"queue_name"},
|
|
|
|
)
|
|
|
|
clientGoWorkqueueAddsMetricVec = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: workqueueMetricsNamespace,
|
|
|
|
Name: "items_total",
|
|
|
|
Help: "Total number of items added to the work queue.",
|
|
|
|
},
|
|
|
|
[]string{"queue_name"},
|
|
|
|
)
|
|
|
|
clientGoWorkqueueLatencyMetricVec = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Namespace: workqueueMetricsNamespace,
|
|
|
|
Name: "latency_seconds",
|
|
|
|
Help: "How long an item stays in the work queue.",
|
|
|
|
Objectives: map[float64]float64{},
|
|
|
|
},
|
|
|
|
[]string{"queue_name"},
|
|
|
|
)
|
2018-12-04 03:19:44 +00:00
|
|
|
clientGoWorkqueueUnfinishedWorkSecondsMetricVec = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: workqueueMetricsNamespace,
|
|
|
|
Name: "unfinished_work_seconds",
|
|
|
|
Help: "How long an item has remained unfinished in the work queue.",
|
|
|
|
},
|
|
|
|
[]string{"queue_name"},
|
|
|
|
)
|
|
|
|
clientGoWorkqueueLongestRunningProcessorMetricVec = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: workqueueMetricsNamespace,
|
2018-12-18 16:14:30 +00:00
|
|
|
Name: "longest_running_processor_seconds",
|
2018-12-21 08:46:36 +00:00
|
|
|
Help: "Duration of the longest running processor in the work queue.",
|
2018-12-04 03:19:44 +00:00
|
|
|
},
|
|
|
|
[]string{"queue_name"},
|
|
|
|
)
|
2018-08-22 19:33:23 +00:00
|
|
|
clientGoWorkqueueWorkDurationMetricVec = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Namespace: workqueueMetricsNamespace,
|
|
|
|
Name: "work_duration_seconds",
|
|
|
|
Help: "How long processing an item from the work queue takes.",
|
|
|
|
Objectives: map[float64]float64{},
|
|
|
|
},
|
|
|
|
[]string{"queue_name"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Definition of dummy metric used as a placeholder if we don't want to observe some data.
|
|
|
|
type noopMetric struct{}
|
|
|
|
|
|
|
|
func (noopMetric) Inc() {}
|
|
|
|
func (noopMetric) Dec() {}
|
|
|
|
func (noopMetric) Observe(float64) {}
|
2019-04-18 20:59:29 +00:00
|
|
|
func (noopMetric) Set(float64) {}
|
2018-08-22 19:33:23 +00:00
|
|
|
|
|
|
|
// Definition of client-go metrics adapters for HTTP requests observation
|
|
|
|
type clientGoRequestMetricAdapter struct{}
|
|
|
|
|
|
|
|
func (f *clientGoRequestMetricAdapter) Register(registerer prometheus.Registerer) {
|
2020-04-06 16:23:02 +00:00
|
|
|
metrics.Register(
|
|
|
|
metrics.RegisterOpts{
|
|
|
|
RequestLatency: f,
|
|
|
|
RequestResult: f,
|
|
|
|
},
|
|
|
|
)
|
2019-08-13 05:55:07 +00:00
|
|
|
registerer.MustRegister(
|
|
|
|
clientGoRequestResultMetricVec,
|
|
|
|
clientGoRequestLatencyMetricVec,
|
|
|
|
)
|
2018-08-22 19:33:23 +00:00
|
|
|
}
|
2021-04-13 08:00:00 +00:00
|
|
|
func (clientGoRequestMetricAdapter) Increment(ctx context.Context, code string, method string, host string) {
|
2018-08-22 19:33:23 +00:00
|
|
|
clientGoRequestResultMetricVec.WithLabelValues(code).Inc()
|
|
|
|
}
|
2021-04-13 08:00:00 +00:00
|
|
|
func (clientGoRequestMetricAdapter) Observe(ctx context.Context, verb string, u url.URL, latency time.Duration) {
|
2018-08-22 19:33:23 +00:00
|
|
|
clientGoRequestLatencyMetricVec.WithLabelValues(u.EscapedPath()).Observe(latency.Seconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Definition of client-go workqueue metrics provider definition
|
|
|
|
type clientGoWorkqueueMetricsProvider struct{}
|
|
|
|
|
|
|
|
func (f *clientGoWorkqueueMetricsProvider) Register(registerer prometheus.Registerer) {
|
|
|
|
workqueue.SetProvider(f)
|
2019-08-13 05:55:07 +00:00
|
|
|
registerer.MustRegister(
|
|
|
|
clientGoWorkqueueDepthMetricVec,
|
|
|
|
clientGoWorkqueueAddsMetricVec,
|
|
|
|
clientGoWorkqueueLatencyMetricVec,
|
|
|
|
clientGoWorkqueueWorkDurationMetricVec,
|
|
|
|
clientGoWorkqueueUnfinishedWorkSecondsMetricVec,
|
|
|
|
clientGoWorkqueueLongestRunningProcessorMetricVec,
|
|
|
|
)
|
2018-08-22 19:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *clientGoWorkqueueMetricsProvider) NewDepthMetric(name string) workqueue.GaugeMetric {
|
|
|
|
return clientGoWorkqueueDepthMetricVec.WithLabelValues(name)
|
|
|
|
}
|
|
|
|
func (f *clientGoWorkqueueMetricsProvider) NewAddsMetric(name string) workqueue.CounterMetric {
|
|
|
|
return clientGoWorkqueueAddsMetricVec.WithLabelValues(name)
|
|
|
|
}
|
2019-04-18 20:59:29 +00:00
|
|
|
func (f *clientGoWorkqueueMetricsProvider) NewLatencyMetric(name string) workqueue.HistogramMetric {
|
2019-11-29 10:56:34 +00:00
|
|
|
return clientGoWorkqueueLatencyMetricVec.WithLabelValues(name)
|
2018-08-22 19:33:23 +00:00
|
|
|
}
|
2019-04-18 20:59:29 +00:00
|
|
|
func (f *clientGoWorkqueueMetricsProvider) NewWorkDurationMetric(name string) workqueue.HistogramMetric {
|
2019-11-29 10:56:34 +00:00
|
|
|
return clientGoWorkqueueWorkDurationMetricVec.WithLabelValues(name)
|
2018-08-22 19:33:23 +00:00
|
|
|
}
|
2018-12-04 03:19:44 +00:00
|
|
|
func (f *clientGoWorkqueueMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric {
|
|
|
|
return clientGoWorkqueueUnfinishedWorkSecondsMetricVec.WithLabelValues(name)
|
|
|
|
}
|
2019-04-18 20:59:29 +00:00
|
|
|
func (f *clientGoWorkqueueMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric {
|
|
|
|
return clientGoWorkqueueLongestRunningProcessorMetricVec.WithLabelValues(name)
|
2018-12-04 03:19:44 +00:00
|
|
|
}
|
2018-08-22 19:33:23 +00:00
|
|
|
func (clientGoWorkqueueMetricsProvider) NewRetriesMetric(name string) workqueue.CounterMetric {
|
2018-11-27 16:44:29 +00:00
|
|
|
// Retries are not used so the metric is omitted.
|
2018-08-22 19:33:23 +00:00
|
|
|
return noopMetric{}
|
|
|
|
}
|