Harden instrumentation, health checks and the admin server
CI / test (push) Failing after 43s

- Admin pprof is opt-in (AdminConfig.EnablePprof)
- Client spans drop the URL query string; HostFunc bounds the host label
- Config.HTTP/HTTPClient/GRPC configure the Observer's instrumentation
- A panic after a partial HTTP response counts as 500
- GaugeFunc, CounterFunc and RegisterDBStats panic on duplicates
- Abandoned client gRPC streams are recorded as Canceled
- OTEL_EXPORTER_OTLP_PROTOCOL selects the protocol when unset
- Health reports are cached for 1s; hung checks are not restarted
- Environment is exported as deployment.environment.name

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
M.ShakhmatovandClaude Fable 5.2 committed 2026-11-15 16:56:39 +03:00
1 parent 8647fdf1bb
commit c92fd9826b
21 files changed
+457 -101

No files matched your search

+47 -11
View File
@@ -41,6 +41,23 @@ func (c *HealthConfig) defaults() {
type namedCheck struct {
name string
fn CheckFunc
busy *atomic.Bool // a run that outlived its timeout is still in progress
}
// healthCacheTTL bounds how often checks hit their dependencies, however
// often the probes (which may be reachable by clients) are called.
const healthCacheTTL = time.Second
type reportCache struct {
mu sync.Mutex // also serializes runs
at time.Time
report Report
}
func (c *reportCache) reset() {
c.mu.Lock()
c.at = time.Time{}
c.mu.Unlock()
}
// Health runs liveness and readiness checks and serves them over HTTP
@@ -57,6 +74,8 @@ type Health struct {
liveness []namedCheck
ready []namedCheck
livenessCache, readyCache reportCache
notReady atomic.Bool
}
@@ -74,15 +93,17 @@ func NewHealth(cfg HealthConfig) *Health {
// AddLivenessCheck registers a check that affects the liveness probe.
func (h *Health) AddLivenessCheck(name string, fn CheckFunc) {
h.mu.Lock()
defer h.mu.Unlock()
h.liveness = append(h.liveness, namedCheck{name: name, fn: fn})
h.liveness = append(h.liveness, namedCheck{name: name, fn: fn, busy: new(atomic.Bool)})
h.mu.Unlock()
h.livenessCache.reset()
}
// AddReadinessCheck registers a check that affects the readiness probe.
func (h *Health) AddReadinessCheck(name string, fn CheckFunc) {
h.mu.Lock()
defer h.mu.Unlock()
h.ready = append(h.ready, namedCheck{name: name, fn: fn})
h.ready = append(h.ready, namedCheck{name: name, fn: fn, busy: new(atomic.Bool)})
h.mu.Unlock()
h.readyCache.reset()
}
// SetReady overrides readiness. Call SetReady(false) during startup warm-up or
@@ -109,12 +130,13 @@ type CheckResult struct {
// Healthy reports whether all checks passed.
func (r Report) Healthy() bool { return r.Status == StatusOK }
// CheckLiveness runs all liveness checks concurrently.
// CheckLiveness runs all liveness checks concurrently. Results are reused for
// up to a second, so frequent probes do not hammer the checked dependencies.
func (h *Health) CheckLiveness(ctx context.Context) Report {
h.mu.RLock()
checks := h.liveness
h.mu.RUnlock()
return h.run(ctx, "liveness", checks)
return h.run(ctx, "liveness", checks, &h.livenessCache)
}
// CheckReadiness runs all readiness checks concurrently. It fails immediately
@@ -126,22 +148,28 @@ func (h *Health) CheckReadiness(ctx context.Context) Report {
h.mu.RLock()
checks := h.ready
h.mu.RUnlock()
return h.run(ctx, "readiness", checks)
return h.run(ctx, "readiness", checks, &h.readyCache)
}
func (h *Health) run(ctx context.Context, kind string, checks []namedCheck) Report {
func (h *Health) run(ctx context.Context, kind string, checks []namedCheck, cache *reportCache) Report {
report := Report{Status: StatusOK}
if len(checks) == 0 {
return report
}
cache.mu.Lock()
defer cache.mu.Unlock()
if !cache.at.IsZero() && time.Since(cache.at) < healthCacheTTL {
return cache.report
}
results := make([]CheckResult, len(checks))
var wg sync.WaitGroup
for i, c := range checks {
wg.Add(1)
go func() {
defer wg.Done()
results[i] = h.runOne(ctx, c.fn)
results[i] = h.runOne(ctx, c)
}()
}
wg.Wait()
@@ -159,22 +187,30 @@ func (h *Health) run(ctx context.Context, kind string, checks []namedCheck) Repo
h.status.WithLabelValues(c.name, kind).Set(healthy)
}
}
if ctx.Err() == nil { // a cancelled caller must not fail the probe for everyone
cache.at, cache.report = time.Now(), report
}
return report
}
func (h *Health) runOne(ctx context.Context, fn CheckFunc) CheckResult {
func (h *Health) runOne(ctx context.Context, c namedCheck) CheckResult {
// A check that ignores ctx must not pile up one goroutine per probe.
if !c.busy.CompareAndSwap(false, true) {
return CheckResult{Status: StatusFail, Error: "previous run is still in progress", Duration: "0s"}
}
ctx, cancel := context.WithTimeout(ctx, h.timeout)
defer cancel()
start := time.Now()
done := make(chan error, 1)
go func() {
defer c.busy.Store(false)
defer func() {
if rec := recover(); rec != nil {
done <- fmt.Errorf("panic: %v", rec)
}
}()
done <- fn(ctx)
done <- c.fn(ctx)
}()
var err error