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

use generic slis as entrypoint for healthcheck metrics #112740

Merged
merged 1 commit into from Sep 26, 2022
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
Expand Up @@ -14,14 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package health
package slis

import (
"context"
"errors"

k8smetrics "k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)

type HealthcheckStatus string
Expand Down Expand Up @@ -64,32 +63,28 @@ var (
)
statuses = []HealthcheckStatus{Success, Error, Pending}
statusSet = map[HealthcheckStatus]struct{}{Success: {}, Error: {}, Pending: {}}
checkSet = map[HealthcheckType]struct{}{Livez: {}, Readyz: {}, Healthz: {}}
)

func init() {
legacyregistry.MustRegister(healthcheck)
legacyregistry.MustRegister(healthchecksTotal)
func Register(registry k8smetrics.KubeRegistry) {
registry.Register(healthcheck)
registry.Register(healthchecksTotal)
}

func ResetHealthMetrics() {
healthcheck.Reset()
healthchecksTotal.Reset()
}

func ObserveHealthcheck(ctx context.Context, name string, healthcheckType HealthcheckType, status HealthcheckStatus) error {
func ObserveHealthcheck(ctx context.Context, name string, healthcheckType string, status HealthcheckStatus) error {
if _, ok := statusSet[status]; !ok {
return errors.New("not a valid healthcheck status")
}
if _, ok := checkSet[healthcheckType]; !ok {
return errors.New("not a valid healthcheck type")
}
for _, s := range statuses {
if status != s {
healthcheck.WithContext(ctx).WithLabelValues(name, string(healthcheckType), string(s)).Set(0)
healthcheck.WithContext(ctx).WithLabelValues(name, healthcheckType, string(s)).Set(0)
}
}
healthchecksTotal.WithContext(ctx).WithLabelValues(name, string(healthcheckType), string(status)).Inc()
healthcheck.WithContext(ctx).WithLabelValues(name, string(healthcheckType), string(status)).Set(1)
healthchecksTotal.WithContext(ctx).WithLabelValues(name, healthcheckType, string(status)).Inc()
healthcheck.WithContext(ctx).WithLabelValues(name, healthcheckType, string(status)).Set(1)
return nil
}
Expand Up @@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package health
package slis

import (
"context"
"strings"
"testing"

"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/testutil"
)

Expand All @@ -30,8 +30,10 @@ var (
)

func TestObserveHealthcheck(t *testing.T) {
defer legacyregistry.Reset()
registry := metrics.NewKubeRegistry()
defer registry.Reset()
defer ResetHealthMetrics()
Register(registry)
initialState := Error
healthcheckName := "healthcheck-a"
initialOutput := `
Expand All @@ -47,14 +49,14 @@ func TestObserveHealthcheck(t *testing.T) {
testCases := []struct {
desc string
name string
hcType HealthcheckType
hcType string
hcStatus HealthcheckStatus
want string
}{
{
desc: "test pending",
name: healthcheckName,
hcType: Healthz,
hcType: "healthz",
hcStatus: Pending,
want: `
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
Expand All @@ -71,7 +73,7 @@ func TestObserveHealthcheck(t *testing.T) {
{
desc: "test success",
name: healthcheckName,
hcType: Healthz,
hcType: "healthz",
hcStatus: Success,
want: `
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
Expand All @@ -95,15 +97,15 @@ func TestObserveHealthcheck(t *testing.T) {
if err != nil {
t.Errorf("unexpected err: %v", err)
}
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(initialOutput), testedMetrics...); err != nil {
if err := testutil.GatherAndCompare(registry, strings.NewReader(initialOutput), testedMetrics...); err != nil {
t.Fatal(err)
}
// now record that we successfully purge state
err = ObserveHealthcheck(context.Background(), test.name, test.hcType, test.hcStatus)
if err != nil {
t.Errorf("unexpected err: %v", err)
}
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(test.want), testedMetrics...); err != nil {
if err := testutil.GatherAndCompare(registry, strings.NewReader(test.want), testedMetrics...); err != nil {
t.Fatal(err)
}
})
Expand Down
@@ -0,0 +1,27 @@
/*
Copyright 2020 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 slis

import (
"k8s.io/component-base/metrics"
)

var (
// Registry exposes the SLI registry so that additional SLIs can be
// added on a per-component basis.
Registry = metrics.NewKubeRegistry()
)
@@ -0,0 +1,53 @@
/*
Copyright 2020 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 slis

import (
"net/http"
"sync"

"k8s.io/component-base/metrics"
)

var (
installOnce = sync.Once{}
installWithResetOnce = sync.Once{}
)

type mux interface {
Handle(path string, handler http.Handler)
}

type SLIMetrics struct{}

// Install adds the DefaultMetrics handler
func (s SLIMetrics) Install(m mux) {
installOnce.Do(func() {
Register(Registry)
m.Handle("/metrics/slis", metrics.HandlerFor(Registry, metrics.HandlerOpts{}))
})
}

type SLIMetricsWithReset struct{}

// Install adds the DefaultMetrics handler
func (s SLIMetricsWithReset) Install(m mux) {
installWithResetOnce.Do(func() {
Register(Registry)
m.Handle("/metrics/slis", metrics.HandlerWithReset(Registry, metrics.HandlerOpts{}))
})
}
6 changes: 6 additions & 0 deletions staging/src/k8s.io/component-base/metrics/wrappers.go
Expand Up @@ -145,6 +145,12 @@ type Gatherer interface {
prometheus.Gatherer
}

// Registerer is the interface for the part of a registry in charge of registering
// the collected metrics.
type Registerer interface {
prometheus.Registerer
}

// GaugeFunc is a Gauge whose value is determined at collect time by calling a
// provided function.
//
Expand Down