mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
Merge pull request #112652 from logicalhan/feature-metric
add a feature enabled metric
This commit is contained in:
commit
8631b2cdf9
@ -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)
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -44,7 +44,7 @@ var (
|
|||||||
// healthcheck is a Prometheus Gauge metrics used for recording the results of a k8s healthcheck.
|
// healthcheck is a Prometheus Gauge metrics used for recording the results of a k8s healthcheck.
|
||||||
healthcheck = k8smetrics.NewGaugeVec(
|
healthcheck = k8smetrics.NewGaugeVec(
|
||||||
&k8smetrics.GaugeOpts{
|
&k8smetrics.GaugeOpts{
|
||||||
Namespace: "k8s",
|
Namespace: "kubernetes",
|
||||||
Name: "healthcheck",
|
Name: "healthcheck",
|
||||||
Help: "This metric records the result of a single healthcheck.",
|
Help: "This metric records the result of a single healthcheck.",
|
||||||
StabilityLevel: k8smetrics.ALPHA,
|
StabilityLevel: k8smetrics.ALPHA,
|
||||||
@ -55,7 +55,7 @@ var (
|
|||||||
// healthchecksTotal is a Prometheus Counter metrics used for counting the results of a k8s healthcheck.
|
// healthchecksTotal is a Prometheus Counter metrics used for counting the results of a k8s healthcheck.
|
||||||
healthchecksTotal = k8smetrics.NewCounterVec(
|
healthchecksTotal = k8smetrics.NewCounterVec(
|
||||||
&k8smetrics.CounterOpts{
|
&k8smetrics.CounterOpts{
|
||||||
Namespace: "k8s",
|
Namespace: "kubernetes",
|
||||||
Name: "healthchecks_total",
|
Name: "healthchecks_total",
|
||||||
Help: "This metric records the results of all healthcheck.",
|
Help: "This metric records the results of all healthcheck.",
|
||||||
StabilityLevel: k8smetrics.ALPHA,
|
StabilityLevel: k8smetrics.ALPHA,
|
||||||
|
@ -26,7 +26,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testedMetrics = []string{"k8s_healthcheck", "k8s_healthchecks_total"}
|
testedMetrics = []string{"kubernetes_healthcheck", "kubernetes_healthchecks_total"}
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestObserveHealthcheck(t *testing.T) {
|
func TestObserveHealthcheck(t *testing.T) {
|
||||||
@ -35,14 +35,14 @@ func TestObserveHealthcheck(t *testing.T) {
|
|||||||
initialState := Error
|
initialState := Error
|
||||||
healthcheckName := "healthcheck-a"
|
healthcheckName := "healthcheck-a"
|
||||||
initialOutput := `
|
initialOutput := `
|
||||||
# HELP k8s_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
||||||
# TYPE k8s_healthcheck gauge
|
# TYPE kubernetes_healthcheck gauge
|
||||||
k8s_healthcheck{name="healthcheck-a",status="error",type="healthz"} 1
|
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 1
|
||||||
k8s_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
|
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
|
||||||
k8s_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
|
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
|
||||||
# HELP k8s_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
# HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
||||||
# TYPE k8s_healthchecks_total counter
|
# TYPE kubernetes_healthchecks_total counter
|
||||||
k8s_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
||||||
`
|
`
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
@ -57,15 +57,15 @@ func TestObserveHealthcheck(t *testing.T) {
|
|||||||
hcType: Healthz,
|
hcType: Healthz,
|
||||||
hcStatus: Pending,
|
hcStatus: Pending,
|
||||||
want: `
|
want: `
|
||||||
# HELP k8s_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
||||||
# TYPE k8s_healthcheck gauge
|
# TYPE kubernetes_healthcheck gauge
|
||||||
k8s_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
|
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
|
||||||
k8s_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 1
|
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 1
|
||||||
k8s_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
|
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
|
||||||
# HELP k8s_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
# HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
||||||
# TYPE k8s_healthchecks_total counter
|
# TYPE kubernetes_healthchecks_total counter
|
||||||
k8s_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
||||||
k8s_healthchecks_total{name="healthcheck-a",status="pending",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,
|
hcType: Healthz,
|
||||||
hcStatus: Success,
|
hcStatus: Success,
|
||||||
want: `
|
want: `
|
||||||
# HELP k8s_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
||||||
# TYPE k8s_healthcheck gauge
|
# TYPE kubernetes_healthcheck gauge
|
||||||
k8s_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
|
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
|
||||||
k8s_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
|
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
|
||||||
k8s_healthcheck{name="healthcheck-a",status="success",type="healthz"} 1
|
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 1
|
||||||
# HELP k8s_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
# HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
||||||
# TYPE k8s_healthchecks_total counter
|
# TYPE kubernetes_healthchecks_total counter
|
||||||
k8s_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
||||||
k8s_healthchecks_total{name="healthcheck-a",status="success",type="healthz"} 1
|
kubernetes_healthchecks_total{name="healthcheck-a",status="success",type="healthz"} 1
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user