Merge pull request #112652 from logicalhan/feature-metric

add a feature enabled metric
This commit is contained in:
Kubernetes Prow Robot 2022-09-22 15:19:03 -07:00 committed by GitHub
commit 8631b2cdf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 156 additions and 29 deletions

View File

@ -0,0 +1,50 @@
/*
Copyright 2022 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 feature
import (
"context"
"fmt"
k8smetrics "k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)
var (
// featureInfo is a Prometheus Gauge metrics used for recording the enablement of a k8s feature.
featureInfo = k8smetrics.NewGaugeVec(
&k8smetrics.GaugeOpts{
Namespace: "kubernetes",
Name: "feature_info",
Help: "This metric records the data about the stage and enablement of a k8s feature.",
StabilityLevel: k8smetrics.ALPHA,
},
[]string{"name", "stage", "enabled"},
)
)
func init() {
legacyregistry.MustRegister(featureInfo)
}
func ResetFeatureInfoMetric() {
featureInfo.Reset()
}
func RecordFeatureInfo(ctx context.Context, name string, stage string, enabled bool) {
featureInfo.WithContext(ctx).WithLabelValues(name, stage, fmt.Sprintf("%v", enabled)).Set(1)
}

View File

@ -0,0 +1,77 @@
/*
Copyright 2022 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 feature
import (
"context"
"strings"
"testing"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/component-base/metrics/testutil"
)
var (
testedMetrics = []string{"kubernetes_feature_info"}
)
func TestObserveHealthcheck(t *testing.T) {
defer legacyregistry.Reset()
defer ResetFeatureInfoMetric()
testCases := []struct {
desc string
name string
stage string
enabled bool
want string
}{
{
desc: "test enabled",
name: "feature-a",
stage: "ALPHA",
enabled: true,
want: `
# HELP kubernetes_feature_info [ALPHA] This metric records the data about the stage and enablement of a k8s feature.
# TYPE kubernetes_feature_info gauge
kubernetes_feature_info{enabled="true",name="feature-a",stage="ALPHA"} 1
`,
},
{
desc: "test disabled",
name: "feature-b",
stage: "BETA",
enabled: false,
want: `
# HELP kubernetes_feature_info [ALPHA] This metric records the data about the stage and enablement of a k8s feature.
# TYPE kubernetes_feature_info gauge
kubernetes_feature_info{enabled="false",name="feature-b",stage="BETA"} 1
`,
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer ResetFeatureInfoMetric()
RecordFeatureInfo(context.Background(), test.name, test.stage, test.enabled)
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(test.want), testedMetrics...); err != nil {
t.Fatal(err)
}
})
}
}

View File

@ -44,7 +44,7 @@ var (
// healthcheck is a Prometheus Gauge metrics used for recording the results of a k8s healthcheck.
healthcheck = k8smetrics.NewGaugeVec(
&k8smetrics.GaugeOpts{
Namespace: "k8s",
Namespace: "kubernetes",
Name: "healthcheck",
Help: "This metric records the result of a single healthcheck.",
StabilityLevel: k8smetrics.ALPHA,
@ -55,7 +55,7 @@ var (
// healthchecksTotal is a Prometheus Counter metrics used for counting the results of a k8s healthcheck.
healthchecksTotal = k8smetrics.NewCounterVec(
&k8smetrics.CounterOpts{
Namespace: "k8s",
Namespace: "kubernetes",
Name: "healthchecks_total",
Help: "This metric records the results of all healthcheck.",
StabilityLevel: k8smetrics.ALPHA,

View File

@ -26,7 +26,7 @@ import (
)
var (
testedMetrics = []string{"k8s_healthcheck", "k8s_healthchecks_total"}
testedMetrics = []string{"kubernetes_healthcheck", "kubernetes_healthchecks_total"}
)
func TestObserveHealthcheck(t *testing.T) {
@ -35,14 +35,14 @@ func TestObserveHealthcheck(t *testing.T) {
initialState := Error
healthcheckName := "healthcheck-a"
initialOutput := `
# HELP k8s_healthcheck [ALPHA] This metric records the result of a single healthcheck.
# TYPE k8s_healthcheck gauge
k8s_healthcheck{name="healthcheck-a",status="error",type="healthz"} 1
k8s_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
k8s_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
# HELP k8s_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
# TYPE k8s_healthchecks_total counter
k8s_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
# TYPE kubernetes_healthcheck gauge
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 1
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
# HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
# TYPE kubernetes_healthchecks_total counter
kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
`
testCases := []struct {
desc string
@ -57,15 +57,15 @@ func TestObserveHealthcheck(t *testing.T) {
hcType: Healthz,
hcStatus: Pending,
want: `
# HELP k8s_healthcheck [ALPHA] This metric records the result of a single healthcheck.
# TYPE k8s_healthcheck gauge
k8s_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
k8s_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 1
k8s_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
# HELP k8s_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
# TYPE k8s_healthchecks_total counter
k8s_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
k8s_healthchecks_total{name="healthcheck-a",status="pending",type="healthz"} 1
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
# TYPE kubernetes_healthcheck gauge
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 1
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
# HELP kubernetes_healthchecks_total [ALPHA] 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="pending",type="healthz"} 1
`,
},
{
@ -74,15 +74,15 @@ func TestObserveHealthcheck(t *testing.T) {
hcType: Healthz,
hcStatus: Success,
want: `
# HELP k8s_healthcheck [ALPHA] This metric records the result of a single healthcheck.
# TYPE k8s_healthcheck gauge
k8s_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
k8s_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
k8s_healthcheck{name="healthcheck-a",status="success",type="healthz"} 1
# HELP k8s_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
# TYPE k8s_healthchecks_total counter
k8s_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
k8s_healthchecks_total{name="healthcheck-a",status="success",type="healthz"} 1
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
# TYPE kubernetes_healthcheck gauge
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 1
# HELP kubernetes_healthchecks_total [ALPHA] 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
`,
},
}