Skip to main content
The auto-instrumentation gives you per-tool spans for free. For everything else — cache lookups, upstream HTTP calls, business outcomes — use the helpers on the ObservabilityInstance returned by instrumentServer.

Setup once, reuse everywhere

Create your instruments once at module scope so you’re not reallocating histograms or counters on the hot path.
instruments.ts
Then import where you need them:

Pattern: time + count an operation

The new span becomes a child of the auto-generated tools/call <toolName> span if you call this from inside a tool handler.

Pattern: record a histogram with tags

Pattern: business outcomes

Counters are great for outcome tracking even when nothing is timed:

Pattern: distributed trace context

If a tool calls an upstream HTTP service, propagate the active context so the upstream’s spans hang under your trace:
The upstream service (if it’s also instrumented with OTel) will continue your trace.

Pattern: feature-flag the SDK

If you want to gate instrumentation:

Pattern: async iterators

If your tool streams events, record one span per batch + a counter per event:
For a span-per-event you’d lose more in overhead than you’d gain in visibility — counters are usually the right shape for high-frequency events.

Pattern: shut down on signal

See also

Automatic metrics

What the package records without any code.

Data processors

Mutate every attribute set in one place.