Skip to main content

Overview

While RDK auto-instruments LLM calls, you may want to trace other operations — database queries, API calls, or business logic. Use the span() context manager to create custom spans.

Creating Manual Spans

The 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 to s.metadata inside the block to record data discovered during execution:

Outside a Trace

If span() 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

Only create manual spans for operations you actually want to trace. Too many spans can make traces hard to read.
  1. Use descriptive namesdatabase.query.customers not db1
  2. Include relevant input — But avoid sensitive data
  3. Set appropriate types — Helps with filtering and visualization
  4. Don’t over-instrument — Auto-instrumented LLM calls don’t need manual spans

See Also