Custom Tracer
Learn how to connect Haystack to a custom tracing backend by implementing the Tracer interface.
| Base classes | Tracer and Span |
| How to enable | Implement the Tracer interface, then tracing.enable_tracing(your_tracer) |
| Content tracing | Optional. Set HAYSTACK_CONTENT_TRACING_ENABLED to true to trace component inputs and outputs |
| Package | Built into Haystack |
| GitHub link | https://github.com/deepset-ai/haystack/blob/main/haystack/tracing/tracer.py |
Overview
If your tracing backend isn't supported out of the box, you can connect it to Haystack by implementing the Tracer interface. This gives you full control over how spans are created and how tags are recorded.
Usage
-
Implement the
Tracerinterface. The following code snippet provides an example using the OpenTelemetry package:pythonimport contextlibfrom typing import Optional, Dict, Any, Iteratorfrom opentelemetry import tracefrom opentelemetry.trace import NonRecordingSpanfrom haystack.tracing import Tracer, Spanfrom haystack.tracing import utils as tracing_utilsimport opentelemetry.traceclass OpenTelemetrySpan(Span):def __init__(self, span: opentelemetry.trace.Span) -> None:self._span = spandef set_tag(self, key: str, value: Any) -> None:# Tracing backends usually don't support any tag value# `coerce_tag_value` forces the value to either be a Python# primitive (int, float, boolean, str) or tries to dump it as string.coerced_value = tracing_utils.coerce_tag_value(value)self._span.set_attribute(key, coerced_value)class OpenTelemetryTracer(Tracer):def __init__(self, tracer: opentelemetry.trace.Tracer) -> None:self._tracer = tracer@contextlib.contextmanagerdef trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> Iterator[Span]:with self._tracer.start_as_current_span(operation_name) as span:span = OpenTelemetrySpan(span)if tags:span.set_tags(tags)yield spandef current_span(self) -> Optional[Span]:current_span = trace.get_current_span()if isinstance(current_span, NonRecordingSpan):return Nonereturn OpenTelemetrySpan(current_span) -
Tell Haystack to use your custom tracer:
pythonfrom haystack import tracinghaystack_tracer = OpenTelemetryTracer(tracer)tracing.enable_tracing(haystack_tracer)