mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-19 16:49:35 +00:00
rename metric and add feature stage to metrics
Change-Id: Iccab766ba2ee16ef54abb79e41ee61a6117fa7dd
This commit is contained in:
parent
80024aefcb
commit
dc2c486059
@ -25,27 +25,26 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// featureEnabled is a Prometheus Gauge metrics used for recording the enablement of a k8s feature.
|
||||
featureEnabled = k8smetrics.NewGaugeVec(
|
||||
// featureInfo is a Prometheus Gauge metrics used for recording the enablement of a k8s feature.
|
||||
featureInfo = k8smetrics.NewGaugeVec(
|
||||
&k8smetrics.GaugeOpts{
|
||||
Namespace: "k8s",
|
||||
Name: "feature_enabled",
|
||||
Help: "This metric records the result of whether a feature is enabled.",
|
||||
Name: "feature_info",
|
||||
Help: "This metric records the data about the stage and enablement of a k8s feature.",
|
||||
StabilityLevel: k8smetrics.ALPHA,
|
||||
},
|
||||
[]string{"name", "enabled"},
|
||||
[]string{"name", "stage", "enabled"},
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
legacyregistry.MustRegister(featureEnabled)
|
||||
legacyregistry.MustRegister(featureInfo)
|
||||
}
|
||||
|
||||
func ResetFeatureEnabledMetric() {
|
||||
featureEnabled.Reset()
|
||||
func ResetFeatureInfoMetric() {
|
||||
featureInfo.Reset()
|
||||
}
|
||||
|
||||
func RecordFeatureEnabled(ctx context.Context, name string, enabled bool) error {
|
||||
featureEnabled.WithContext(ctx).WithLabelValues(name, fmt.Sprintf("%v", enabled)).Set(1)
|
||||
return nil
|
||||
func RecordFeatureInfo(ctx context.Context, name string, stage string, enabled bool) {
|
||||
featureInfo.WithContext(ctx).WithLabelValues(name, stage, fmt.Sprintf("%v", enabled)).Set(1)
|
||||
}
|
||||
|
@ -26,48 +26,48 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
testedMetrics = []string{"k8s_feature_enabled"}
|
||||
testedMetrics = []string{"k8s_feature_info"}
|
||||
)
|
||||
|
||||
func TestObserveHealthcheck(t *testing.T) {
|
||||
defer legacyregistry.Reset()
|
||||
defer ResetFeatureEnabledMetric()
|
||||
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 k8s_feature_enabled [ALPHA] This metric records the result of whether a feature is enabled.
|
||||
# TYPE k8s_feature_enabled gauge
|
||||
k8s_feature_enabled{enabled="true",name="feature-a"} 1
|
||||
# HELP k8s_feature_info [ALPHA] This metric records the data about the stage and enablement of a k8s feature.
|
||||
# TYPE k8s_feature_info gauge
|
||||
k8s_feature_info{enabled="true",name="feature-a",stage="ALPHA"} 1
|
||||
`,
|
||||
},
|
||||
{
|
||||
desc: "test disabled",
|
||||
name: "feature-b",
|
||||
stage: "BETA",
|
||||
enabled: false,
|
||||
want: `
|
||||
# HELP k8s_feature_enabled [ALPHA] This metric records the result of whether a feature is enabled.
|
||||
# TYPE k8s_feature_enabled gauge
|
||||
k8s_feature_enabled{enabled="false",name="feature-b"} 1
|
||||
# HELP k8s_feature_info [ALPHA] This metric records the data about the stage and enablement of a k8s feature.
|
||||
# TYPE k8s_feature_info gauge
|
||||
k8s_feature_info{enabled="false",name="feature-b",stage="BETA"} 1
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
defer ResetFeatureEnabledMetric()
|
||||
err := RecordFeatureEnabled(context.Background(), test.name, test.enabled)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected err: %v", err)
|
||||
}
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user