Skip to main content
Spans represent units of work in your application and are the building blocks of distributed tracing.

Overview

Spans track:
  • Operation timing (start and end)
  • Operation metadata (name, description)
  • Attributes for filtering and analysis
  • Parent-child relationships
  • Status and outcome

Creating Spans

startSpan

Create an active span that automatically becomes the parent of nested spans.
StartSpanOptions
required
Configuration for the span.
string
required
The span description.
string
The span operation (e.g., 'http.client', 'db.query').
SpanAttributes
Additional span attributes.
SpanTimeInput
Custom start time.
Scope
Custom scope to use.
Span
Explicit parent span.
boolean
Only create span if there’s a parent span.
function
required
Function to execute within the span context. Receives the span as argument.
Example:

startInactiveSpan

Create an inactive span that doesn’t automatically become the active parent.

startSpanManual

Create a span with manual control over when it finishes.

Span Interface

From packages/core/src/types-hoist/span.ts:

spanContext()

Get context data for the span.
string
32-character hex string representing the trace.
string
16-character hex string representing the span.
number
Trace flags (1 = sampled, 0 = not sampled).

setAttribute()

Set a single attribute on the span.

setAttributes()

Set multiple attributes at once.

setStatus()

Set the span status.
Status Codes:
  • 0 - Unset
  • 1 - OK
  • 2 - Error

updateName()

Update the span name.
Note: Use Sentry.updateSpanName(span, name) to ensure the name persists through instrumentation.

end()

End the span.

isRecording()

Check if the span is recording.

Span Attributes

Semantic Attributes

Use semantic conventions for consistency: HTTP Attributes:
Database Attributes:
Custom Attributes:

Nested Spans

Create parent-child relationships:

Error Handling

Async Operations

Getting Active Span

Span JSON

Spans are serialized as:

Best Practices

1. Use Descriptive Names

2. Set Appropriate Operations

3. Add Relevant Attributes

4. Handle Errors Properly