Files
mtusi2026lab2/student-tests/unit/test_telemetry_platform.py
T
ArtyomandClaude Opus 5.5 e60b4eac98 Start the OpenTelemetry SDK in every process
Add the OpenTelemetry API and SDK, the OTLP over HTTP exporter and the
FastAPI and asyncpg instrumentations to the lock, and a platform module
that builds one resource per process (service.name, service.version,
service.instance.id as hostname and pid, deployment.environment.name
from OTEL_RESOURCE_ATTRIBUTES), batch processors for traces and logs,
a periodic metric reader, PostgreSQL client spans and a bounded
shutdown in a daemon thread instead of the 30 s flush of the SDK. The
SDK stays off without a Collector address or with OTEL_SDK_DISABLED.

The sampler lets only server, consumer and producer spans open a
trace, so readiness probes, outbox polls and sweeps do not become
one-span traces. Log records carry trace_id and span_id in stdout and
go to OTLP through a handler that keeps only the allowlisted fields,
and deferred event subscribers now run in the context the event was
emitted in, so audit records stay linked to their request or job.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 04:45:14 +03:00

116 lines
4.7 KiB
Python

import json
import logging
from datetime import UTC, datetime
from typing import Any
from uuid import uuid4
import pytest
from opentelemetry import context, trace
from opentelemetry.sdk._logs import LoggerProvider
from opentelemetry.sdk._logs.export import InMemoryLogRecordExporter, SimpleLogRecordProcessor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.sdk.trace.sampling import Decision, ParentBased
from opentelemetry.trace import SpanKind
from fakes import NullLogger
from messenger.messaging.domain.events import MessagePosted
from messenger.shared.application.messagebus import EventBus
from messenger.shared.config import PlatformSettings
from messenger.shared.infrastructure.logging import PLATFORM_FIELDS, StructlogLogger
from messenger.shared.infrastructure.telemetry import OtlpLogHandler, ServerRoots, start_telemetry
UNUSED_DSN = "postgresql://unused@127.0.0.1:1/unused"
def traced() -> tuple[trace.Tracer, InMemorySpanExporter]:
exporter = InMemorySpanExporter()
provider = TracerProvider(shutdown_on_exit=False)
provider.add_span_processor(SimpleSpanProcessor(exporter))
return provider.get_tracer("test"), exporter
@pytest.mark.parametrize(
"switches",
[{"otel_sdk_disabled": True, "otel_exporter_otlp_endpoint": "http://otel-collector:4318"}, {}],
)
def test_disabled_or_unaddressed_sdk_starts_nothing(switches: dict[str, Any]) -> None:
settings = PlatformSettings.model_validate({"database_url": UNUSED_DSN, **switches})
assert start_telemetry(settings, PLATFORM_FIELDS, "messenger-api") is None
assert not isinstance(trace.get_tracer_provider(), TracerProvider)
def test_only_server_side_spans_open_a_trace() -> None:
sampler = ParentBased(ServerRoots())
roots = {kind: sampler.should_sample(None, 1, "root", kind).decision for kind in SpanKind}
assert roots == {
SpanKind.SERVER: Decision.RECORD_AND_SAMPLE,
SpanKind.CONSUMER: Decision.RECORD_AND_SAMPLE,
SpanKind.PRODUCER: Decision.RECORD_AND_SAMPLE,
SpanKind.CLIENT: Decision.DROP,
SpanKind.INTERNAL: Decision.DROP,
}
tracer, _ = traced()
with tracer.start_as_current_span("POST /api/v1/users", kind=SpanKind.SERVER):
child = sampler.should_sample(context.get_current(), 1, "SELECT", SpanKind.CLIENT)
assert child.decision is Decision.RECORD_AND_SAMPLE
def test_log_record_carries_the_span_and_only_allowed_fields(
capsys: pytest.CaptureFixture[str],
) -> None:
exporter = InMemoryLogRecordExporter()
provider = LoggerProvider(shutdown_on_exit=False)
provider.add_log_record_processor(SimpleLogRecordProcessor(exporter))
logger = StructlogLogger(level="INFO", allowed_fields=PLATFORM_FIELDS)
handler = OtlpLogHandler(provider.get_logger("test"), PLATFORM_FIELDS)
logging.getLogger().addHandler(handler)
tracer, _ = traced()
try:
with tracer.start_as_current_span("request", kind=SpanKind.SERVER) as span:
logger.info("request.handled", request_id="r-1", route="/x", password="hunter2")
logger.info("service.started")
finally:
logging.getLogger().removeHandler(handler)
expected = span.get_span_context()
request, started = (item.log_record for item in exporter.get_finished_logs())
assert request.body == "request.handled"
assert request.severity_text == "INFO"
assert (request.trace_id, request.span_id) == (expected.trace_id, expected.span_id)
assert dict(request.attributes or {}) == {
"request_id": "r-1",
"route": "/x",
"event": "request.handled",
}
assert started.body == "service.started"
assert started.trace_id == 0
line = json.loads(capsys.readouterr().out.splitlines()[0])
assert line["trace_id"] == trace.format_trace_id(expected.trace_id)
assert line["span_id"] == trace.format_span_id(expected.span_id)
assert "password" not in line
async def test_deferred_subscriber_runs_in_the_context_of_the_event() -> None:
bus = EventBus(NullLogger())
seen: list[int] = []
async def remember(_event: MessagePosted) -> None:
seen.append(trace.get_current_span().get_span_context().trace_id)
bus.subscribe(MessagePosted, remember)
bus.start()
tracer, _ = traced()
event = MessagePosted(
message_id=uuid4(),
chat_id=uuid4(),
sender_id=uuid4(),
text_length=1,
created_at=datetime.now(UTC),
)
with tracer.start_as_current_span("request", kind=SpanKind.SERVER) as span:
await bus.emit(event)
await bus.emit(event)
await bus.stop()
assert seen == [span.get_span_context().trace_id, 0]