|
1 | 1 | """Tests for the instrumentor module.""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import sys |
| 5 | +from types import SimpleNamespace |
| 6 | + |
4 | 7 | import pytest |
5 | 8 | from unittest.mock import MagicMock, patch |
6 | 9 |
|
@@ -182,3 +185,142 @@ def test_merges_existing_trace_attributes(self): |
182 | 185 | call_kwargs = mock_agent_class.call_args[1] |
183 | 186 | assert call_kwargs["trace_attributes"]["session.id"] == "sess-123" |
184 | 187 | assert call_kwargs["trace_attributes"]["custom"] == "value" |
| 188 | + |
| 189 | + |
| 190 | +class _RecordingStrandsTelemetry: |
| 191 | + """Stands in for strands.telemetry.StrandsTelemetry and records which exporters were set up.""" |
| 192 | + |
| 193 | + calls: list = [] |
| 194 | + |
| 195 | + def __init__(self, tracer_provider=None): |
| 196 | + self.tracer_provider = tracer_provider |
| 197 | + |
| 198 | + def setup_otlp_exporter(self, **kwargs): |
| 199 | + self.calls.append("otlp") |
| 200 | + return self |
| 201 | + |
| 202 | + def setup_console_exporter(self, **kwargs): |
| 203 | + self.calls.append("console") |
| 204 | + return self |
| 205 | + |
| 206 | + def setup_meter(self, **kwargs): |
| 207 | + self.calls.append("meter") |
| 208 | + return self |
| 209 | + |
| 210 | + |
| 211 | +def _provider_exporting_to(endpoint, batch=False): |
| 212 | + """A tracer provider shaped like the SDK's, with one processor exporting to `endpoint`.""" |
| 213 | + exporter = SimpleNamespace(_endpoint=endpoint) |
| 214 | + if batch: |
| 215 | + # Recent OTel SDKs keep a BatchSpanProcessor's exporter on an inner _batch_processor. |
| 216 | + processor = SimpleNamespace(_batch_processor=SimpleNamespace(_exporter=exporter)) |
| 217 | + else: |
| 218 | + processor = SimpleNamespace(span_exporter=exporter) |
| 219 | + return SimpleNamespace( |
| 220 | + _active_span_processor=SimpleNamespace(_span_processors=(processor,)) |
| 221 | + ) |
| 222 | + |
| 223 | + |
| 224 | +_FUTURE_AGI = "http://127.0.0.1:5999/tracer/v1/traces" |
| 225 | + |
| 226 | + |
| 227 | +class TestStrandsOtlpExporterIsNotDoubled: |
| 228 | + """Strands' OTLP exporter is not added for a collector the provider already exports to. |
| 229 | +
|
| 230 | + With register()'s provider, the Future AGI exporter already sends every span. Pointing |
| 231 | + OTEL_EXPORTER_OTLP_* at Future AGI as well used to add a second exporter for the same |
| 232 | + collector, so every span arrived twice once fi-instrumentation-otel stopped dropping |
| 233 | + register()'s exporter on add_span_processor() (TH-8727). |
| 234 | + """ |
| 235 | + |
| 236 | + @pytest.fixture(autouse=True) |
| 237 | + def _recording_telemetry(self, monkeypatch): |
| 238 | + StrandsInstrumentor._instance = None |
| 239 | + StrandsInstrumentor._is_instrumented = False |
| 240 | + _RecordingStrandsTelemetry.calls = [] |
| 241 | + monkeypatch.setattr( |
| 242 | + sys.modules["strands.telemetry"], "StrandsTelemetry", _RecordingStrandsTelemetry |
| 243 | + ) |
| 244 | + for name in ("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", "OTEL_EXPORTER_OTLP_ENDPOINT"): |
| 245 | + monkeypatch.delenv(name, raising=False) |
| 246 | + yield |
| 247 | + StrandsInstrumentor._instance = None |
| 248 | + StrandsInstrumentor._is_instrumented = False |
| 249 | + |
| 250 | + @pytest.mark.parametrize("batch", [False, True], ids=["simple", "batch"]) |
| 251 | + def test_not_added_when_the_traces_endpoint_is_the_providers_collector( |
| 252 | + self, monkeypatch, batch |
| 253 | + ): |
| 254 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", _FUTURE_AGI) |
| 255 | + |
| 256 | + configure_strands_tracing( |
| 257 | + tracer_provider=_provider_exporting_to(_FUTURE_AGI, batch=batch), |
| 258 | + enable_metrics=False, |
| 259 | + ) |
| 260 | + |
| 261 | + assert _RecordingStrandsTelemetry.calls == [] |
| 262 | + |
| 263 | + def test_not_added_when_the_base_endpoint_is_the_providers_collector(self, monkeypatch): |
| 264 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://127.0.0.1:5999/") |
| 265 | + |
| 266 | + configure_strands_tracing( |
| 267 | + tracer_provider=_provider_exporting_to(_FUTURE_AGI), enable_metrics=False |
| 268 | + ) |
| 269 | + |
| 270 | + assert _RecordingStrandsTelemetry.calls == [] |
| 271 | + |
| 272 | + def test_not_added_when_otlp_endpoint_is_the_providers_collector(self, monkeypatch): |
| 273 | + # configure_strands_tracing() stores otlp_endpoint in OTEL_EXPORTER_OTLP_ENDPOINT; |
| 274 | + # monkeypatch puts the variable back afterwards. |
| 275 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "") |
| 276 | + |
| 277 | + configure_strands_tracing( |
| 278 | + tracer_provider=_provider_exporting_to("https://api.futureagi.com/tracer/v1/traces"), |
| 279 | + otlp_endpoint="https://API.futureagi.com:443", |
| 280 | + enable_metrics=False, |
| 281 | + ) |
| 282 | + |
| 283 | + assert _RecordingStrandsTelemetry.calls == [] |
| 284 | + |
| 285 | + def test_console_exporter_and_meter_are_still_set_up(self, monkeypatch): |
| 286 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", _FUTURE_AGI) |
| 287 | + |
| 288 | + configure_strands_tracing( |
| 289 | + tracer_provider=_provider_exporting_to(_FUTURE_AGI), |
| 290 | + enable_console_exporter=True, |
| 291 | + enable_metrics=True, |
| 292 | + ) |
| 293 | + |
| 294 | + assert _RecordingStrandsTelemetry.calls == ["console", "meter"] |
| 295 | + |
| 296 | + @pytest.mark.parametrize( |
| 297 | + "traces_endpoint", |
| 298 | + [ |
| 299 | + "http://127.0.0.1:6000/v1/traces", # another port |
| 300 | + "http://collector.internal:5999/v1/traces", # another host |
| 301 | + ], |
| 302 | + ) |
| 303 | + def test_added_for_another_collector(self, monkeypatch, traces_endpoint): |
| 304 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", traces_endpoint) |
| 305 | + |
| 306 | + configure_strands_tracing( |
| 307 | + tracer_provider=_provider_exporting_to(_FUTURE_AGI), enable_metrics=False |
| 308 | + ) |
| 309 | + |
| 310 | + assert _RecordingStrandsTelemetry.calls == ["otlp"] |
| 311 | + |
| 312 | + def test_added_for_the_default_endpoint_when_nothing_is_set(self): |
| 313 | + configure_strands_tracing( |
| 314 | + tracer_provider=_provider_exporting_to(_FUTURE_AGI), enable_metrics=False |
| 315 | + ) |
| 316 | + |
| 317 | + assert _RecordingStrandsTelemetry.calls == ["otlp"] |
| 318 | + |
| 319 | + def test_added_when_the_provider_has_no_span_processors( |
| 320 | + self, monkeypatch, mock_tracer_provider |
| 321 | + ): |
| 322 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", _FUTURE_AGI) |
| 323 | + |
| 324 | + configure_strands_tracing(tracer_provider=mock_tracer_provider, enable_metrics=False) |
| 325 | + |
| 326 | + assert _RecordingStrandsTelemetry.calls == ["otlp"] |
0 commit comments