Add HTTPMiddleware, HTTPTransport and LogHandler; extend TracerConfig with Insecure, Headers and Attributes; install W3C propagators; parent-based sampling; end-to-end OTLP export tests; update dependencies (Go 1.26). Breaking since v0.2.0: the OTLP connection uses TLS unless Insecure is set. Co-Authored-By: Claude <noreply@anthropic.com>
4.4 KiB
4.4 KiB
AGENTS.md — obsx
Universal guide for AI coding agents working with this codebase.
Overview
git.codelab.vc/pkg/obsx is a Go observability library providing Prometheus metrics, OpenTelemetry tracing, trace-correlated slog logging and HTTP instrumentation with consistent defaults and isolated registries. The public API is stable (v1): do not break it.
Package map
obsx/ Root — single package
├── doc.go Package documentation
├── tracer.go SetupTracer, StartSpan, TracerConfig
├── metrics.go Metrics factory, NewMetrics, Counter/Histogram/Gauge, Handler
├── http.go HTTPMiddleware (server), HTTPTransport (client)
└── log.go LogHandler (slog trace correlation)
Architecture
┌──────────────────────┐
│ obsx │
└──────────┬───────────┘
│
┌───────────────┴───────────────┐
│ │
┌──────┴──────┐ ┌──────┴──────┐
│ Tracing │ │ Metrics │
└──────┬──────┘ └──────┬──────┘
│ │
SetupTracer() NewMetrics()
│ │
▼ ▼
OTLP gRPC exporter prometheus.Registry
TracerProvider (process + Go collectors)
(global via otel.Set) │
│ Counter / Histogram / Gauge
▼ │
StartSpan() Handler() → /metrics
Common tasks
Add a new metric type (e.g., Summary)
- Add a method to
Metricsstruct inmetrics.go - Create the Prometheus collector with
m.cfg.Namespace/m.cfg.Subsystem - Register via
m.registry.MustRegister() - Return the collector
Change the trace exporter (e.g., HTTP instead of gRPC)
- Replace
otlptracegrpc.NewinSetupTracerwith the desired exporter - Update
TracerConfigfields if the new exporter needs different options - Keep the same return signature (
shutdown func(context.Context) error)
Add custom resource attributes to traces
- Append additional
resource.WithAttributes(...)options to theattrsslice inSetupTracer - Use
semconvconstants where possible
Gotchas
- SetupTracer sets the global provider: calling it multiple times overwrites the previous provider; the old provider is not shut down automatically
- Isolated registry:
NewMetricscreates its ownprometheus.Registry, not the global default. Metrics registered here will not appear inpromhttp.Handler()— usem.Handler()instead - Sampler default: if
TracerConfig.Sampleris 0 or negative, it defaults to 1.0 (sample everything), not 0 - TLS by default: the OTLP connection is plaintext only with
TracerConfig.Insecure: true - Tracer name:
StartSpanand the HTTP instrumentation share the instrumentation scopegit.codelab.vc/pkg/obsx - HTTPMiddleware registers metrics: calling it twice with the same
Metricspanics (duplicate registration); the route label comes fromRequest.Pattern, so it must wrap thehttp.ServeMuxdirectly - LogHandler needs the context: only
slog.*Contextcalls gettrace_id/span_id
Commands
go build ./... # compile
go test ./... # all tests
go test -race ./... # tests with race detector
go test -v -run TestName ./... # single test
go vet ./... # static analysis
Conventions
- Struct-based Config with
defaults()method for zero-value defaults - Isolated Prometheus registry — each
Metricsinstance has its own registry - stdlib only testing — no testify, no gomock
- No functional options — direct struct configuration
- Shutdown function —
SetupTracerreturns a shutdown callback, caller is responsible for calling it