|
21 | 21 | import pytest |
22 | 22 |
|
23 | 23 | from models.result import BaseResult |
24 | | -from telemetry import DEFAULT_OTLP_ENDPOINT, _record_metrics, init_telemetry, run_tool |
| 24 | +from telemetry import ( |
| 25 | + DEFAULT_OTLP_ENDPOINT, |
| 26 | + DEFAULT_OTLP_PROTOCOL, |
| 27 | + _create_metric_exporter, |
| 28 | + _create_trace_exporter, |
| 29 | + _get_otlp_protocol, |
| 30 | + _record_metrics, |
| 31 | + init_telemetry, |
| 32 | + run_tool, |
| 33 | +) |
25 | 34 |
|
26 | 35 |
|
27 | 36 | def _make_ctx(meta=None): |
@@ -232,3 +241,53 @@ def test_explicit_endpoint_not_overridden(self, monkeypatch): |
232 | 241 | monkeypatch.delenv("OTEL_SDK_DISABLED", raising=False) |
233 | 242 | init_telemetry("perfecto-mcp", "1.0.0") |
234 | 243 | assert os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] == "http://localhost:4317" |
| 244 | + |
| 245 | + def test_defaults_protocol_to_grpc(self, monkeypatch): |
| 246 | + monkeypatch.delenv("OTEL_EXPORTER_OTLP_PROTOCOL", raising=False) |
| 247 | + assert _get_otlp_protocol() == DEFAULT_OTLP_PROTOCOL |
| 248 | + assert DEFAULT_OTLP_PROTOCOL == "grpc" |
| 249 | + |
| 250 | + def test_http_protocol_env(self, monkeypatch): |
| 251 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf") |
| 252 | + assert _get_otlp_protocol() == "http/protobuf" |
| 253 | + |
| 254 | + def test_does_not_raise_with_http_protocol(self, monkeypatch): |
| 255 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf") |
| 256 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318") |
| 257 | + init_telemetry("perfecto-mcp", "1.0.0") |
| 258 | + |
| 259 | + def test_grpc_trace_exporter_selected(self, monkeypatch): |
| 260 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc") |
| 261 | + with patch( |
| 262 | + "opentelemetry.exporter.otlp.proto.grpc.trace_exporter.OTLPSpanExporter", |
| 263 | + return_value=MagicMock(), |
| 264 | + ) as grpc_exporter: |
| 265 | + _create_trace_exporter() |
| 266 | + grpc_exporter.assert_called_once() |
| 267 | + |
| 268 | + def test_http_trace_exporter_selected(self, monkeypatch): |
| 269 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf") |
| 270 | + with patch( |
| 271 | + "opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter", |
| 272 | + return_value=MagicMock(), |
| 273 | + ) as http_exporter: |
| 274 | + _create_trace_exporter() |
| 275 | + http_exporter.assert_called_once() |
| 276 | + |
| 277 | + def test_grpc_metric_exporter_selected(self, monkeypatch): |
| 278 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc") |
| 279 | + with patch( |
| 280 | + "opentelemetry.exporter.otlp.proto.grpc.metric_exporter.OTLPMetricExporter", |
| 281 | + return_value=MagicMock(), |
| 282 | + ) as grpc_exporter: |
| 283 | + _create_metric_exporter() |
| 284 | + grpc_exporter.assert_called_once() |
| 285 | + |
| 286 | + def test_http_metric_exporter_selected(self, monkeypatch): |
| 287 | + monkeypatch.setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/json") |
| 288 | + with patch( |
| 289 | + "opentelemetry.exporter.otlp.proto.http.metric_exporter.OTLPMetricExporter", |
| 290 | + return_value=MagicMock(), |
| 291 | + ) as http_exporter: |
| 292 | + _create_metric_exporter() |
| 293 | + http_exporter.assert_called_once() |
0 commit comments