Files
obsx/admin.go
T
M.ShakhmatovandClaude Fable 5.2 c92fd9826b
CI / test (push) Failing after 43s
Harden instrumentation, health checks and the admin server
- 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>
2026-11-15 16:56:39 +03:00

81 lines
2.7 KiB
Go

package obsx
import (
"io"
"log/slog"
"net/http"
"net/http/pprof"
"strings"
)
// AdminHandler returns the handler served by the admin server:
//
// GET /metrics Prometheus metrics
// GET /healthz, /livez liveness checks
// GET /readyz readiness checks
// GET /buildinfo service name, version, revision, uptime
// GET /loglevel current log level
// PUT /loglevel change log level: body or ?level= "debug", "info", ...
// /debug/pprof/ Go profiling (only with Admin.EnablePprof)
//
// The handler has no authentication: expose it on an internal address only.
func (o *Observer) AdminHandler() http.Handler {
mux := http.NewServeMux()
mux.Handle("GET /metrics", o.metrics.Handler())
mux.Handle("GET /healthz", o.health.LivenessHandler())
mux.Handle("GET /livez", o.health.LivenessHandler())
mux.Handle("GET /readyz", o.health.ReadinessHandler())
mux.HandleFunc("GET /buildinfo", func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, o.BuildInfo())
})
mux.Handle("/loglevel", LogLevelHandler(o.level, o.logger))
if o.cfg.Admin.EnablePprof {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
return mux
}
// LogLevelHandler serves the level held by lv: GET returns it, PUT or POST
// changes it from the "level" query parameter or the request body.
// Changes are logged with logger when it is non-nil.
func LogLevelHandler(lv *slog.LevelVar, logger *slog.Logger) http.Handler {
type response struct {
Level string `json:"level"`
Error string `json:"error,omitempty"`
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead:
writeJSON(w, http.StatusOK, response{Level: lv.Level().String()})
case http.MethodPut, http.MethodPost:
value := r.URL.Query().Get("level")
if value == "" {
body, _ := io.ReadAll(io.LimitReader(r.Body, 64))
value = strings.TrimSpace(string(body))
}
level, err := ParseLevel(value)
if err != nil {
writeJSON(w, http.StatusBadRequest, response{Level: lv.Level().String(), Error: err.Error()})
return
}
prev := lv.Level()
lv.Set(level)
if logger != nil {
logger.LogAttrs(r.Context(), slog.LevelInfo, "log level changed",
slog.String("from", prev.String()), slog.String("to", level.String()))
}
writeJSON(w, http.StatusOK, response{Level: level.String()})
default:
w.Header().Set("Allow", "GET, HEAD, PUT, POST")
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
})
}