mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 04:06:03 +00:00
Merge pull request #79237 from logicalhan/show-hidden-metrics
enable ability to show hidden metrics
This commit is contained in:
commit
db892ba7bc
@ -92,6 +92,10 @@ func (r *lazyMetric) determineDeprecationStatus(version semver.Version) {
|
||||
if selfVersion.LTE(version) {
|
||||
r.isDeprecated = true
|
||||
}
|
||||
if ShouldShowHidden() {
|
||||
klog.Warningf("Hidden metrics have been manually overridden, showing this very deprecated metric.")
|
||||
return
|
||||
}
|
||||
if selfVersion.LT(version) {
|
||||
klog.Warningf("This metric has been deprecated for more than one release, hiding.")
|
||||
r.isHidden = true
|
||||
|
@ -17,12 +17,36 @@ limitations under the License.
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
|
||||
var (
|
||||
showHiddenOnce sync.Once
|
||||
showHidden atomic.Value
|
||||
)
|
||||
|
||||
// SetShowHidden will enable showing hidden metrics. This will no-opt
|
||||
// after the initial call
|
||||
func SetShowHidden() {
|
||||
showHiddenOnce.Do(func() {
|
||||
showHidden.Store(true)
|
||||
})
|
||||
}
|
||||
|
||||
// ShouldShowHidden returns whether showing hidden deprecated metrics
|
||||
// is enabled. While the primary usecase for this is internal (to determine
|
||||
// registration behavior) this can also be used to introspect
|
||||
func ShouldShowHidden() bool {
|
||||
return showHidden.Load() != nil && showHidden.Load().(bool)
|
||||
}
|
||||
|
||||
// Registerable is an interface for a collector metric which we
|
||||
// will register with KubeRegistry.
|
||||
type Registerable interface {
|
||||
|
@ -17,11 +17,12 @@ limitations under the License.
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -195,4 +196,42 @@ func TestMustRegister(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
func TestShowHiddenMetric(t *testing.T) {
|
||||
registry := NewKubeRegistry(apimachineryversion.Info{
|
||||
Major: "1",
|
||||
Minor: "15",
|
||||
GitVersion: "v1.15.0-alpha-1.12345",
|
||||
})
|
||||
|
||||
expectedMetricCount := 0
|
||||
registry.MustRegister(alphaHiddenCounter)
|
||||
|
||||
ms, err := registry.Gather()
|
||||
if len(ms) != expectedMetricCount {
|
||||
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
|
||||
}
|
||||
showHidden.Store(true)
|
||||
defer showHidden.Store(false)
|
||||
registry.MustRegister(NewCounter(
|
||||
&CounterOpts{
|
||||
Namespace: "some_namespace",
|
||||
Name: "test_alpha_show_hidden_counter",
|
||||
Subsystem: "subsystem",
|
||||
StabilityLevel: ALPHA,
|
||||
Help: "counter help",
|
||||
DeprecatedVersion: &v114,
|
||||
},
|
||||
))
|
||||
expectedMetricCount = 1
|
||||
|
||||
ms, err = registry.Gather()
|
||||
if len(ms) != expectedMetricCount {
|
||||
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Gather failed %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user