From 9309f1686fb9c4c88362535db56608361dbee333 Mon Sep 17 00:00:00 2001 From: Han Kang Date: Sun, 18 Aug 2019 16:27:46 -0700 Subject: [PATCH] duplicate migrated variants of shared metrics into component-base --- .../src/k8s.io/component-base/metrics/BUILD | 3 + .../metrics/prometheus/clientgo/BUILD | 31 ++++ .../prometheus/clientgo/leaderelection/BUILD | 28 ++++ .../clientgo/leaderelection/metrics.go | 53 +++++++ .../metrics/prometheus/clientgo/metrics.go | 23 +++ .../metrics/prometheus/restclient/BUILD | 32 +++++ .../metrics/prometheus/restclient/metrics.go | 84 +++++++++++ .../metrics/prometheus/workqueue/BUILD | 29 ++++ .../metrics/prometheus/workqueue/metrics.go | 132 ++++++++++++++++++ 9 files changed, 415 insertions(+) create mode 100644 staging/src/k8s.io/component-base/metrics/prometheus/clientgo/BUILD create mode 100644 staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/BUILD create mode 100644 staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go create mode 100644 staging/src/k8s.io/component-base/metrics/prometheus/clientgo/metrics.go create mode 100644 staging/src/k8s.io/component-base/metrics/prometheus/restclient/BUILD create mode 100644 staging/src/k8s.io/component-base/metrics/prometheus/restclient/metrics.go create mode 100644 staging/src/k8s.io/component-base/metrics/prometheus/workqueue/BUILD create mode 100644 staging/src/k8s.io/component-base/metrics/prometheus/workqueue/metrics.go diff --git a/staging/src/k8s.io/component-base/metrics/BUILD b/staging/src/k8s.io/component-base/metrics/BUILD index 774fce33bdb..55a4e605fe1 100644 --- a/staging/src/k8s.io/component-base/metrics/BUILD +++ b/staging/src/k8s.io/component-base/metrics/BUILD @@ -67,6 +67,9 @@ filegroup( srcs = [ ":package-srcs", "//staging/src/k8s.io/component-base/metrics/legacyregistry:all-srcs", + "//staging/src/k8s.io/component-base/metrics/prometheus/clientgo:all-srcs", + "//staging/src/k8s.io/component-base/metrics/prometheus/restclient:all-srcs", + "//staging/src/k8s.io/component-base/metrics/prometheus/workqueue:all-srcs", ], tags = ["automanaged"], ) diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/BUILD b/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/BUILD new file mode 100644 index 00000000000..c519a817965 --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/BUILD @@ -0,0 +1,31 @@ +package(default_visibility = ["//visibility:public"]) + +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["metrics.go"], + importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/prometheus/clientgo", + importpath = "k8s.io/component-base/metrics/prometheus/clientgo", + deps = [ + "//staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection:go_default_library", + "//staging/src/k8s.io/component-base/metrics/prometheus/restclient:go_default_library", + "//staging/src/k8s.io/component-base/metrics/prometheus/workqueue:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [ + ":package-srcs", + "//staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection:all-srcs", + ], + tags = ["automanaged"], +) diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/BUILD b/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/BUILD new file mode 100644 index 00000000000..d4634d4d5e0 --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/BUILD @@ -0,0 +1,28 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["metrics.go"], + importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection", + importpath = "k8s.io/component-base/metrics/prometheus/clientgo/leaderelection", + visibility = ["//visibility:public"], + deps = [ + "//staging/src/k8s.io/client-go/tools/leaderelection:go_default_library", + "//staging/src/k8s.io/component-base/metrics:go_default_library", + "//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go b/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go new file mode 100644 index 00000000000..6592c755b3f --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go @@ -0,0 +1,53 @@ +/* +Copyright 2018 The Kubernetes 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 leaderelection + +import ( + "k8s.io/client-go/tools/leaderelection" + k8smetrics "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" +) + +var ( + leaderGauge = k8smetrics.NewGaugeVec(&k8smetrics.GaugeOpts{ + Name: "leader_election_master_status", + Help: "Gauge of if the reporting system is master of the relevant lease, 0 indicates backup, 1 indicates master. 'name' is the string used to identify the lease. Please make sure to group by name.", + }, []string{"name"}) +) + +func init() { + legacyregistry.MustRegister(leaderGauge) + leaderelection.SetProvider(prometheusMetricsProvider{}) +} + +type prometheusMetricsProvider struct{} + +func (prometheusMetricsProvider) NewLeaderMetric() leaderelection.SwitchMetric { + return &switchAdapter{gauge: leaderGauge} +} + +type switchAdapter struct { + gauge *k8smetrics.GaugeVec +} + +func (s *switchAdapter) On(name string) { + s.gauge.WithLabelValues(name).Set(1.0) +} + +func (s *switchAdapter) Off(name string) { + s.gauge.WithLabelValues(name).Set(0.0) +} diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/metrics.go b/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/metrics.go new file mode 100644 index 00000000000..43574ca9a39 --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/prometheus/clientgo/metrics.go @@ -0,0 +1,23 @@ +/* +Copyright 2019 The Kubernetes 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 clientgo + +import ( + _ "k8s.io/component-base/metrics/prometheus/clientgo/leaderelection" // load leaderelection metrics + _ "k8s.io/component-base/metrics/prometheus/restclient" // load restclient metrics + _ "k8s.io/component-base/metrics/prometheus/workqueue" // load the workqueue metrics +) diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/restclient/BUILD b/staging/src/k8s.io/component-base/metrics/prometheus/restclient/BUILD new file mode 100644 index 00000000000..85098adcaa6 --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/prometheus/restclient/BUILD @@ -0,0 +1,32 @@ +package(default_visibility = ["//visibility:public"]) + +load( + "@io_bazel_rules_go//go:def.bzl", + "go_library", +) + +go_library( + name = "go_default_library", + srcs = ["metrics.go"], + importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/prometheus/restclient", + importpath = "k8s.io/component-base/metrics/prometheus/restclient", + deps = [ + "//staging/src/k8s.io/client-go/tools/metrics:go_default_library", + "//staging/src/k8s.io/component-base/metrics:go_default_library", + "//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library", + "//vendor/github.com/prometheus/client_golang/prometheus:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], +) diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/restclient/metrics.go b/staging/src/k8s.io/component-base/metrics/prometheus/restclient/metrics.go new file mode 100644 index 00000000000..805ef722ef7 --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/prometheus/restclient/metrics.go @@ -0,0 +1,84 @@ +/* +Copyright 2016 The Kubernetes 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 restclient + +import ( + "net/url" + "time" + + "github.com/prometheus/client_golang/prometheus" + + "k8s.io/client-go/tools/metrics" + k8smetrics "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" +) + +var ( + // requestLatency is a Prometheus Summary metric type partitioned by + // "verb" and "url" labels. It is used for the rest client latency metrics. + requestLatency = k8smetrics.NewHistogramVec( + &k8smetrics.HistogramOpts{ + Name: "rest_client_request_duration_seconds", + Help: "Request latency in seconds. Broken down by verb and URL.", + Buckets: prometheus.ExponentialBuckets(0.001, 2, 10), + }, + []string{"verb", "url"}, + ) + + // deprecatedRequestLatency is deprecated, please use requestLatency. + deprecatedRequestLatency = k8smetrics.NewHistogramVec( + &k8smetrics.HistogramOpts{ + Name: "rest_client_request_latency_seconds", + Help: "(Deprecated) Request latency in seconds. Broken down by verb and URL.", + Buckets: prometheus.ExponentialBuckets(0.001, 2, 10), + }, + []string{"verb", "url"}, + ) + + requestResult = k8smetrics.NewCounterVec( + &k8smetrics.CounterOpts{ + Name: "rest_client_requests_total", + Help: "Number of HTTP requests, partitioned by status code, method, and host.", + }, + []string{"code", "method", "host"}, + ) +) + +func init() { + legacyregistry.MustRegister(requestLatency) + legacyregistry.MustRegister(deprecatedRequestLatency) + legacyregistry.MustRegister(requestResult) + metrics.Register(&latencyAdapter{m: requestLatency, dm: deprecatedRequestLatency}, &resultAdapter{requestResult}) +} + +type latencyAdapter struct { + m *k8smetrics.HistogramVec + dm *k8smetrics.HistogramVec +} + +func (l *latencyAdapter) Observe(verb string, u url.URL, latency time.Duration) { + l.m.WithLabelValues(verb, u.String()).Observe(latency.Seconds()) + l.dm.WithLabelValues(verb, u.String()).Observe(latency.Seconds()) +} + +type resultAdapter struct { + m *k8smetrics.CounterVec +} + +func (r *resultAdapter) Increment(code, method, host string) { + r.m.WithLabelValues(code, method, host).Inc() +} diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/workqueue/BUILD b/staging/src/k8s.io/component-base/metrics/prometheus/workqueue/BUILD new file mode 100644 index 00000000000..9b32cc0661e --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/prometheus/workqueue/BUILD @@ -0,0 +1,29 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["metrics.go"], + importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/prometheus/workqueue", + importpath = "k8s.io/component-base/metrics/prometheus/workqueue", + visibility = ["//visibility:public"], + deps = [ + "//staging/src/k8s.io/client-go/util/workqueue:go_default_library", + "//staging/src/k8s.io/component-base/metrics:go_default_library", + "//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library", + "//vendor/github.com/prometheus/client_golang/prometheus:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/workqueue/metrics.go b/staging/src/k8s.io/component-base/metrics/prometheus/workqueue/metrics.go new file mode 100644 index 00000000000..bd9e6d11f05 --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/prometheus/workqueue/metrics.go @@ -0,0 +1,132 @@ +/* +Copyright 2019 The Kubernetes 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 workqueue + +import ( + "github.com/prometheus/client_golang/prometheus" + + "k8s.io/client-go/util/workqueue" + k8smetrics "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" +) + +// Package prometheus sets the workqueue DefaultMetricsFactory to produce +// prometheus metrics. To use this package, you just have to import it. + +// Metrics subsystem and keys used by the workqueue. +const ( + WorkQueueSubsystem = "workqueue" + DepthKey = "depth" + AddsKey = "adds_total" + QueueLatencyKey = "queue_duration_seconds" + WorkDurationKey = "work_duration_seconds" + UnfinishedWorkKey = "unfinished_work_seconds" + LongestRunningProcessorKey = "longest_running_processor_seconds" + RetriesKey = "retries_total" +) + +var ( + depth = k8smetrics.NewGaugeVec(&k8smetrics.GaugeOpts{ + Subsystem: WorkQueueSubsystem, + Name: DepthKey, + Help: "Current depth of workqueue", + }, []string{"name"}) + + adds = k8smetrics.NewCounterVec(&k8smetrics.CounterOpts{ + Subsystem: WorkQueueSubsystem, + Name: AddsKey, + Help: "Total number of adds handled by workqueue", + }, []string{"name"}) + + latency = k8smetrics.NewHistogramVec(&k8smetrics.HistogramOpts{ + Subsystem: WorkQueueSubsystem, + Name: QueueLatencyKey, + Help: "How long in seconds an item stays in workqueue before being requested.", + Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10), + }, []string{"name"}) + + workDuration = k8smetrics.NewHistogramVec(&k8smetrics.HistogramOpts{ + Subsystem: WorkQueueSubsystem, + Name: WorkDurationKey, + Help: "How long in seconds processing an item from workqueue takes.", + Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10), + }, []string{"name"}) + + unfinished = k8smetrics.NewGaugeVec(&k8smetrics.GaugeOpts{ + Subsystem: WorkQueueSubsystem, + Name: UnfinishedWorkKey, + Help: "How many seconds of work has done that " + + "is in progress and hasn't been observed by work_duration. Large " + + "values indicate stuck threads. One can deduce the number of stuck " + + "threads by observing the rate at which this increases.", + }, []string{"name"}) + + longestRunningProcessor = k8smetrics.NewGaugeVec(&k8smetrics.GaugeOpts{ + Subsystem: WorkQueueSubsystem, + Name: LongestRunningProcessorKey, + Help: "How many seconds has the longest running " + + "processor for workqueue been running.", + }, []string{"name"}) + + retries = k8smetrics.NewCounterVec(&k8smetrics.CounterOpts{ + Subsystem: WorkQueueSubsystem, + Name: RetriesKey, + Help: "Total number of retries handled by workqueue", + }, []string{"name"}) + + metrics = []k8smetrics.Registerable{ + depth, adds, latency, workDuration, unfinished, longestRunningProcessor, retries, + } +) + +type prometheusMetricsProvider struct { +} + +func init() { + for _, m := range metrics { + legacyregistry.MustRegister(m) + } + workqueue.SetProvider(prometheusMetricsProvider{}) +} + +func (prometheusMetricsProvider) NewDepthMetric(name string) workqueue.GaugeMetric { + return depth.WithLabelValues(name) +} + +func (prometheusMetricsProvider) NewAddsMetric(name string) workqueue.CounterMetric { + return adds.WithLabelValues(name) +} + +func (prometheusMetricsProvider) NewLatencyMetric(name string) workqueue.HistogramMetric { + return latency.WithLabelValues(name) +} + +func (prometheusMetricsProvider) NewWorkDurationMetric(name string) workqueue.HistogramMetric { + return workDuration.WithLabelValues(name) +} + +func (prometheusMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric { + return unfinished.WithLabelValues(name) +} + +func (prometheusMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric { + return longestRunningProcessor.WithLabelValues(name) +} + +func (prometheusMetricsProvider) NewRetriesMetric(name string) workqueue.CounterMetric { + return retries.WithLabelValues(name) +}