mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
add a feature enabled metric
Change-Id: Id872651c4219d4749fc4227da7d944e883e4e355
This commit is contained in:
parent
f38bf1ff3e
commit
80024aefcb
@ -0,0 +1,51 @@
|
||||
/*
|
||||
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 (
|
||||
// featureEnabled is a Prometheus Gauge metrics used for recording the enablement of a k8s feature.
|
||||
featureEnabled = k8smetrics.NewGaugeVec(
|
||||
&k8smetrics.GaugeOpts{
|
||||
Namespace: "k8s",
|
||||
Name: "feature_enabled",
|
||||
Help: "This metric records the result of whether a feature is enabled.",
|
||||
StabilityLevel: k8smetrics.ALPHA,
|
||||
},
|
||||
[]string{"name", "enabled"},
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
legacyregistry.MustRegister(featureEnabled)
|
||||
}
|
||||
|
||||
func ResetFeatureEnabledMetric() {
|
||||
featureEnabled.Reset()
|
||||
}
|
||||
|
||||
func RecordFeatureEnabled(ctx context.Context, name string, enabled bool) error {
|
||||
featureEnabled.WithContext(ctx).WithLabelValues(name, fmt.Sprintf("%v", enabled)).Set(1)
|
||||
return nil
|
||||
}
|
@ -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{"k8s_feature_enabled"}
|
||||
)
|
||||
|
||||
func TestObserveHealthcheck(t *testing.T) {
|
||||
defer legacyregistry.Reset()
|
||||
defer ResetFeatureEnabledMetric()
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
name string
|
||||
enabled bool
|
||||
want string
|
||||
}{
|
||||
{
|
||||
desc: "test enabled",
|
||||
name: "feature-a",
|
||||
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
|
||||
`,
|
||||
},
|
||||
{
|
||||
desc: "test disabled",
|
||||
name: "feature-b",
|
||||
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
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(test.want), testedMetrics...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user