span() decorator provides automatic tracing for class methods and functions. It captures arguments, return values, and errors without manual instrumentation.
Usage
You can usespan() in two ways:
- As a method decorator - Annotate class methods with
@span() - As a function wrapper - Wrap standalone functions
Method Decorator
Decorate class methods to automatically trace their execution:Function Wrapper
Wrap standalone functions for tracing:Type Signature
Parameters
Configuration options for the span
SpanOptions
The name of the span. Used to identify this operation in your traces.
Optional session identifier to group related spans together.
Optional human-readable name for the session.
String key-value pairs for categorizing and filtering spans.
Additional metadata to attach to the span. Accepts any JSON-serializable values.
Explicit input data to log. If not provided, function arguments are automatically captured and JSON-serialized.
Explicit output data to log. If not provided, the return value is automatically captured and JSON-serialized.
Automatic Capture
The decorator automatically captures:- Arguments - All function arguments are JSON-serialized (unless
inputDatais explicitly provided) - Return values - Function return values are JSON-serialized (unless
outputDatais explicitly provided) - Errors - Exceptions are captured with code, message, and stack trace
- Timing - Start and end timestamps for the span
Error Handling
When a decorated method throws an error, the span:- Captures the error details (name, message, stack)
- Marks the span as failed
- Ends the span
- Re-throws the original error
Async and Sync Support
The decorator works with both synchronous and asynchronous methods:Advanced Example
Combine multiple spans with explicit input/output control:When
inputData is not provided, all arguments are automatically serialized using JSON.stringify(). BigInts are converted to strings, and functions are represented as [Function name].Related
- withSpan() - Context-based tracing without decorators
- Tracer - Low-level span management