Skip to content

Commit 6eebd83

Browse files
lesnik512claude
andcommitted
test(observability): assert log record fires in the no-active-span path
The test previously passed even if _emit_event became a no-op because its only assertion was "no exception raised." Strengthen it with caplog: capture at WARNING level on the test logger and assert that exactly one record fires with the correct level, message, and event attribute. This confirms the log-only fallback path actually emitted (not silently swallowed) when OTel is installed but no tracer is active. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent d4ae14d commit 6eebd83

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

‎tests/test_observability.py‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,29 @@ def test_emit_event_calls_add_event_when_otel_installed() -> None:
119119
mock_span.add_event.assert_called_once_with("test.event", attributes={"k": "v"})
120120

121121

122-
def test_emit_event_works_when_otel_installed_but_no_active_span() -> None:
122+
def test_emit_event_works_when_otel_installed_but_no_active_span(
123+
caplog: pytest.LogCaptureFixture,
124+
) -> None:
123125
"""With OTel installed but no tracer configured, get_current_span() returns NonRecordingSpan.
124126
125-
add_event is a documented no-op. No error.
127+
add_event is a documented no-op. The log-only fallback path must still emit
128+
a record at the requested level with the correct event attribute.
126129
"""
127130
# Real OTel API call (no mocking) — opentelemetry-api is installed via the otel extra.
128-
_emit_event(
129-
_TEST_LOGGER,
130-
"test.event",
131-
level=logging.WARNING,
132-
message="real-otel-but-no-tracer",
133-
attributes={"a": 1},
134-
)
135-
# No assertion needed — the absence of an exception IS the assertion.
131+
with caplog.at_level(logging.WARNING, logger="httpware.test.observability"):
132+
_emit_event(
133+
_TEST_LOGGER,
134+
"test.event",
135+
level=logging.WARNING,
136+
message="real-otel-but-no-tracer",
137+
attributes={"a": 1},
138+
)
139+
140+
assert len(caplog.records) == 1
141+
record = caplog.records[0]
142+
assert record.levelno == logging.WARNING
143+
assert record.message == "real-otel-but-no-tracer"
144+
assert record.event == "test.event" # ty: ignore[unresolved-attribute]
136145

137146

138147
def test_emit_event_swallows_add_event_failure() -> None:

0 commit comments

Comments
 (0)