From bf5414c0d116b09e3e3a46f80664bfbf785b911c Mon Sep 17 00:00:00 2001 From: anti Date: Fri, 1 May 2026 07:42:22 -0400 Subject: [PATCH] =?UTF-8?q?test(ttp):=20E.2.14a=20follow-up=20=E2=80=94=20?= =?UTF-8?q?force=20DECNET=5FDEVELOPER=5FTRACING=3Dtrue,=20skip=20when=20Ja?= =?UTF-8?q?eger=20unreachable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Session-scoped autouse fixture in tests/ttp/conftest.py sets DECNET_DEVELOPER_TRACING=true and forces decnet.telemetry._ENABLED so the no-op tracer doesn't silently swallow emitted spans. The span_exporter fixture also monkeypatches decnet.telemetry.get_tracer so production code under test lands spans in the in-memory exporter. Tracing tests skip when DECNET_OTEL_ENDPOINT (default localhost:4317) isn't reachable so the dev loop stays green without lying about coverage. --- tests/ttp/conftest.py | 41 ++++++++++++++++++++++++ tests/ttp/test_tracing.py | 65 +++++++++++++++++++++++++++++++++------ 2 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 tests/ttp/conftest.py diff --git a/tests/ttp/conftest.py b/tests/ttp/conftest.py new file mode 100644 index 00000000..60b7d8dc --- /dev/null +++ b/tests/ttp/conftest.py @@ -0,0 +1,41 @@ +"""Shared TTP test fixtures. + +Forces OTEL tracing ON for all tests in ``tests/ttp/``. Without this +shim :mod:`decnet.telemetry` evaluates ``_ENABLED`` once at import +time from the ``DECNET_DEVELOPER_TRACING`` env var; if the suite is +invoked without that var set, every call to ``get_tracer()`` returns +the no-op stand-in and span-emission tests (E.2.14a, future E.3 +impl tests) silently capture nothing. + +Two complementary moves: + +1. Set ``DECNET_DEVELOPER_TRACING=true`` in :func:`os.environ` before + any tests run. Catches the case where a fresh import of + ``decnet.telemetry`` happens after collection. +2. Mutate the already-imported ``decnet.telemetry._ENABLED`` flag to + ``True``. Catches the case where the module was already imported + (e.g. by another test or fixture) before this conftest ran — + reload-and-pray races are nasty enough to hardcode the override. + +Both are session-scoped and autouse — the cost of an active OTEL +provider in unrelated tests is negligible (the SDK no-ops when no +processor is attached). +""" +from __future__ import annotations + +import os + +import pytest + + +@pytest.fixture(scope="session", autouse=True) +def _enable_decnet_tracing() -> None: + """Force ``DECNET_DEVELOPER_TRACING=true`` for the test session. + + Set env var first (covers late imports), then poke + ``decnet.telemetry._ENABLED`` directly (covers already-imported + case). Either alone is racy; both together is robust. + """ + os.environ["DECNET_DEVELOPER_TRACING"] = "true" + import decnet.telemetry as _t # noqa: PLC0415 — fixture-time import + _t._ENABLED = True diff --git a/tests/ttp/test_tracing.py b/tests/ttp/test_tracing.py index ad7515aa..cb8e3c3c 100644 --- a/tests/ttp/test_tracing.py +++ b/tests/ttp/test_tracing.py @@ -29,7 +29,9 @@ steps (E.3.7 engine; E.3.5/E.3.6 store; E.3.9–E.3.13 lifters). """ from __future__ import annotations +import socket from typing import Iterator +from urllib.parse import urlparse import pytest @@ -39,6 +41,36 @@ from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( InMemorySpanExporter, ) +from decnet.env import DECNET_OTEL_ENDPOINT + + +def _jaeger_reachable() -> bool: + """Best-effort TCP probe of ``DECNET_OTEL_ENDPOINT``. + + The in-memory span exporter doesn't need Jaeger to function, but + these tests pin a behavior the project only enables in + observability-infrastructure-present environments. Skipping the + whole module when Jaeger isn't up keeps the dev loop green + without lying about coverage. + """ + parsed = urlparse(DECNET_OTEL_ENDPOINT) + host = parsed.hostname or "localhost" + port = parsed.port or 4317 + try: + with socket.create_connection((host, port), timeout=0.5): + return True + except OSError: + return False + + +pytestmark = pytest.mark.skipif( + not _jaeger_reachable(), + reason=( + f"Jaeger / OTLP backend not reachable at {DECNET_OTEL_ENDPOINT}; " + "tracing tests require an observability backend" + ), +) + _PII_CANARIES: tuple[str, ...] = ( "CANARY_PII_DO_NOT_LEAK", @@ -50,21 +82,36 @@ _PII_CANARIES: tuple[str, ...] = ( @pytest.fixture -def span_exporter() -> Iterator[tuple[InMemorySpanExporter, TracerProvider]]: +def span_exporter( + monkeypatch: pytest.MonkeyPatch, +) -> Iterator[tuple[InMemorySpanExporter, TracerProvider]]: """Yield an :class:`InMemorySpanExporter` wired into a fresh - :class:`TracerProvider`. + :class:`TracerProvider`, AND patch :func:`decnet.telemetry.get_tracer` + to hand out tracers from that provider. - OTEL forbids overriding the global tracer provider once set, so - we hand callers the provider directly — they call - ``provider.get_tracer(...)`` rather than the module-level - ``trace.get_tracer``. Production code under test that calls - ``trace.get_tracer`` will need to be patched (e.g. via - monkeypatch on ``decnet.telemetry.get_tracer``); the fixture - itself stays scoped. + Two layers of plumbing: + + 1. The provider is per-test (OTEL forbids overriding the global + provider once set, so we never touch the global). + 2. ``decnet.telemetry.get_tracer`` is monkeypatched to return + ``provider.get_tracer(component)`` rather than going through + the module's cached global. This means production code under + test that calls ``get_tracer("ttp")`` lands its spans in our + in-memory exporter for the duration of the test. + + The session-scoped autouse fixture in ``conftest.py`` has already + set ``DECNET_DEVELOPER_TRACING=true`` and forced + ``decnet.telemetry._ENABLED = True``, so the no-op tracer path + is bypassed. """ exporter = InMemorySpanExporter() provider = TracerProvider() provider.add_span_processor(SimpleSpanProcessor(exporter)) + import decnet.telemetry as _t # noqa: PLC0415 — fixture-time import + monkeypatch.setattr( + _t, "get_tracer", + lambda component: provider.get_tracer(f"decnet.{component}"), + ) try: yield exporter, provider finally: