Span structure
Every span contains:- Identity:
spanId,traceId,parentId - Timing:
startTime,endTime,durationMs - Context:
sessionId,sessionName - Metadata:
attributes,tags,traceTags,sessionTags - Data:
inputData,outputData - Status:
status(‘ok’ or ‘error’),errorobject - Signals:
signals(metrics and flags)
Creating spans
Using withSpan (recommended)
ThewithSpan helper provides automatic lifecycle management:
Using the @span decorator
Decorate methods to automatically create spans:Manual span management
For fine-grained control:Identifiers
Span ID
Each span has a unique UUID (spanId) generated automatically:
Trace ID
All spans in the same trace share atraceId:
Parent ID
Child spans reference their parent viaparentId:
Timing
Automatic timing
Spans automatically record start and end times:Duration calculation
ThedurationMs property is computed from start and end times:
Capturing input and output
Automatic capture with decorator
The@span decorator automatically captures function arguments and return values:
Manual capture
Data serialization
Input and output are automatically serialized to JSON:- String values are stored as-is
- Objects and arrays are stringified with
JSON.stringify() - BigInts are converted to strings
- Functions are represented as
[Function name]
Error handling
Automatic error capture
ThewithSpan helper automatically captures errors:
Manual error capture
Status field
Spans have astatus field that indicates success or failure:
Metadata
Attributes
Attributes are arbitrary key-value pairs for structured data:Tags
Tags are string key-value pairs used for filtering and grouping:Signals
Spans can include boolean, numerical, or string signals for metrics:Serialization
Spans are serialized to JSON when flushed to the backend:Best practices
Use withSpan for simple cases
Use withSpan for simple cases
The
withSpan helper handles lifecycle and errors automatically:Always end spans
Always end spans
If using manual span management, always end spans in a finally block:
Use attributes for structured data
Use attributes for structured data
Store queryable data in attributes, not tags:
Capture meaningful input/output
Capture meaningful input/output
Don’t capture excessive data: