From 04ea1d506b4b6e6be1330e9d2224b07d979e745f Mon Sep 17 00:00:00 2001 From: Han Kang Date: Thu, 29 Jun 2023 09:23:01 -0700 Subject: [PATCH 1/4] promote sli metrics to beta --- .../metrics/prometheus/slis/metrics.go | 4 +-- .../testdata/stable-metrics-list.yaml | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/slis/metrics.go b/staging/src/k8s.io/component-base/metrics/prometheus/slis/metrics.go index 7fb4a8e064e..7907dfad12a 100644 --- a/staging/src/k8s.io/component-base/metrics/prometheus/slis/metrics.go +++ b/staging/src/k8s.io/component-base/metrics/prometheus/slis/metrics.go @@ -37,7 +37,7 @@ var ( Namespace: "kubernetes", Name: "healthcheck", Help: "This metric records the result of a single healthcheck.", - StabilityLevel: k8smetrics.ALPHA, + StabilityLevel: k8smetrics.BETA, }, []string{"name", "type"}, ) @@ -48,7 +48,7 @@ var ( Namespace: "kubernetes", Name: "healthchecks_total", Help: "This metric records the results of all healthcheck.", - StabilityLevel: k8smetrics.ALPHA, + StabilityLevel: k8smetrics.BETA, }, []string{"name", "type", "status"}, ) diff --git a/test/instrumentation/testdata/stable-metrics-list.yaml b/test/instrumentation/testdata/stable-metrics-list.yaml index bf911947898..13d3c70da62 100644 --- a/test/instrumentation/testdata/stable-metrics-list.yaml +++ b/test/instrumentation/testdata/stable-metrics-list.yaml @@ -409,3 +409,37 @@ stabilityLevel: STABLE labels: - resource +- name: healthcheck + namespace: kubernetes + help: This metric records the result of a single healthcheck. + type: Gauge + stabilityLevel: BETA + labels: + - name + - type +- name: healthcheck + namespace: kubernetes + help: This metric records the result of a single healthcheck. + type: Gauge + stabilityLevel: BETA + labels: + - name + - type +- name: healthchecks_total + namespace: kubernetes + help: This metric records the results of all healthcheck. + type: Counter + stabilityLevel: BETA + labels: + - name + - status + - type +- name: healthchecks_total + namespace: kubernetes + help: This metric records the results of all healthcheck. + type: Counter + stabilityLevel: BETA + labels: + - name + - status + - type From 0eeca0987a6cadfcd111770caea7a52696fa8a95 Mon Sep 17 00:00:00 2001 From: Han Kang Date: Thu, 29 Jun 2023 10:14:45 -0700 Subject: [PATCH 2/4] fix tests --- .../metrics/prometheus/slis/metrics_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/staging/src/k8s.io/component-base/metrics/prometheus/slis/metrics_test.go b/staging/src/k8s.io/component-base/metrics/prometheus/slis/metrics_test.go index b4f8df34046..b328c868d8b 100644 --- a/staging/src/k8s.io/component-base/metrics/prometheus/slis/metrics_test.go +++ b/staging/src/k8s.io/component-base/metrics/prometheus/slis/metrics_test.go @@ -37,10 +37,10 @@ func TestObserveHealthcheck(t *testing.T) { initialState := Error healthcheckName := "healthcheck-a" initialOutput := ` - # HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck. + # HELP kubernetes_healthcheck [BETA] This metric records the result of a single healthcheck. # TYPE kubernetes_healthcheck gauge kubernetes_healthcheck{name="healthcheck-a",type="healthz"} 0 - # HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck. + # HELP kubernetes_healthchecks_total [BETA] This metric records the results of all healthcheck. # TYPE kubernetes_healthchecks_total counter kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1 ` @@ -57,10 +57,10 @@ func TestObserveHealthcheck(t *testing.T) { hcType: "healthz", hcStatus: Success, want: ` - # HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck. + # HELP kubernetes_healthcheck [BETA] This metric records the result of a single healthcheck. # TYPE kubernetes_healthcheck gauge kubernetes_healthcheck{name="healthcheck-a",type="healthz"} 1 - # HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck. + # HELP kubernetes_healthchecks_total [BETA] This metric records the results of all healthcheck. # TYPE kubernetes_healthchecks_total counter kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1 kubernetes_healthchecks_total{name="healthcheck-a",status="success",type="healthz"} 1 From 44f1c4dd94d03181f58a463d30b301e6b33e1098 Mon Sep 17 00:00:00 2001 From: Han Kang Date: Thu, 29 Jun 2023 12:27:55 -0700 Subject: [PATCH 3/4] fix duplicate metrics issue --- test/instrumentation/error.go | 2 +- test/instrumentation/main.go | 9 ++++++-- .../testdata/pkg/kubelet/metrics/metrics.go | 23 +++++++++++++++++++ .../testdata/stable-metrics-list.yaml | 17 -------------- .../testdata/test-stable-metrics-list.yaml | 17 ++++++++++++++ 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/test/instrumentation/error.go b/test/instrumentation/error.go index b7e628358d7..0443f4bfefb 100644 --- a/test/instrumentation/error.go +++ b/test/instrumentation/error.go @@ -25,7 +25,7 @@ import ( const ( errNotDirectCall = "Opts for STABLE metric was not directly passed to new metric function" errPositionalArguments = "Positional arguments are not supported" - errStabilityLevel = "StabilityLevel should be passed STABLE, ALPHA or removed" + errStabilityLevel = "StabilityLevel should be passed STABLE, BETA, ALPHA or removed" errInvalidNewMetricCall = "Invalid new metric call, please ensure code compiles" errNonStringAttribute = "Non string attribute is not supported" errBadVariableAttribute = "Metric attribute was not correctly set. Please use only global consts in same file" diff --git a/test/instrumentation/main.go b/test/instrumentation/main.go index 451023cc21a..3cd12bb1a93 100644 --- a/test/instrumentation/main.go +++ b/test/instrumentation/main.go @@ -55,7 +55,7 @@ func main() { fmt.Fprintf(os.Stderr, "USAGE: %s [...]\n", os.Args[0]) os.Exit(64) } - + stableMetricNames := map[string]struct{}{} stableMetrics := []metric{} errors := []error{} @@ -66,7 +66,12 @@ func main() { continue } ms, es := searchPathForStableMetrics(arg) - stableMetrics = append(stableMetrics, ms...) + for _, m := range ms { + if _, ok := stableMetricNames[m.Name]; !ok { + stableMetrics = append(stableMetrics, m) + } + stableMetricNames[m.Name] = struct{}{} + } errors = append(errors, es...) } if addStdin { diff --git a/test/instrumentation/testdata/pkg/kubelet/metrics/metrics.go b/test/instrumentation/testdata/pkg/kubelet/metrics/metrics.go index 8e87f70030a..0d87bdccaad 100644 --- a/test/instrumentation/testdata/pkg/kubelet/metrics/metrics.go +++ b/test/instrumentation/testdata/pkg/kubelet/metrics/metrics.go @@ -132,6 +132,27 @@ var ( }, testLabels, ) + // healthcheck is a Prometheus Gauge metrics used for recording the results of a k8s healthcheck. + healthcheck = metrics.NewGaugeVec( + &metrics.GaugeOpts{ + Namespace: "kubernetes", + Name: "healthcheck", + Help: "This metric records the result of a single healthcheck.", + StabilityLevel: metrics.BETA, + }, + []string{"name", "type"}, + ) + + // healthchecksTotal is a Prometheus Counter metrics used for counting the results of a k8s healthcheck. + healthchecksTotal = metrics.NewCounterVec( + &metrics.CounterOpts{ + Namespace: "kubernetes", + Name: "healthchecks_total", + Help: "This metric records the results of all healthcheck.", + StabilityLevel: metrics.BETA, + }, + []string{"name", "type", "status"}, + ) // PodWorkerDuration is a Histogram that tracks the duration (in seconds) in takes to sync a single pod. // Broken down by the operation type. SummaryMaxAge = metrics.NewSummary( @@ -567,6 +588,8 @@ func Register(collectors ...metrics.StableCollector) { legacyregistry.MustRegister(NodeName) legacyregistry.MustRegister(PodWorkerDuration) legacyregistry.MustRegister(PodStartDuration) + legacyregistry.MustRegister(healthcheck) + legacyregistry.MustRegister(healthchecksTotal) legacyregistry.MustRegister(CgroupManagerDuration) legacyregistry.MustRegister(PodWorkerStartDuration) legacyregistry.MustRegister(ContainersPerPodCount) diff --git a/test/instrumentation/testdata/stable-metrics-list.yaml b/test/instrumentation/testdata/stable-metrics-list.yaml index 13d3c70da62..29716399727 100644 --- a/test/instrumentation/testdata/stable-metrics-list.yaml +++ b/test/instrumentation/testdata/stable-metrics-list.yaml @@ -417,23 +417,6 @@ labels: - name - type -- name: healthcheck - namespace: kubernetes - help: This metric records the result of a single healthcheck. - type: Gauge - stabilityLevel: BETA - labels: - - name - - type -- name: healthchecks_total - namespace: kubernetes - help: This metric records the results of all healthcheck. - type: Counter - stabilityLevel: BETA - labels: - - name - - status - - type - name: healthchecks_total namespace: kubernetes help: This metric records the results of all healthcheck. diff --git a/test/instrumentation/testdata/test-stable-metrics-list.yaml b/test/instrumentation/testdata/test-stable-metrics-list.yaml index 4b6453d943f..4fc74a5849f 100644 --- a/test/instrumentation/testdata/test-stable-metrics-list.yaml +++ b/test/instrumentation/testdata/test-stable-metrics-list.yaml @@ -192,6 +192,23 @@ labels: - namespace - persistentvolumeclaim +- name: healthcheck + namespace: kubernetes + help: This metric records the result of a single healthcheck. + type: Gauge + stabilityLevel: BETA + labels: + - name + - type +- name: healthchecks_total + namespace: kubernetes + help: This metric records the results of all healthcheck. + type: Counter + stabilityLevel: BETA + labels: + - name + - status + - type - name: priority_level_seat_utilization subsystem: subsystem namespace: namespace From d68f5f0d921374a42adabf74c9bcd3db0512470e Mon Sep 17 00:00:00 2001 From: Han Kang Date: Thu, 29 Jun 2023 13:46:12 -0700 Subject: [PATCH 4/4] fix tests --- test/instrumentation/main_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/instrumentation/main_test.go b/test/instrumentation/main_test.go index 747bce15792..2b1f12c7eeb 100644 --- a/test/instrumentation/main_test.go +++ b/test/instrumentation/main_test.go @@ -601,7 +601,7 @@ var _ = metrics.NewCounter( `}, { testName: "Fail on metric with stability set to function return", - err: fmt.Errorf("testdata/metric.go:9:20: StabilityLevel should be passed STABLE, ALPHA or removed"), + err: fmt.Errorf("testdata/metric.go:9:20: %s", errStabilityLevel), src: ` package test import "k8s.io/component-base/metrics" @@ -616,7 +616,7 @@ var _ = metrics.NewCounter( `}, { testName: "error for passing stability as string", - err: fmt.Errorf("testdata/metric.go:6:20: StabilityLevel should be passed STABLE, ALPHA or removed"), + err: fmt.Errorf("testdata/metric.go:6:20: %s", errStabilityLevel), src: ` package test import "k8s.io/component-base/metrics" @@ -628,7 +628,7 @@ var _ = metrics.NewCounter( `}, { testName: "error for passing stability as variable", - err: fmt.Errorf("testdata/metric.go:7:20: StabilityLevel should be passed STABLE, ALPHA or removed"), + err: fmt.Errorf("testdata/metric.go:7:20: %s", errStabilityLevel), src: ` package test import "k8s.io/component-base/metrics"