Manually create and nest spans with the OTel SDK to trace internal business logic beyond framework auto-instrumentation

domain: opentelemetry.io · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Obtain a Tracer from the global TracerProvider using a library-scoped instrumentation name and version
  2. Call tracer.startSpan() with a descriptive operation name, setting SpanKind and initial attributes at start time
  3. Activate the span in context using context.with(trace.setSpan(context.active(), span)) so child work inherits it
  4. Record span events (span.addEvent) for significant intermediate steps and set attributes as the business logic executes
  5. Call span.setStatus(SpanStatusCode.ERROR, description) on exception paths, then record the exception with span.recordException(err)
  6. End the span in a finally block to guarantee it closes even on error paths

Known gotchas

Related routes

Capture and export browser errors and web vitals as OTel spans using the OTel JS SDK with custom instrumentation
opentelemetry.io · 6 steps · unrated
Use OTel span links to connect causally related spans across asynchronous message queue boundaries
opentelemetry.io · 5 steps · unrated
Generate span metrics from traces using the OTel Collector spanmetrics connector
opentelemetry.io · 5 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans