|
| 1 | +--- |
| 2 | +title: "OpenTelemetry" |
| 3 | +id: integrations-opentelemetry |
| 4 | +description: "OpenTelemetry integration for Haystack" |
| 5 | +slug: "/integrations-opentelemetry" |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +## haystack_integrations.components.connectors.opentelemetry.opentelemetry_connector |
| 10 | + |
| 11 | +### OpenTelemetryConnector |
| 12 | + |
| 13 | +OpenTelemetryConnector connects Haystack to [OpenTelemetry](https://opentelemetry.io/) in order to enable the |
| 14 | + |
| 15 | +tracing of operations and data flow within the components of a pipeline. |
| 16 | + |
| 17 | +To use the OpenTelemetryConnector, add it to your pipeline without connecting it to any other component. It will |
| 18 | +automatically trace all pipeline operations when tracing is enabled. Make sure to configure an OpenTelemetry |
| 19 | +`TracerProvider` (for example, with an exporter) before initializing the connector. |
| 20 | + |
| 21 | +**Environment Configuration:** |
| 22 | + |
| 23 | +- `HAYSTACK_CONTENT_TRACING_ENABLED`: Must be set to `"true"` to trace the content (inputs and outputs) of the |
| 24 | + pipeline components. |
| 25 | + |
| 26 | +Here is an example of how to use the OpenTelemetryConnector in a pipeline: |
| 27 | + |
| 28 | +```python |
| 29 | +import os |
| 30 | + |
| 31 | +os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true" |
| 32 | + |
| 33 | +from opentelemetry import trace |
| 34 | +from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter |
| 35 | +from opentelemetry.sdk.resources import Resource |
| 36 | +from opentelemetry.sdk.trace import TracerProvider |
| 37 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 38 | +from opentelemetry.semconv.resource import ResourceAttributes |
| 39 | + |
| 40 | +# Configure the OpenTelemetry SDK. A service name is required for most backends. |
| 41 | +resource = Resource(attributes={ResourceAttributes.SERVICE_NAME: "haystack"}) |
| 42 | +tracer_provider = TracerProvider(resource=resource) |
| 43 | +processor = BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")) |
| 44 | +tracer_provider.add_span_processor(processor) |
| 45 | +trace.set_tracer_provider(tracer_provider) |
| 46 | + |
| 47 | +from haystack import Pipeline |
| 48 | +from haystack.components.builders import ChatPromptBuilder |
| 49 | +from haystack.components.generators.chat import OpenAIChatGenerator |
| 50 | +from haystack.dataclasses import ChatMessage |
| 51 | +from haystack_integrations.components.connectors.opentelemetry import OpenTelemetryConnector |
| 52 | + |
| 53 | +pipe = Pipeline() |
| 54 | +pipe.add_component("tracer", OpenTelemetryConnector()) |
| 55 | +pipe.add_component("prompt_builder", ChatPromptBuilder()) |
| 56 | +pipe.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini")) |
| 57 | + |
| 58 | +pipe.connect("prompt_builder.prompt", "llm.messages") |
| 59 | + |
| 60 | +messages = [ |
| 61 | + ChatMessage.from_system("Always respond in German even if some input data is in other languages."), |
| 62 | + ChatMessage.from_user("Tell me about {{location}}"), |
| 63 | +] |
| 64 | + |
| 65 | +response = pipe.run( |
| 66 | + data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages}} |
| 67 | +) |
| 68 | +print(response["llm"]["replies"][0]) |
| 69 | +``` |
| 70 | + |
| 71 | +#### __init__ |
| 72 | + |
| 73 | +```python |
| 74 | +__init__(name: str = 'opentelemetry') -> None |
| 75 | +``` |
| 76 | + |
| 77 | +Initialize the OpenTelemetryConnector component. |
| 78 | + |
| 79 | +**Parameters:** |
| 80 | + |
| 81 | +- **name** (<code>str</code>) – The name used to identify this tracing component. It is returned by the `run` method and can be |
| 82 | + used to mark traces produced by this connector. |
| 83 | + |
| 84 | +#### run |
| 85 | + |
| 86 | +```python |
| 87 | +run() -> dict[str, str] |
| 88 | +``` |
| 89 | + |
| 90 | +Runs the OpenTelemetryConnector component. |
| 91 | + |
| 92 | +**Returns:** |
| 93 | + |
| 94 | +- <code>dict\[str, str\]</code> – A dictionary with the following keys: |
| 95 | +- `name`: The name of the tracing component. |
| 96 | + |
| 97 | +#### to_dict |
| 98 | + |
| 99 | +```python |
| 100 | +to_dict() -> dict[str, Any] |
| 101 | +``` |
| 102 | + |
| 103 | +Serialize this component to a dictionary. |
| 104 | + |
| 105 | +**Returns:** |
| 106 | + |
| 107 | +- <code>dict\[str, Any\]</code> – The serialized component as a dictionary. |
| 108 | + |
| 109 | +#### from_dict |
| 110 | + |
| 111 | +```python |
| 112 | +from_dict(data: dict[str, Any]) -> OpenTelemetryConnector |
| 113 | +``` |
| 114 | + |
| 115 | +Deserialize this component from a dictionary. |
| 116 | + |
| 117 | +**Parameters:** |
| 118 | + |
| 119 | +- **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component. |
| 120 | + |
| 121 | +**Returns:** |
| 122 | + |
| 123 | +- <code>OpenTelemetryConnector</code> – The deserialized component instance. |
| 124 | + |
| 125 | +## haystack_integrations.tracing.opentelemetry.tracer |
| 126 | + |
| 127 | +### OpenTelemetrySpan |
| 128 | + |
| 129 | +Bases: <code>Span</code> |
| 130 | + |
| 131 | +#### __init__ |
| 132 | + |
| 133 | +```python |
| 134 | +__init__(span: opentelemetry.trace.Span) -> None |
| 135 | +``` |
| 136 | + |
| 137 | +Creates an instance of OpenTelemetrySpan. |
| 138 | + |
| 139 | +#### set_tag |
| 140 | + |
| 141 | +```python |
| 142 | +set_tag(key: str, value: Any) -> None |
| 143 | +``` |
| 144 | + |
| 145 | +Set a single tag on the span. |
| 146 | + |
| 147 | +**Parameters:** |
| 148 | + |
| 149 | +- **key** (<code>str</code>) – the name of the tag. |
| 150 | +- **value** (<code>Any</code>) – the value of the tag. |
| 151 | + |
| 152 | +#### raw_span |
| 153 | + |
| 154 | +```python |
| 155 | +raw_span() -> Any |
| 156 | +``` |
| 157 | + |
| 158 | +Provides access to the underlying span object of the tracer. |
| 159 | + |
| 160 | +**Returns:** |
| 161 | + |
| 162 | +- <code>Any</code> – The underlying span object. |
| 163 | + |
| 164 | +#### get_correlation_data_for_logs |
| 165 | + |
| 166 | +```python |
| 167 | +get_correlation_data_for_logs() -> dict[str, Any] |
| 168 | +``` |
| 169 | + |
| 170 | +Return a dictionary with correlation data for logs. |
| 171 | + |
| 172 | +### OpenTelemetryTracer |
| 173 | + |
| 174 | +Bases: <code>Tracer</code> |
| 175 | + |
| 176 | +#### __init__ |
| 177 | + |
| 178 | +```python |
| 179 | +__init__(tracer: opentelemetry.trace.Tracer) -> None |
| 180 | +``` |
| 181 | + |
| 182 | +Creates an instance of OpenTelemetryTracer. |
| 183 | + |
| 184 | +#### trace |
| 185 | + |
| 186 | +```python |
| 187 | +trace( |
| 188 | + operation_name: str, |
| 189 | + tags: dict[str, Any] | None = None, |
| 190 | + parent_span: Span | None = None, |
| 191 | +) -> Iterator[Span] |
| 192 | +``` |
| 193 | + |
| 194 | +Activate and return a new span that inherits from the current active span. |
| 195 | + |
| 196 | +#### current_span |
| 197 | + |
| 198 | +```python |
| 199 | +current_span() -> Span | None |
| 200 | +``` |
| 201 | + |
| 202 | +Return the current active span |
0 commit comments