Instrument the API, the relay and the worker
Start the telemetry in create_app, which gunicorn runs in every worker after fork because preload_app is off, and in the relay and worker processes with their own service names, and stop it after the rest of the shutdown within TELEMETRY_SHUTDOWN_SECONDS. The API gets server spans and the http.server.request.duration histogram in seconds from the FastAPI instrumentation with stable HTTP semantic conventions, without /health probes and without the internal send and receive spans, and the request pipeline puts X-Request-Id on the server span as messenger.request_id. The worker broker gets the FastStream RabbitTelemetryMiddleware, so every delivery runs inside a consumer span. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e2ec3fe9af
commit
a8836ce8b5
@@ -0,0 +1,64 @@
|
||||
from uuid import uuid4
|
||||
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from opentelemetry.sdk.metrics import MeterProvider
|
||||
from opentelemetry.sdk.metrics.export import HistogramDataPoint, InMemoryMetricReader
|
||||
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.trace import SpanKind
|
||||
|
||||
from conftest import new_user, split_settings
|
||||
from fakes import MemoryStorageProvider
|
||||
from messenger.main import create_app
|
||||
from messenger.shared.entrypoints.telemetry import instrument_http
|
||||
|
||||
MARKER = "текст-маркер-телеметрии"
|
||||
|
||||
|
||||
async def test_server_spans_and_duration_follow_the_route_template() -> None:
|
||||
exporter = InMemorySpanExporter()
|
||||
tracer = TracerProvider(shutdown_on_exit=False)
|
||||
tracer.add_span_processor(SimpleSpanProcessor(exporter))
|
||||
reader = InMemoryMetricReader()
|
||||
app = create_app(**split_settings(), overrides=(MemoryStorageProvider(),))
|
||||
instrument_http(app, tracer, MeterProvider(metric_readers=[reader], shutdown_on_exit=False))
|
||||
chat = uuid4()
|
||||
async with (
|
||||
app.router.lifespan_context(app),
|
||||
AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") as http,
|
||||
):
|
||||
created = await http.post("/api/v1/users", json=new_user("otel"))
|
||||
ready = await http.get("/health/ready")
|
||||
denied = await http.post(
|
||||
f"/api/v1/chats/{chat}/messages",
|
||||
json={"text": MARKER},
|
||||
headers={"Idempotency-Key": "otel-1", "Authorization": "Bearer not-a-token"},
|
||||
)
|
||||
assert (created.status_code, ready.status_code, denied.status_code) == (201, 200, 401)
|
||||
|
||||
spans = exporter.get_finished_spans()
|
||||
servers = {span.name: span for span in spans if span.kind is SpanKind.SERVER}
|
||||
assert set(servers) == {"POST /api/v1/users", "POST /api/v1/chats/{chat_id}/messages"}
|
||||
users = servers["POST /api/v1/users"].attributes or {}
|
||||
assert users["messenger.request_id"] == created.headers["X-Request-Id"]
|
||||
assert users["http.route"] == "/api/v1/users"
|
||||
assert users["http.response.status_code"] == 201
|
||||
exported = repr([dict(span.attributes or {}) for span in spans])
|
||||
assert MARKER not in exported
|
||||
assert "not-a-token" not in exported
|
||||
|
||||
data = reader.get_metrics_data()
|
||||
assert data is not None
|
||||
points = [
|
||||
point
|
||||
for resource in data.resource_metrics
|
||||
for scope in resource.scope_metrics
|
||||
for metric in scope.metrics
|
||||
if metric.name == "http.server.request.duration" and metric.unit == "s"
|
||||
for point in metric.data.data_points
|
||||
if isinstance(point, HistogramDataPoint)
|
||||
]
|
||||
routes = {(point.attributes or {}).get("http.route") for point in points}
|
||||
assert routes == {"/api/v1/users", "/api/v1/chats/{chat_id}/messages"}
|
||||
assert str(chat) not in repr([dict(point.attributes or {}) for point in points])
|
||||
Reference in New Issue
Block a user