Skip to content

Commit 9309f16

Browse files
author
Han Kang
committed
duplicate migrated variants of shared metrics into component-base
1 parent b037203 commit 9309f16

File tree

9 files changed

+415
-0
lines changed

9 files changed

+415
-0
lines changed

staging/src/k8s.io/component-base/metrics/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ filegroup(
6767
srcs = [
6868
":package-srcs",
6969
"//staging/src/k8s.io/component-base/metrics/legacyregistry:all-srcs",
70+
"//staging/src/k8s.io/component-base/metrics/prometheus/clientgo:all-srcs",
71+
"//staging/src/k8s.io/component-base/metrics/prometheus/restclient:all-srcs",
72+
"//staging/src/k8s.io/component-base/metrics/prometheus/workqueue:all-srcs",
7073
],
7174
tags = ["automanaged"],
7275
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
4+
5+
go_library(
6+
name = "go_default_library",
7+
srcs = ["metrics.go"],
8+
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/prometheus/clientgo",
9+
importpath = "k8s.io/component-base/metrics/prometheus/clientgo",
10+
deps = [
11+
"//staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection:go_default_library",
12+
"//staging/src/k8s.io/component-base/metrics/prometheus/restclient:go_default_library",
13+
"//staging/src/k8s.io/component-base/metrics/prometheus/workqueue:go_default_library",
14+
],
15+
)
16+
17+
filegroup(
18+
name = "package-srcs",
19+
srcs = glob(["**"]),
20+
tags = ["automanaged"],
21+
visibility = ["//visibility:private"],
22+
)
23+
24+
filegroup(
25+
name = "all-srcs",
26+
srcs = [
27+
":package-srcs",
28+
"//staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection:all-srcs",
29+
],
30+
tags = ["automanaged"],
31+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["metrics.go"],
6+
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection",
7+
importpath = "k8s.io/component-base/metrics/prometheus/clientgo/leaderelection",
8+
visibility = ["//visibility:public"],
9+
deps = [
10+
"//staging/src/k8s.io/client-go/tools/leaderelection:go_default_library",
11+
"//staging/src/k8s.io/component-base/metrics:go_default_library",
12+
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
13+
],
14+
)
15+
16+
filegroup(
17+
name = "package-srcs",
18+
srcs = glob(["**"]),
19+
tags = ["automanaged"],
20+
visibility = ["//visibility:private"],
21+
)
22+
23+
filegroup(
24+
name = "all-srcs",
25+
srcs = [":package-srcs"],
26+
tags = ["automanaged"],
27+
visibility = ["//visibility:public"],
28+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2018 The Kubernetes 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+
17+
package leaderelection
18+
19+
import (
20+
"k8s.io/client-go/tools/leaderelection"
21+
k8smetrics "k8s.io/component-base/metrics"
22+
"k8s.io/component-base/metrics/legacyregistry"
23+
)
24+
25+
var (
26+
leaderGauge = k8smetrics.NewGaugeVec(&k8smetrics.GaugeOpts{
27+
Name: "leader_election_master_status",
28+
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.",
29+
}, []string{"name"})
30+
)
31+
32+
func init() {
33+
legacyregistry.MustRegister(leaderGauge)
34+
leaderelection.SetProvider(prometheusMetricsProvider{})
35+
}
36+
37+
type prometheusMetricsProvider struct{}
38+
39+
func (prometheusMetricsProvider) NewLeaderMetric() leaderelection.SwitchMetric {
40+
return &switchAdapter{gauge: leaderGauge}
41+
}
42+
43+
type switchAdapter struct {
44+
gauge *k8smetrics.GaugeVec
45+
}
46+
47+
func (s *switchAdapter) On(name string) {
48+
s.gauge.WithLabelValues(name).Set(1.0)
49+
}
50+
51+
func (s *switchAdapter) Off(name string) {
52+
s.gauge.WithLabelValues(name).Set(0.0)
53+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2019 The Kubernetes 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+
17+
package clientgo
18+
19+
import (
20+
_ "k8s.io/component-base/metrics/prometheus/clientgo/leaderelection" // load leaderelection metrics
21+
_ "k8s.io/component-base/metrics/prometheus/restclient" // load restclient metrics
22+
_ "k8s.io/component-base/metrics/prometheus/workqueue" // load the workqueue metrics
23+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load(
4+
"@io_bazel_rules_go//go:def.bzl",
5+
"go_library",
6+
)
7+
8+
go_library(
9+
name = "go_default_library",
10+
srcs = ["metrics.go"],
11+
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/prometheus/restclient",
12+
importpath = "k8s.io/component-base/metrics/prometheus/restclient",
13+
deps = [
14+
"//staging/src/k8s.io/client-go/tools/metrics:go_default_library",
15+
"//staging/src/k8s.io/component-base/metrics:go_default_library",
16+
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
17+
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
18+
],
19+
)
20+
21+
filegroup(
22+
name = "package-srcs",
23+
srcs = glob(["**"]),
24+
tags = ["automanaged"],
25+
visibility = ["//visibility:private"],
26+
)
27+
28+
filegroup(
29+
name = "all-srcs",
30+
srcs = [":package-srcs"],
31+
tags = ["automanaged"],
32+
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2016 The Kubernetes 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+
17+
package restclient
18+
19+
import (
20+
"net/url"
21+
"time"
22+
23+
"github.com/prometheus/client_golang/prometheus"
24+
25+
"k8s.io/client-go/tools/metrics"
26+
k8smetrics "k8s.io/component-base/metrics"
27+
"k8s.io/component-base/metrics/legacyregistry"
28+
)
29+
30+
var (
31+
// requestLatency is a Prometheus Summary metric type partitioned by
32+
// "verb" and "url" labels. It is used for the rest client latency metrics.
33+
requestLatency = k8smetrics.NewHistogramVec(
34+
&k8smetrics.HistogramOpts{
35+
Name: "rest_client_request_duration_seconds",
36+
Help: "Request latency in seconds. Broken down by verb and URL.",
37+
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
38+
},
39+
[]string{"verb", "url"},
40+
)
41+
42+
// deprecatedRequestLatency is deprecated, please use requestLatency.
43+
deprecatedRequestLatency = k8smetrics.NewHistogramVec(
44+
&k8smetrics.HistogramOpts{
45+
Name: "rest_client_request_latency_seconds",
46+
Help: "(Deprecated) Request latency in seconds. Broken down by verb and URL.",
47+
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
48+
},
49+
[]string{"verb", "url"},
50+
)
51+
52+
requestResult = k8smetrics.NewCounterVec(
53+
&k8smetrics.CounterOpts{
54+
Name: "rest_client_requests_total",
55+
Help: "Number of HTTP requests, partitioned by status code, method, and host.",
56+
},
57+
[]string{"code", "method", "host"},
58+
)
59+
)
60+
61+
func init() {
62+
legacyregistry.MustRegister(requestLatency)
63+
legacyregistry.MustRegister(deprecatedRequestLatency)
64+
legacyregistry.MustRegister(requestResult)
65+
metrics.Register(&latencyAdapter{m: requestLatency, dm: deprecatedRequestLatency}, &resultAdapter{requestResult})
66+
}
67+
68+
type latencyAdapter struct {
69+
m *k8smetrics.HistogramVec
70+
dm *k8smetrics.HistogramVec
71+
}
72+
73+
func (l *latencyAdapter) Observe(verb string, u url.URL, latency time.Duration) {
74+
l.m.WithLabelValues(verb, u.String()).Observe(latency.Seconds())
75+
l.dm.WithLabelValues(verb, u.String()).Observe(latency.Seconds())
76+
}
77+
78+
type resultAdapter struct {
79+
m *k8smetrics.CounterVec
80+
}
81+
82+
func (r *resultAdapter) Increment(code, method, host string) {
83+
r.m.WithLabelValues(code, method, host).Inc()
84+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["metrics.go"],
6+
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/prometheus/workqueue",
7+
importpath = "k8s.io/component-base/metrics/prometheus/workqueue",
8+
visibility = ["//visibility:public"],
9+
deps = [
10+
"//staging/src/k8s.io/client-go/util/workqueue:go_default_library",
11+
"//staging/src/k8s.io/component-base/metrics:go_default_library",
12+
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
13+
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
14+
],
15+
)
16+
17+
filegroup(
18+
name = "package-srcs",
19+
srcs = glob(["**"]),
20+
tags = ["automanaged"],
21+
visibility = ["//visibility:private"],
22+
)
23+
24+
filegroup(
25+
name = "all-srcs",
26+
srcs = [":package-srcs"],
27+
tags = ["automanaged"],
28+
visibility = ["//visibility:public"],
29+
)

0 commit comments

Comments
 (0)