Skip to content

Commit

Permalink
chore(general): add healthz-related metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzhhb authored and waynepeking348 committed May 6, 2024
1 parent ed74702 commit 17dfadc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
10 changes: 2 additions & 8 deletions cmd/base/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func NewGenericContext(
Handler: httpHandler.WithHandleChain(mux),
Addr: genericConf.GenericEndpoint,
},
healthChecker: NewHealthzChecker(),
healthChecker: NewHealthzChecker(customMetricsEmitterPool.GetDefaultMetricsEmitter()),
DisabledByDefault: disabledByDefault,
MetaInformerFactory: metaInformerFactory,
KubeInformerFactory: kubeInformerFactory,
Expand Down Expand Up @@ -266,14 +266,8 @@ func (c *GenericContext) StartInformer(ctx context.Context) {
// serveHealthZHTTP is used to provide health check for current running components.
func (c *GenericContext) serveHealthZHTTP(mux *http.ServeMux, enableHealthzCheck bool) {
mux.HandleFunc(healthZPath, func(w http.ResponseWriter, r *http.Request) {
if !enableHealthzCheck {
w.WriteHeader(200)
_, _ = w.Write([]byte("healthz check is disabled"))
return
}

ok, content := c.healthChecker.CheckHealthy()
if ok {
if ok || !enableHealthzCheck {
w.WriteHeader(200)
_, _ = w.Write([]byte(content))
} else {
Expand Down
24 changes: 22 additions & 2 deletions cmd/base/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,45 @@ package katalyst_base
import (
"context"
"encoding/json"
"time"

"go.uber.org/atomic"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/general"
)

const (
syncPeriod = 30 * time.Second
MetricNameUnhealthyRule = "unhealthy_healthz_check_rule"
)

// HealthzChecker periodically checks the running states
type HealthzChecker struct {
// if unhealthyReason is none-empty, it means some check failed
unhealthyReason *atomic.String
emitter metrics.MetricEmitter
}

func NewHealthzChecker() *HealthzChecker {
func NewHealthzChecker(emitter metrics.MetricEmitter) *HealthzChecker {
return &HealthzChecker{
unhealthyReason: atomic.NewString(""),
emitter: emitter,
}
}

func (h *HealthzChecker) Run(_ context.Context) {}
func (h *HealthzChecker) Run(ctx context.Context) {
go wait.Until(func() {
results := general.GetRegisterReadinessCheckResult()
for key, result := range results {
if !result.Ready {
_ = h.emitter.StoreInt64(MetricNameUnhealthyRule, 1, metrics.MetricTypeNameRaw,
metrics.MetricTag{Key: "rule", Val: string(key)})
}
}
}, syncPeriod, ctx.Done())
}

// CheckHealthy returns whether the component is healthy.
func (h *HealthzChecker) CheckHealthy() (bool, string) {
Expand Down

0 comments on commit 17dfadc

Please sign in to comment.