mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-20 10:20:51 +00:00
patch healthcheck sli metric so that we only have a binary value
Change-Id: I0d87e29715432f772309a0d4a7305fff358c6d48
This commit is contained in:
parent
db13f51db9
commit
334be489d4
@ -18,8 +18,6 @@ package slis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
k8smetrics "k8s.io/component-base/metrics"
|
||||
)
|
||||
|
||||
@ -28,17 +26,10 @@ type HealthcheckStatus string
|
||||
const (
|
||||
Success HealthcheckStatus = "success"
|
||||
Error HealthcheckStatus = "error"
|
||||
Pending HealthcheckStatus = "pending"
|
||||
)
|
||||
|
||||
type HealthcheckType string
|
||||
|
||||
const (
|
||||
Livez HealthcheckType = "livez"
|
||||
Readyz HealthcheckType = "readyz"
|
||||
Healthz HealthcheckType = "healthz"
|
||||
)
|
||||
|
||||
var (
|
||||
// healthcheck is a Prometheus Gauge metrics used for recording the results of a k8s healthcheck.
|
||||
healthcheck = k8smetrics.NewGaugeVec(
|
||||
@ -48,7 +39,7 @@ var (
|
||||
Help: "This metric records the result of a single healthcheck.",
|
||||
StabilityLevel: k8smetrics.ALPHA,
|
||||
},
|
||||
[]string{"name", "type", "status"},
|
||||
[]string{"name", "type"},
|
||||
)
|
||||
|
||||
// healthchecksTotal is a Prometheus Counter metrics used for counting the results of a k8s healthcheck.
|
||||
@ -61,8 +52,6 @@ var (
|
||||
},
|
||||
[]string{"name", "type", "status"},
|
||||
)
|
||||
statuses = []HealthcheckStatus{Success, Error, Pending}
|
||||
statusSet = map[HealthcheckStatus]struct{}{Success: {}, Error: {}, Pending: {}}
|
||||
)
|
||||
|
||||
func Register(registry k8smetrics.KubeRegistry) {
|
||||
@ -76,15 +65,12 @@ func ResetHealthMetrics() {
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
for _, s := range statuses {
|
||||
if status != s {
|
||||
healthcheck.WithContext(ctx).WithLabelValues(name, healthcheckType, string(s)).Set(0)
|
||||
}
|
||||
if status == Success {
|
||||
healthcheck.WithContext(ctx).WithLabelValues(name, healthcheckType).Set(1)
|
||||
} else {
|
||||
healthcheck.WithContext(ctx).WithLabelValues(name, healthcheckType).Set(0)
|
||||
}
|
||||
|
||||
healthchecksTotal.WithContext(ctx).WithLabelValues(name, healthcheckType, string(status)).Inc()
|
||||
healthcheck.WithContext(ctx).WithLabelValues(name, healthcheckType, string(status)).Set(1)
|
||||
return nil
|
||||
}
|
||||
|
@ -39,9 +39,7 @@ func TestObserveHealthcheck(t *testing.T) {
|
||||
initialOutput := `
|
||||
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
||||
# TYPE kubernetes_healthcheck gauge
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 1
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
|
||||
kubernetes_healthcheck{name="healthcheck-a",type="healthz"} 0
|
||||
# HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
||||
# TYPE kubernetes_healthchecks_total counter
|
||||
kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
||||
@ -53,23 +51,6 @@ func TestObserveHealthcheck(t *testing.T) {
|
||||
hcStatus HealthcheckStatus
|
||||
want string
|
||||
}{
|
||||
{
|
||||
desc: "test pending",
|
||||
name: healthcheckName,
|
||||
hcType: "healthz",
|
||||
hcStatus: Pending,
|
||||
want: `
|
||||
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
||||
# TYPE kubernetes_healthcheck gauge
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 1
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 0
|
||||
# HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
||||
# TYPE kubernetes_healthchecks_total counter
|
||||
kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
||||
kubernetes_healthchecks_total{name="healthcheck-a",status="pending",type="healthz"} 1
|
||||
`,
|
||||
},
|
||||
{
|
||||
desc: "test success",
|
||||
name: healthcheckName,
|
||||
@ -78,9 +59,7 @@ func TestObserveHealthcheck(t *testing.T) {
|
||||
want: `
|
||||
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck.
|
||||
# TYPE kubernetes_healthcheck gauge
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="error",type="healthz"} 0
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="pending",type="healthz"} 0
|
||||
kubernetes_healthcheck{name="healthcheck-a",status="success",type="healthz"} 1
|
||||
kubernetes_healthcheck{name="healthcheck-a",type="healthz"} 1
|
||||
# HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck.
|
||||
# TYPE kubernetes_healthchecks_total counter
|
||||
kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
|
||||
|
Loading…
Reference in New Issue
Block a user