4.0 KiB
4.0 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 and OpenTelemetry tracing setup with consistent defaults and isolated registries.
Package map
obsx/ Root — tracing, metrics, config
├── doc.go Package documentation
├── tracer.go SetupTracer, StartSpan, TracerConfig
├── metrics.go Metrics factory, NewMetrics, Counter/Histogram/Gauge, Handler
└── config.go (empty — configs are co-located with their components)
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 - Insecure gRPC:
SetupTraceralways usesotlptracegrpc.WithInsecure()— no TLS for the OTLP endpoint - StartSpan tracer name: uses the hardcoded tracer name
"obsx"— all spans created via this helper share the same instrumentation scope
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