Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable ability to show hidden metrics #79237

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions staging/src/k8s.io/component-base/metrics/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions staging/src/k8s.io/component-base/metrics/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this not a property on the registry?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial iteration actually did that but I ended up changing my mind.

My reasoning was this: we are not guaranteed to have a single registry per component (kubelet already doesn't), if we were to have this as a property on the registry, we would either need to have an individual flag added per registry for each component, (i.e. --show-deprecated-hidden-metrics-for-endpoint-1 and --show-deprecated-hidden-metrics-for-endpoint-2), rather a single flag for that component (i.e. --show-deprecated-hidden-metrics). It felt reasonable to trade the ability to configure individual registries with keeping the API surface area a bit simpler but provide a single option to configure all registries for that component, but that is definitely a subjective call. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d prefer not to introduce more global state and instead pass it into each registry as a parameter, which is not mutually exclusive with having a single flag to do so.

)

// 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 {
Expand Down
41 changes: 40 additions & 1 deletion staging/src/k8s.io/component-base/metrics/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}

}