How-To: Trace code execution with Ragbits#
Each component of Ragbits includes built-in tracing, enabling users to collect detailed telemetry data without additional configuration. These traces provide visibility into execution flow, performance characteristics, and potential bottlenecks.
Default tracing#
By default, the SDK traces the following:
- LLM generation and streaming
- Embedder calls for text and image embeddings
- Sources data fetching
- Vector Store operations - retrieve, store, remove and list
- Document Search operations - search and ingest
Tracing your own code#
The main component of the tracing system is the trace. Traces need to be started and finished. You can create a trace in two ways:
-
Using the
trace()
context manager. -
Using the
@traceable
decorator.
The current trace is tracked via a Python contextvar
. This means that it works with concurrency automatically. During runtime traces are populated to the configured trace handleres defined via set_trace_handlers
.
Using CLI tracer#
To print traces locally in the CLI, configure CLITraceHandler
. You can enable the CLI tracer in a few ways:
- Setting the environment variable
RAGBITS_VERBOSE=1
- Using the
--verbose
flag in the Ragbits CLIuv run ragbits --verbose ...
- Using the
set_trace_handlers("cli")
function explicitly in your script
Using OpenTelemetry tracer#
To export traces to the OpenTelemetry collector, configure the provider and exporter, and set up the OtelTraceHandler
using the set_trace_handlers
method.
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from ragbits.core.audit import set_trace_handlers
resource = Resource(attributes={SERVICE_NAME: "ragbits-example"})
span_exporter = OTLPSpanExporter("http://localhost:4317", insecure=True)
tracer_provider = TracerProvider(resource=resource)
tracer_provider.add_span_processor(BatchSpanProcessor(span_exporter, max_export_batch_size=1))
trace.set_tracer_provider(tracer_provider)
set_trace_handlers("otel")
Info
This code snippet exports traces to the local OpenTelemetry collector running at http://localhost:4317. To visualize traces from Ragbits, open a browser and navigate to the Grafana dashboard at http://localhost:3000.
A full example along with a detailed installation guide is available here
.