Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/1604.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- `reset_tracer()` shuts the tracer it drops down first, waiting for its exports in flight: every test resets the tracer, and a test's queued OTLP exports ran on into the next test, whose dry-run socket probe saw connections to `localhost:4318` it never made ([#1604](https://github.com/dan-petty/devops-cli/issues/1604)).
10 changes: 8 additions & 2 deletions src/devops_cli/telemetry/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,9 +1337,15 @@ def get_tracer() -> OTelTelemetryClient:


def reset_tracer() -> None:
"""Reset the global tracer instance (used for testing)."""
"""Reset the global tracer instance (used for testing).

The dropped tracer is shut down first, which waits for its exports in flight: one left
running carried a test's exports into the next test.
"""
global _GLOBAL_TRACER
_GLOBAL_TRACER = None
dropped, _GLOBAL_TRACER = _GLOBAL_TRACER, None
if dropped is not None:
dropped.shutdown()
if hasattr(_detect_vcs_metadata, "cache_clear"):
_detect_vcs_metadata.cache_clear()
if hasattr(_detect_k8s_context, "cache_clear"):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_gateway_review_stall.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ def handle_request(self, request: httpx2.Request) -> httpx2.Response:
assert (
calls,
payloads[0].ai_scratchpad.get("stage"),
) == (6, "reviewed")
payloads[0].ai_scratchpad.get("error"),
) == (6, "reviewed", None)
finally:
close_shared_clients()
22 changes: 22 additions & 0 deletions tests/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,25 @@ def test_response_span_attributes_enriched() -> None:
"server.port" in handle._attributes,
"gen_ai.server.served_by" in handle._attributes,
) == ("gpt-4o-2024-08-06", "ollama-48gib", 30.0, "example.com", False, False)


def test_a_reset_lets_no_export_of_the_dropped_tracer_run_on(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Every test resets the tracer, and an export still in flight from one test ran on into the
next, whose socket probe then saw connections it never made."""
import threading
import time

tracer = get_tracer()
finished = threading.Event()

def slow_send(path: str, payload: dict[str, Any]) -> None:
time.sleep(0.2)
finished.set()

monkeypatch.setattr(tracer, "enabled", True)
monkeypatch.setattr(tracer, "_send_payload_sync", slow_send)
tracer._send_payload("/v1/traces", {})
reset_tracer()
assert finished.is_set()
Loading