Update documentation for the observability toolkit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
M.ShakhmatovandClaude Opus 5.5 committed 2026-11-15 16:40:52 +03:00
1 parent 2bd9def584
commit b53a6a6c30
7 files changed
+315 -198

No files matched your search

+20 -22
View File
@@ -8,39 +8,37 @@ go test ./... # all tests
go test -race ./... # tests with race detector
go test -v -run TestName ./... # single test
go vet ./... # static analysis
go run ./examples/service # example service (:8080, admin :9090)
```
## Architecture
- **Module**: `git.codelab.vc/pkg/obsx`, Go 1.26, depends on OpenTelemetry and Prometheus client_golang
- **Single package** `obsx`
- **Module**: `git.codelab.vc/pkg/obsx`, Go 1.26, depends on OpenTelemetry, Prometheus client_golang and gRPC-Go
- **Single package** `obsx` (+ `examples/service`, `deploy/` with Grafana dashboard and Prometheus alerts)
### Core patterns
- **SetupTracer** — creates OTLP gRPC exporter (TLS unless `Insecure`), builds `sdktrace.TracerProvider` with batcher and parent-based ratio sampler, sets it and W3C propagators as OpenTelemetry globals
- **StartSpan** — convenience function starting spans via the global provider
- **TracerConfig** — service name, version, OTLP endpoint, insecure, headers, sampling ratio, resource attributes; `defaults()` sets `Sampler` to 1.0 if unset
- **HTTPMiddleware** — server span + `http_requests_total` / `http_request_duration_seconds` per request; route label from `Request.Pattern`
- **HTTPTransport** — `http.RoundTripper` with client spans and trace context injection
- **LogHandler** — `slog.Handler` wrapper adding `trace_id` / `span_id`
- **Metrics** — factory that creates Prometheus `CounterVec`, `HistogramVec`, `GaugeVec` with consistent namespace/subsystem
- **NewMetrics** — creates an isolated `prometheus.Registry` pre-loaded with process and Go collectors
- **Handler()** — returns `promhttp.HandlerFor` using the isolated registry
- **Registry()** — exposes the underlying `*prometheus.Registry` for direct access
### Tracer lifecycle
- `SetupTracer` returns `shutdown func(context.Context) error` — must be called to flush pending spans
- Shutdown is the `TracerProvider.Shutdown` method directly
- **New / Observer** — one call wires logger, tracer provider, metrics, health and optional admin server; `Shutdown` flushes spans and stops the admin server
- **Config** — service identity + per-component configs; `defaults()` fills service name/version and propagates identity to `Tracing`; `WithEnv()` applies `OBSX_*` / `OTEL_*` overrides
- **NewLogger / TraceHandler** — slog JSON/text logger; `TraceHandler` adds `trace_id`/`span_id` from context
- **SetupTracer / newTracerProvider** — OTLP gRPC or HTTP exporter (TLS unless `Insecure`; none when no endpoint), `ParentBased(TraceIDRatioBased)` sampler, resource with service/host attrs; sets global provider + TraceContext/Baggage propagator
- **HTTPMiddleware / HTTPTransport / LogHandler** — the v1.0.0 API (`http.go`, `log.go`), kept for compatibility: metrics `http_requests_total` / `http_request_duration_seconds` under the namespace
- **StartSpan / Trace / RecordError / TraceID** — span helpers on the global provider
- **Metrics** — Prometheus factory with namespace/subsystem, idempotent `register`, `Sub`, `GaugeFunc`, `CounterFunc`, `RegisterDBStats`; `base()` gives a namespace-free factory for built-in metrics; `observe` attaches trace exemplars
- **NewHTTPMiddleware / NewHTTPTransport / NewGRPCInterceptors** — server/client instrumentation: spans, propagation, RED metrics, access logs
- **Health** — liveness/readiness checks with timeouts, HTTP handlers, `grpc.health.v1` server, `SetReady`
- **AdminHandler** — `/metrics`, `/healthz`, `/livez`, `/readyz`, `/buildinfo`, `/loglevel`, `/debug/pprof/`
## Conventions
- Struct-based configs (`TracerConfig`, `MetricsConfig`) with `defaults()` methods
- Isolated Prometheus registry — not the global default, avoids conflicts in tests and multi-tenant setups
- `nil` buckets in `Histogram()` defaults to `prometheus.DefBuckets`
- No functional options — direct struct configuration only
- Struct-based configs with `defaults()` methods; no functional options
- Public API is stable since v1.0.0 — no breaking changes; record changes in `CHANGELOG.md`
- Built-in metric names are fixed (no namespace) so dashboards/alerts in `deploy/` work for every service — update `deploy/`, README and doc.go when adding or renaming them
- Isolated Prometheus registry — not the global default
- `nil` buckets in `Histogram()` default to `prometheus.DefBuckets`
- Bounded label cardinality: route templates, `OTHER` for unknown methods, `unmatched` for unknown routes
- stdlib-only tests; restore otel/slog globals in tests that call `New` or `SetupTracer`
## See also
- `AGENTS.md` — universal AI agent guide with package map and common tasks
- `AGENTS.md` — package map, common tasks, gotchas