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

+18 -8
View File
@@ -56,7 +56,7 @@ A runnable version lives in [`examples/service`](examples/service/main.go).
| **Traces** | OpenTelemetry provider with OTLP gRPC or HTTP export, parent-based ratio sampling, W3C TraceContext + Baggage propagation, resource with service/host/instance attributes. Without an endpoint, spans are still created so trace IDs appear in logs. |
| **Metrics** | Isolated Prometheus registry with Go runtime and process collectors, `service_build_info`, trace exemplars on latency histograms. |
| **Health** | Liveness and readiness checks run concurrently with timeouts; HTTP (`/healthz`, `/readyz`) and gRPC (`grpc.health.v1`) servers. |
| **Admin server** | `/metrics`, `/healthz`, `/livez`, `/readyz`, `/buildinfo`, `/loglevel`, `/debug/pprof/` on an internal port. |
| **Admin server** | `/metrics`, `/healthz`, `/livez`, `/readyz`, `/buildinfo`, `/loglevel` and, with `Admin.EnablePprof`, `/debug/pprof/`. No authentication: bind it to an internal address. |
| Instrumentation | Usage |
|-----------------|-------|
@@ -96,12 +96,15 @@ Cardinality is bounded: `route` is the router template (`/orders/{id}`), never t
**Routes.** With `http.ServeMux` (Go 1.22+ patterns) routes are detected automatically. For other routers set `RouteFunc`:
```go
obsx.NewHTTPMiddleware(obsx.HTTPServerConfig{
Metrics: o.Metrics(),
RouteFunc: func(r *http.Request) string {
return chi.RouteContext(r.Context()).RoutePattern()
obsx.Config{
HTTP: obsx.HTTPServerConfig{
RouteFunc: func(r *http.Request) string {
return chi.RouteContext(r.Context()).RoutePattern()
},
// Long-lived connections would distort latency histograms and alerts.
Skip: func(r *http.Request) bool { return r.URL.Path == "/ws" },
},
})
}
```
## Dashboards and alerts
@@ -123,11 +126,13 @@ Scrape the admin port (`:9090/metrics`) of every pod.
| `ServiceVersion` | main module version from build info |
| `Logging.Level` / `Format` / `Output` | `info` / `json` / stdout |
| `Tracing.Endpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT`; if unset, traces are not exported |
| `Tracing.Protocol` | `grpc` (`http` for OTLP/HTTP) |
| `Tracing.Protocol` | `OTEL_EXPORTER_OTLP_PROTOCOL`, then `grpc` (`http` for OTLP/HTTP) |
| `Tracing.Insecure` / `TLS` | TLS with system roots; `Insecure: true` for plaintext collectors, `TLS` for a custom `tls.Config` |
| `Tracing.Sampler` | `1.0`, parent-based |
| `Health.Timeout` | `5s` per check |
| `Admin.Addr` | empty — no server; mount `o.AdminHandler()` yourself |
| `Admin.EnablePprof` | `false` |
| `HTTP`, `HTTPClient`, `GRPC` | instrumentation settings used by the Observer: `RouteFunc`, `Skip`, `Buckets`, `Propagator`, `HostFunc` |
`Config.WithEnv()` lets deployments override settings without code changes:
@@ -143,7 +148,7 @@ Scrape the admin port (`:9090/metrics`) of every pod.
| `OBSX_TRACING_DISABLED`, `OTEL_SDK_DISABLED` | `Tracing.Disabled` |
| `OBSX_METRICS_NAMESPACE` | `Metrics.Namespace` |
| `OBSX_HEALTH_TIMEOUT` | `Health.Timeout` |
| `OBSX_ADMIN_ADDR`, `OBSX_ADMIN_DISABLE_PPROF` | `Admin.Addr`, `Admin.DisablePprof` |
| `OBSX_ADMIN_ADDR`, `OBSX_ADMIN_ENABLE_PPROF` | `Admin.Addr`, `Admin.EnablePprof` |
| `OBSX_DISABLE_ACCESS_LOG` | `DisableAccessLog` |
Standard `OTEL_EXPORTER_OTLP_*` and `OTEL_RESOURCE_ATTRIBUTES` are honored as well.
@@ -197,6 +202,11 @@ The v1.0.0 helpers `HTTPMiddleware(metrics)`, `HTTPTransport(base)` and `LogHand
- Creating an already registered metric with the same name, help and labels returns the existing collector instead of panicking.
- gRPC health-check calls are not instrumented by `Observer.GRPCInterceptors` to keep probe noise out of metrics and logs.
- HTTP client latency is measured until response headers arrive.
- Client spans record the URL without credentials and without the query string. The `host` label is the URL host; when hosts come from users set `HTTPClient.HostFunc` to keep cardinality bounded.
- Sampling follows the caller's trace headers. On endpoints reachable by untrusted clients set `HTTP.Propagator` / `GRPC.Propagator` to `propagation.NewCompositeTextMapPropagator()` so they cannot force sampling.
- Health results are reused for up to one second; a check that outlives its timeout is not started again until it returns.
- `GaugeFunc`, `CounterFunc` and `RegisterDBStats` panic on a duplicate name instead of silently keeping the first function or pool.
- A panic after a partial HTTP response is counted as 500.
## Requirements