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
+6 -1
View File
@@ -13,6 +13,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
@@ -20,7 +21,9 @@ PING_TIMEOUT_SECONDS = 2.0
async def serve() -> None:
platform = PlatformSettings()
pipeline = 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-relay")
broker = RabbitBroker(
pipeline.broker_url.get_secret_value(),
default_channel=Channel(publisher_confirms=True, on_return_raises=True),
@@ -56,6 +59,8 @@ async def serve() -> None:
await broker.stop()
await container.close()
logger.info("relay.stopped")
if telemetry is not None:
telemetry.shutdown(platform.telemetry_shutdown_seconds)
def main() -> None: