Overview
While RDK auto-instruments LLM calls, you may want to trace other operations — database queries, API calls, or business logic. Use thespan() context manager to create custom spans.
Creating Manual Spans
span() context manager handles timing and status automatically:
- On normal exit: span is marked
SUCCESS - On exception: span is marked
ERROR, then the exception re-raises
Span Types
Choose the type that best describes the operation:Complete Example
Multiple manual spans in one function:Attaching Data During Execution
Write tos.metadata inside the block to record data discovered during execution:
Outside a Trace
Ifspan() is called outside an active trace context, it yields a no-op dummy span. No error is raised, and no data is sent. This makes it safe to call span() in library code that may or may not be traced by the caller.
Best Practices
- Use descriptive names —
database.query.customersnotdb1 - Include relevant input — But avoid sensitive data
- Set appropriate types — Helps with filtering and visualization
- Don’t over-instrument — Auto-instrumented LLM calls don’t need manual spans
See Also
- span() — Full API reference
- SpanType / SpanStatus — Enum values

