Loading...
Loading...
Implements liveness and readiness health check endpoints following Kubernetes probe conventions. Covers response schema, dependency checks, Kubernetes probe config, and circuit breaker integration. Invoked when the user asks to add health checks, implement a /health endpoint, or set up Kubernetes probes.
npx skill4agent add soulcodex/agentic health-check-endpointsGET /health/live// Go example
func livenessHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}200 OKGET /health/ready// Go example
func readinessHandler(w http.ResponseWriter, r *http.Request) {
checks := map[string]CheckResult{}
overall := "ok"
// Database check
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
if err := db.PingContext(ctx); err != nil {
checks["db"] = CheckResult{Status: "fail", Error: err.Error()}
overall = "fail"
} else {
start := time.Now()
db.PingContext(ctx)
checks["db"] = CheckResult{Status: "ok", LatencyMs: time.Since(start).Milliseconds()}
}
// Redis check
if err := cache.Ping(r.Context()).Err(); err != nil {
checks["cache"] = CheckResult{Status: "fail", Error: err.Error()}
overall = "fail"
} else {
checks["cache"] = CheckResult{Status: "ok"}
}
status := http.StatusOK
if overall == "fail" {
status = http.StatusServiceUnavailable
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(HealthResponse{Status: overall, Checks: checks})
}{
"status": "ok",
"checks": {
"db": {
"status": "ok",
"latency_ms": 2
},
"cache": {
"status": "ok",
"latency_ms": 1
},
"broker": {
"status": "fail",
"error": "connection refused"
}
}
}status"ok""degraded""fail"status"ok""fail"200okdegraded503fail# deployment.yaml
livenessProbe:
httpGet:
path: /health/live
port: 8080
initialDelaySeconds: 10 # give the app time to start
periodSeconds: 10
failureThreshold: 3 # restart after 3 consecutive failures
readinessProbe:
httpGet:
path: /health/ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
failureThreshold: 3 # remove from LB after 3 failures
successThreshold: 1 # re-add after 1 successinitialDelaySecondstimeoutSeconds"fail"if circuitBreaker.IsOpen("db") {
checks["db"] = CheckResult{Status: "fail", Error: "circuit open"}
overall = "fail"
}GET /health/live200 OKGET /health/ready503GET /health/ready200livenessProbereadinessProbetimeoutSeconds