|
| 1 | +"""Dependency spans and sampling. |
| 2 | +
|
| 3 | +Database spans are allowlisted for export; the LLM vendor transports deliberately are not. |
| 4 | +Sampling needs no code of ours, so the check here is that it stays that way. |
| 5 | +
|
| 6 | +Each case runs in a subprocess because the tracer provider is process-global. |
| 7 | +""" |
| 8 | + |
| 9 | +import gzip |
| 10 | +import http.server |
| 11 | +import os |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +import threading |
| 15 | + |
| 16 | +import pytest |
| 17 | +from lfx.observability import APPLICATION_INSTRUMENTATION_SCOPES |
| 18 | + |
| 19 | + |
| 20 | +class _Collector(http.server.BaseHTTPRequestHandler): |
| 21 | + def do_POST(self) -> None: |
| 22 | + body = self.rfile.read(int(self.headers.get("Content-Length", 0))) |
| 23 | + if self.headers.get("Content-Encoding") == "gzip": |
| 24 | + body = gzip.decompress(body) |
| 25 | + self.server.requests.append((self.path, body)) |
| 26 | + self.send_response(200) |
| 27 | + self.send_header("Content-Length", "0") |
| 28 | + self.end_headers() |
| 29 | + |
| 30 | + def log_message(self, *args) -> None: |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def collector(): |
| 36 | + server = http.server.HTTPServer(("127.0.0.1", 0), _Collector) |
| 37 | + server.requests = [] |
| 38 | + thread = threading.Thread(target=server.serve_forever, daemon=True) |
| 39 | + thread.start() |
| 40 | + yield server |
| 41 | + server.shutdown() |
| 42 | + server.server_close() |
| 43 | + thread.join(timeout=5) |
| 44 | + |
| 45 | + |
| 46 | +def test_outbound_http_scopes_stay_out_of_the_allowlist(): |
| 47 | + """Redacting URLs does not make the LLM transports safe to export; that boundary stands.""" |
| 48 | + for scope in ( |
| 49 | + "opentelemetry.instrumentation.httpx", |
| 50 | + "opentelemetry.instrumentation.requests", |
| 51 | + "opentelemetry.instrumentation.urllib3", |
| 52 | + ): |
| 53 | + assert scope not in APPLICATION_INSTRUMENTATION_SCOPES |
| 54 | + |
| 55 | + |
| 56 | +def test_database_spans_are_allowlisted(): |
| 57 | + """Verified separately to carry bound-parameter placeholders, never row values.""" |
| 58 | + assert "opentelemetry.instrumentation.sqlalchemy" in APPLICATION_INSTRUMENTATION_SCOPES |
| 59 | + |
| 60 | + |
| 61 | +# The ticket's sampler criterion, run against the real bootstrap rather than a hand-built |
| 62 | +# provider: the same request at ratio 0.0 must export nothing and at 1.0 must export the lot. |
| 63 | +SAMPLER_PROBE = """ |
| 64 | +import json |
| 65 | +from opentelemetry import trace |
| 66 | +from lfx.observability import APPLICATION_TRACER_NAME, bootstrap_application_telemetry |
| 67 | +
|
| 68 | +telemetry = bootstrap_application_telemetry() |
| 69 | +tracer = trace.get_tracer(APPLICATION_TRACER_NAME) |
| 70 | +for _ in range(20): |
| 71 | + with tracer.start_as_current_span("flow.execute"): |
| 72 | + pass |
| 73 | +telemetry.shutdown() |
| 74 | +print("PROBE_RESULT done") |
| 75 | +""" |
| 76 | + |
| 77 | + |
| 78 | +def _run_sampler_probe(endpoint: str, ratio: str) -> None: |
| 79 | + env = {k: v for k, v in os.environ.items() if not k.startswith("OTEL_")} |
| 80 | + env["OTEL_EXPORTER_OTLP_ENDPOINT"] = endpoint |
| 81 | + env["OTEL_EXPORTER_OTLP_PROTOCOL"] = "http/protobuf" |
| 82 | + env["OTEL_TRACES_SAMPLER"] = "traceidratio" |
| 83 | + env["OTEL_TRACES_SAMPLER_ARG"] = ratio |
| 84 | + completed = subprocess.run( # noqa: S603 |
| 85 | + [sys.executable, "-c", SAMPLER_PROBE], |
| 86 | + env=env, |
| 87 | + capture_output=True, |
| 88 | + text=True, |
| 89 | + timeout=300, |
| 90 | + check=False, |
| 91 | + ) |
| 92 | + assert completed.returncode == 0, completed.stderr |
| 93 | + |
| 94 | + |
| 95 | +def _exported_span_count(requests_seen) -> int: |
| 96 | + from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import ExportTraceServiceRequest |
| 97 | + |
| 98 | + total = 0 |
| 99 | + for path, body in requests_seen: |
| 100 | + if path != "/v1/traces": |
| 101 | + continue |
| 102 | + request = ExportTraceServiceRequest() |
| 103 | + request.ParseFromString(body) |
| 104 | + for rs in request.resource_spans: |
| 105 | + for ss in rs.scope_spans: |
| 106 | + total += len(ss.spans) |
| 107 | + return total |
| 108 | + |
| 109 | + |
| 110 | +def test_sampling_off_exports_nothing(collector): |
| 111 | + port = collector.server_address[1] |
| 112 | + _run_sampler_probe(f"http://127.0.0.1:{port}", "0.0") |
| 113 | + |
| 114 | + assert _exported_span_count(collector.requests) == 0 |
| 115 | + |
| 116 | + |
| 117 | +def test_sampling_on_exports_every_span(collector): |
| 118 | + port = collector.server_address[1] |
| 119 | + _run_sampler_probe(f"http://127.0.0.1:{port}", "1.0") |
| 120 | + |
| 121 | + assert _exported_span_count(collector.requests) == 20 |
0 commit comments