OpenTelemetry
haystack_integrations.components.connectors.opentelemetry.opentelemetry_connector
OpenTelemetryConnector
OpenTelemetryConnector connects Haystack to OpenTelemetry in order to enable the
tracing of operations and data flow within the components of a pipeline.
To use the OpenTelemetryConnector, add it to your pipeline without connecting it to any other component. It will
automatically trace all pipeline operations when tracing is enabled. Make sure to configure an OpenTelemetry
TracerProvider (for example, with an exporter) before initializing the connector.
Environment Configuration:
HAYSTACK_CONTENT_TRACING_ENABLED: Must be set to"true"to trace the content (inputs and outputs) of the pipeline components.
Here is an example of how to use the OpenTelemetryConnector in a pipeline:
import os
os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.semconv.resource import ResourceAttributes
# Configure the OpenTelemetry SDK. A service name is required for most backends.
resource = Resource(attributes={ResourceAttributes.SERVICE_NAME: "haystack"})
tracer_provider = TracerProvider(resource=resource)
processor = BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces"))
tracer_provider.add_span_processor(processor)
trace.set_tracer_provider(tracer_provider)
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.connectors.opentelemetry import OpenTelemetryConnector
pipe = Pipeline()
pipe.add_component("tracer", OpenTelemetryConnector())
pipe.add_component("prompt_builder", ChatPromptBuilder())
pipe.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini"))
pipe.connect("prompt_builder.prompt", "llm.messages")
messages = [
ChatMessage.from_system("Always respond in German even if some input data is in other languages."),
ChatMessage.from_user("Tell me about {{location}}"),
]
response = pipe.run(
data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages}}
)
print(response["llm"]["replies"][0])
init
Initialize the OpenTelemetryConnector component.
Parameters:
- name (
str) – The name used to identify this tracing component. It is returned by therunmethod and can be used to mark traces produced by this connector.
run
Runs the OpenTelemetryConnector component.
Returns:
dict[str, str]– A dictionary with the following keys:name: The name of the tracing component.
to_dict
Serialize this component to a dictionary.
Returns:
dict[str, Any]– The serialized component as a dictionary.
from_dict
Deserialize this component from a dictionary.
Parameters:
- data (
dict[str, Any]) – The dictionary representation of this component.
Returns:
OpenTelemetryConnector– The deserialized component instance.
haystack_integrations.tracing.opentelemetry.tracer
OpenTelemetrySpan
Bases: Span
init
Creates an instance of OpenTelemetrySpan.
set_tag
Set a single tag on the span.
Parameters:
- key (
str) – the name of the tag. - value (
Any) – the value of the tag.
raw_span
Provides access to the underlying span object of the tracer.
Returns:
Any– The underlying span object.
get_correlation_data_for_logs
Return a dictionary with correlation data for logs.
OpenTelemetryTracer
Bases: Tracer
init
Creates an instance of OpenTelemetryTracer.
trace
trace(
operation_name: str,
tags: dict[str, Any] | None = None,
parent_span: Span | None = None,
) -> Iterator[Span]
Activate and return a new span that inherits from the current active span.
current_span
Return the current active span