Add Observer as a single entry point

obsx.New wires logger, tracer provider, metrics, health checks and the
admin server (/metrics, /healthz, /readyz, /buildinfo, /loglevel,
pprof). Adds Track for arbitrary operations, Config.WithEnv overrides,
godoc examples and a runnable example service.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
M.ShakhmatovandClaude Opus 5.5 committed 2026-10-30 00:18:43 +03:00
1 parent e8c211e93f
commit f7c8186040
6 files changed
+1092

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
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 (unless Admin.DisablePprof)
//
// Expose it on an internal port 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.DisablePprof {
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)
}
})
}