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:
Artyom
2026-10-29 04:45:26 +03:00
co-authored by Claude Opus 5.5
parent e2ec3fe9af
commit a8836ce8b5
7 changed files with 113 additions and 5 deletions
@@ -11,6 +11,7 @@ from messenger.shared.application.errors import Unavailable
from messenger.shared.application.ports import Logger
from messenger.shared.entrypoints.errors import INSTANCE_ID_HEADER, MESSAGES, REQUEST_ID_HEADER
from messenger.shared.entrypoints.schemas import ErrorEnvelope
from messenger.shared.infrastructure.telemetry import tag_request
type Headers = tuple[tuple[str, str], ...]
@@ -85,6 +86,7 @@ class RequestPipeline:
incoming = header(scope, HEADER_BYTES)
request_id = incoming[:MAX_REQUEST_ID] if incoming else uuid4().hex
tag_request(request_id)
state = scope.setdefault("state", {})
state["request_id"] = request_id
state["actor"] = None
@@ -0,0 +1,23 @@
import os
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.metrics import MeterProvider
from opentelemetry.trace import TracerProvider
PROBES = "/health/"
def instrument_http(
app: FastAPI,
tracer_provider: TracerProvider | None = None,
meter_provider: MeterProvider | None = None,
) -> None:
os.environ.setdefault("OTEL_SEMCONV_STABILITY_OPT_IN", "http")
FastAPIInstrumentor.instrument_app(
app,
tracer_provider=tracer_provider,
meter_provider=meter_provider,
excluded_urls=PROBES,
exclude_spans=["receive", "send"],
)