test(ttp): E.2.14a follow-up — force DECNET_DEVELOPER_TRACING=true, skip when Jaeger unreachable

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.
This commit is contained in:
2026-05-01 07:42:22 -04:00
parent f4fe6fe6e4
commit bf5414c0d1
2 changed files with 97 additions and 9 deletions

41
tests/ttp/conftest.py Normal file
View File

@@ -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

View File

@@ -29,7 +29,9 @@ steps (E.3.7 engine; E.3.5/E.3.6 store; E.3.9E.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: