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
+8 -1
View File
@@ -5,6 +5,7 @@ from dishka import Provider, make_async_container
from dishka_faststream import setup_dishka
from faststream import FastStream
from faststream.rabbit import RabbitBroker
from faststream.rabbit.opentelemetry import RabbitTelemetryMiddleware
from messenger.attachments.config import PipelineSettings
from messenger.attachments.entrypoints.heartbeat import Heartbeat
@@ -15,6 +16,7 @@ from messenger.shared.application.messagebus import EventBus
from messenger.shared.config import PlatformSettings
from messenger.shared.entrypoints.ioc import PlatformProvider
from messenger.shared.infrastructure.logging import PLATFORM_FIELDS, StructlogLogger
from messenger.shared.infrastructure.telemetry import start_telemetry
PING_TIMEOUT_SECONDS = 2.0
@@ -27,11 +29,14 @@ def create_worker(
) -> FastStream:
platform = platform or PlatformSettings()
pipeline = pipeline or PipelineSettings()
logger = StructlogLogger(level=platform.log_level, allowed_fields=PLATFORM_FIELDS | LOG_FIELDS)
fields = PLATFORM_FIELDS | LOG_FIELDS
logger = StructlogLogger(level=platform.log_level, allowed_fields=fields)
telemetry = start_telemetry(platform, fields, "messenger-worker")
broker = RabbitBroker(
pipeline.broker_url.get_secret_value(),
graceful_timeout=pipeline.shutdown_grace_seconds,
logger=None,
middlewares=() if telemetry is None else (RabbitTelemetryMiddleware(),),
)
subscribe(broker, pipeline.worker_concurrency)
container = make_async_container(
@@ -65,6 +70,8 @@ def create_worker(
await (await container.get(EventBus)).stop()
await container.close()
logger.info("worker.stopped")
if telemetry is not None:
telemetry.shutdown(platform.telemetry_shutdown_seconds)
setup_dishka(container, app, auto_inject=True)
return app