Skip to main content
The SDK re-exports the manual-span surface of the OpenTelemetry API: trace, context, SpanKind, SpanStatusCode, and the Span, SpanContext, Attributes, and Tracer types.
Import these from @sherlock-labs/otel, never from @opentelemetry/api. The SDK re-exports the same objects, so your app and the SDK share one copy. With two copies in one process, one of them holds a provider the other cannot see.

When to add a manual span

Auto-instrumentation creates spans for libraries, not for units of work. Add a manual span for:
  • A queue job or a cron tick. There is no incoming request, so nothing opens a root span. Without one, every database and Redis span the job makes is an orphan.
  • A database client with no instrumentation. Wrap each call in a CLIENT span.
  • A unit of work you want to see by name inside a request.
Do not add a manual span around a route the HTTP instrumentation already traces. That makes a duplicate span with the same timing. Set attributes on the active span instead:

A withSpan helper

The SDK does not ship a span wrapper yet. Copy this one. It sets the attributes, runs the function with the span active, sets the status, records an exception on throw, and always ends the span.

A root span per job

startActiveSpan puts the span in async-local storage. Every patched library call inside the handler parents to it. That one span turns the fragments into a waterfall.

A CLIENT span for a database call

Record a custom histogram inside this span and its exemplar links to the query. See Custom metrics and exemplars.

Sampling and the kill switch

  • tracing.sampleRatio is parent-based. A request is traced whole or not at all. A manual span inside an unsampled request is not recorded.
  • A root span you open yourself takes a fresh sampling decision.
  • When traces are off through setTelemetryEnabled or SHERLOCK_TRACES_ENABLED=false, new spans are not recorded. The API calls still succeed, so your code does not need to check.

Custom metrics

A histogram recorded inside a span links its exemplar to that span.

Traces in Sherlock

Find a trace and read the waterfall.