Instrumentation

The code that makes your app report on itself: the spans, metrics, and logs you add so there is anything to look at later.

wiring the app up to report thingsadding the tracking hooksinstrumenting the codeinstrumantationmake the app report metricsadding logging and metrics to my codethe code that emits telemetryhooking the app up to monitoring

See it

Live demo coming soon

What it is

Instrumentation is the emitting side of observability: the code that makes your app talk about itself. No instrumentation, no data, and no dashboard can conjure it after the fact. It comes in two flavors. Auto-instrumentation patches known libraries at startup (the HTTP server, fetch, the Postgres driver, Redis) and hands you a usable trace and a request-rate chart for roughly zero lines of your own code. Manual instrumentation is the part only you can write: a span around 'generate invoice PDF', a counter for 'coupon rejected', an attribute for plan tier.

The order that works: switch on auto-instrumentation first for cheap coverage, then add manual spans and attributes at the edges of your own business logic, one incident at a time. The best instrumentation gets written the day after an outage, when you remember exactly which field you wished you had. Prefer fewer, fatter spans carrying lots of context over a swarm of thin ones that say nothing.

Gotchas: auto-instrumentation usually has to load before everything else (a preload flag, or a require at the very top of the entrypoint), and quietly does nothing if a single import beats it. It also costs real CPU and real money at traffic, so sample. And instrumentation is the main way PII escapes into a vendor: request bodies, auth headers, and full URLs with query strings are captured by default in some agents, so redact at the collector rather than trusting each service.

Ask AI for it

Instrument this codebase with OpenTelemetry in three steps. One: load auto-instrumentation for this runtime from a file required before the app entrypoint, covering the HTTP server, outbound HTTP, and the database client. Two: add manual spans around the business operations in this repo that are not plain library calls, with attributes for the inputs worth filtering on (operation, plan tier, item count, retry count, cache hit). Three: add counters for the domain events worth alerting on and a histogram for the slowest operation. Redact authorization headers, cookies, and query strings before export, keep user identifiers out of attributes, and put the sampling rate behind an env var.

You might have meant

opentelemetrydistributed tracingspantelemetrysampling