Update documentation for the observability toolkit
Publish / publish (push) Successful in 1m21s

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 3acf207727
commit d1fc35fdf0
4 files changed
+300 -143

No files matched your search

+19 -19
View File
@@ -8,35 +8,35 @@ 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.25.7, depends on OpenTelemetry and Prometheus client_golang
- **Single package** `obsx`
- **Module**: `git.codelab.vc/pkg/obsx`, Go 1.25.7, 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, builds `sdktrace.TracerProvider` with batcher and ratio sampler, sets it as global `otel.TracerProvider`
- **StartSpan** — convenience function using `otel.Tracer("obsx")` to start spans via the global provider
- **TracerConfig** — service name, version, OTLP endpoint, sampling ratio; `defaults()` sets `Sampler` to 1.0 if unset
- **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 (none when no endpoint), `ParentBased(TraceIDRatioBased)` sampler, resource with service/host attrs; sets global provider + TraceContext/Baggage propagator
- **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
- 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