"""Проверки приватности и исходов на реальных SDK exporters в памяти.""" import json from fastapi import FastAPI import httpx from opentelemetry.sdk._logs.export import InMemoryLogRecordExporter from opentelemetry.sdk.metrics.export import InMemoryMetricReader from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter from opentelemetry.trace import SpanKind import pytest from app.core import telemetry as module @pytest.fixture(autouse=True) async def clean_db(): # Этот набор не использует БД, Redis и фикстуры приложения. yield @pytest.fixture def telemetry(monkeypatch): spans = InMemorySpanExporter() logs = InMemoryLogRecordExporter() metrics = InMemoryMetricReader() monkeypatch.setattr(module, "OTLPSpanExporter", lambda **_: spans) monkeypatch.setattr(module, "OTLPLogExporter", lambda **_: logs) monkeypatch.setattr(module, "PeriodicExportingMetricReader", lambda *_, **__: metrics) runtime = module.Telemetry("messenger-api", "unit", enabled=True) yield runtime, spans, logs, metrics runtime.shutdown() async def test_http_template_correlation_and_private_data(telemetry): runtime, spans, logs, metrics = telemetry app = FastAPI() @app.post("/items/{item_id}") async def item(item_id: str): return {"ok": True} app.add_middleware(module.HttpTelemetryMiddleware, telemetry=runtime) async with httpx.AsyncClient(transport=httpx.ASGITransport(app), base_url="http://test") as client: # ASGI caller может иметь свой активный span, не относящийся к входящему HTTP. with runtime.tracer.start_as_current_span("unrelated-task"): response = await client.post("/items/private-id?token=private-query", json={"text":"private-body"}, headers={"Authorization":"Bearer private-token"}) await client.get("/arbitrary-private-path") runtime.traces.force_flush() runtime.logs.force_flush() finished = spans.get_finished_spans() assert finished[0].kind == SpanKind.SERVER assert finished[0].name == "POST /items/{item_id}" assert finished[1].attributes["http.route"] == "unmatched" assert finished[0].context.trace_id != finished[1].context.trace_id assert all(span.parent is None for span in finished[:2]) body = json.loads(logs.get_finished_logs()[0].log_record.body) assert body["trace_id"] == response.headers["X-Trace-Id"] assert int(body["span_id"], 16) == finished[0].context.span_id data = metrics.get_metrics_data() all_attributes = [dict(point.attributes) for resource in data.resource_metrics for scope in resource.scope_metrics for metric in scope.metrics for point in metric.data.data_points] assert {"http.request.method":"POST", "http.route":"/items/{item_id}", "http.response.status_class":"2xx"} in all_attributes exported = repr([(s.name, dict(s.attributes)) for s in finished]) + repr(all_attributes) + repr([str(log.log_record.body) for log in logs.get_finished_logs()]) for private in ("private-id", "private-query", "private-body", "private-token", "arbitrary-private-path"): assert private not in exported async def test_unhandled_error_preserves_correlation_without_exception_payload(telemetry): from app.core.request_id import RequestIdMiddleware from app.errors.handlers import register_exception_handlers runtime, spans, logs, _ = telemetry app = FastAPI() @app.get("/explode") async def explode(): raise RuntimeError("private-error-with-credentials") app.add_middleware(RequestIdMiddleware) app.add_middleware(module.HttpTelemetryMiddleware, telemetry=runtime) register_exception_handlers(app) async with httpx.AsyncClient(transport=httpx.ASGITransport(app, raise_app_exceptions=False), base_url="http://test") as client: response = await client.get("/explode") runtime.traces.force_flush() runtime.logs.force_flush() record = json.loads(logs.get_finished_logs()[0].log_record.body) assert response.status_code == record["status_code"] == 500 assert response.headers["X-Request-Id"] == record["request_id"] assert spans.get_finished_spans()[0].status.status_code.name == "ERROR" assert "private-error-with-credentials" not in repr(record) def test_job_success_and_error_record_real_outcomes(telemetry): runtime, spans, logs, metrics = telemetry for fail in (False, True): try: with runtime.tracer.start_as_current_span("attachment.process.v1", kind=SpanKind.CONSUMER, record_exception=False, set_status_on_exception=False): with runtime.job_attempt(): if fail: raise RuntimeError("private-exception-payload") except RuntimeError: pass runtime.traces.force_flush() runtime.logs.force_flush() finished = spans.get_finished_spans() assert [s.attributes["outcome"] for s in finished] == ["success", "error"] assert finished[1].status.status_code.name == "ERROR" assert not finished[1].events records = [json.loads(item.log_record.body) for item in logs.get_finished_logs()] assert [record["outcome"] for record in records] == ["success", "error"] assert "private-exception-payload" not in repr(records) data = metrics.get_metrics_data() counters = [point for resource in data.resource_metrics for scope in resource.scope_metrics for metric in scope.metrics if metric.name == "messenger.job.attempts" for point in metric.data.data_points] assert {p.attributes["outcome"]:p.value for p in counters} == {"success":1,"error":1} def test_disabled_telemetry_has_no_exporters(): runtime = module.Telemetry("messenger-api", enabled=False) runtime.event("ignored") runtime.shutdown() assert not hasattr(runtime, "metrics") def test_tempo_base64_and_hex_identifiers_match(): from scripts.check_observability import identifier_hex assert identifier_hex("3X/51BqmrXA=", 8) == "dd7ff9d41aa6ad70" assert identifier_hex("dd7ff9d41aa6ad70", 8) == "dd7ff9d41aa6ad70"