Files
obsx/AGENTS.md
T
Aleksey ShakhmatovandClaude 2c83b28342
CI / test (push) Successful in 1m17s
Publish / publish (push) Successful in 1m24s
Release v1.0.0: HTTP instrumentation, slog correlation, TLS by default
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>
2026-11-10 20:20:09 +03:00

91 lines
4.4 KiB
Markdown

# 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)
1. Add a method to `Metrics` struct in `metrics.go`
2. Create the Prometheus collector with `m.cfg.Namespace` / `m.cfg.Subsystem`
3. Register via `m.registry.MustRegister()`
4. Return the collector
### Change the trace exporter (e.g., HTTP instead of gRPC)
1. Replace `otlptracegrpc.New` in `SetupTracer` with the desired exporter
2. Update `TracerConfig` fields if the new exporter needs different options
3. Keep the same return signature (`shutdown func(context.Context) error`)
### Add custom resource attributes to traces
1. Append additional `resource.WithAttributes(...)` options to the `attrs` slice in `SetupTracer`
2. Use `semconv` constants 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**: `NewMetrics` creates its own `prometheus.Registry`, not the global default. Metrics registered here will not appear in `promhttp.Handler()` — use `m.Handler()` instead
- **Sampler default**: if `TracerConfig.Sampler` is 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**: `StartSpan` and the HTTP instrumentation share the instrumentation scope `git.codelab.vc/pkg/obsx`
- **HTTPMiddleware registers metrics**: calling it twice with the same `Metrics` panics (duplicate registration); the route label comes from `Request.Pattern`, so it must wrap the `http.ServeMux` directly
- **LogHandler needs the context**: only `slog.*Context` calls get `trace_id` / `span_id`
## Commands
```bash
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 `Metrics` instance has its own registry
- **stdlib only** testing — no testify, no gomock
- **No functional options** — direct struct configuration
- **Shutdown function** — `SetupTracer` returns a shutdown callback, caller is responsible for calling it