Skip to main content
Spans are the building blocks of performance monitoring in Sentry. Each span represents a single operation or unit of work with a start and end time.

Span Basics

A span contains:
  • Span ID: Unique identifier for the span
  • Trace ID: Identifier linking it to a trace
  • Parent Span ID: Links to parent span (if any)
  • Operation: Type of operation (e.g., db.query, http.client)
  • Description/Name: What the span measures
  • Start/End Time: When the span began and ended
  • Status: Success or failure indicator
  • Attributes: Custom key-value data

Creating Spans

Auto-finishing Spans

Spans that finish when the callback completes:

Manual Span Control

Control span lifetime explicitly:

Async Operations

Spans work seamlessly with async/await:

Span Hierarchy

Parent-Child Relationships

Spans automatically form a hierarchy:

Root Spans (Transactions)

The top-level span in a trace is called a transaction:
Transactions are sent to Sentry when the root span finishes. Child spans are included in the transaction.

Span Attributes

Setting Attributes

Add custom data to spans:

Removing Attributes

Standard Attributes

Use semantic attributes for common data:

Span Status

Indicate whether a span succeeded or failed:

Status Codes

  • SPAN_STATUS_UNSET (0): Default, no status set
  • SPAN_STATUS_OK (1): Operation completed successfully
  • SPAN_STATUS_ERROR (2): Operation failed

HTTP Status Codes

Automatically set status from HTTP responses:

Span Events

Add timestamped events within a span:

Updating Span Names

Change span names dynamically:
Use dynamic span names to categorize similar operations based on their outcome or parameters.

Span Context

Access span identifiers:

Span Sampling

Control whether a span is recorded:

Getting Span Data

Retrieve span information:
Link spans to other traces:
Span links allow you to connect spans across different traces, useful for batch processing or fan-out operations.

Practical Examples

Database Query

HTTP Request

File Processing

Caching Operation

Span Best Practices

  1. Use descriptive names: Clearly indicate what the span measures
  2. Set appropriate operations: Use standard operation types
  3. Add relevant attributes: Include data that helps identify slow operations
  4. Set status correctly: Indicate success or failure
  5. Keep spans focused: Each span should represent one logical operation
  6. Don’t create too many spans: Balance detail with overhead
  7. Finish spans promptly: Don’t leave spans open

Span Limits

Be aware of limits:
  • Maximum 1000 spans per transaction
  • Attribute values are truncated if too large
  • Deeply nested spans may impact performance
Transactions with more than 1000 spans will have spans dropped. Keep your span count reasonable.

Next Steps

Tracing

Learn about traces and span relationships

Distributed Tracing

Track spans across services

Performance

Performance monitoring overview

Profiling

CPU profiling for code-level insights