Start the OpenTelemetry SDK in every process

Add the OpenTelemetry API and SDK, the OTLP over HTTP exporter and the
FastAPI and asyncpg instrumentations to the lock, and a platform module
that builds one resource per process (service.name, service.version,
service.instance.id as hostname and pid, deployment.environment.name
from OTEL_RESOURCE_ATTRIBUTES), batch processors for traces and logs,
a periodic metric reader, PostgreSQL client spans and a bounded
shutdown in a daemon thread instead of the 30 s flush of the SDK. The
SDK stays off without a Collector address or with OTEL_SDK_DISABLED.

The sampler lets only server, consumer and producer spans open a
trace, so readiness probes, outbox polls and sweeps do not become
one-span traces. Log records carry trace_id and span_id in stdout and
go to OTLP through a handler that keeps only the allowlisted fields,
and deferred event subscribers now run in the context the event was
emitted in, so audit records stay linked to their request or job.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 04:45:14 +03:00
co-authored by Claude Opus 5.5
parent a41d6d39e8
commit e60b4eac98
9 changed files with 608 additions and 12 deletions
@@ -1,10 +1,10 @@
## 1. Зависимости и платформа телеметрии
- [ ] 1.1 Зависимости `opentelemetry-sdk`, `opentelemetry-exporter-otlp-proto-http`, `opentelemetry-instrumentation-fastapi`, `opentelemetry-instrumentation-asyncpg`, `faststream[rabbit,otel]` в `pyproject.toml` и `uv.lock`
- [ ] 1.2 `PlatformSettings`: `otel_sdk_disabled`, `otel_exporter_otlp_endpoint`, `otel_service_name`, `telemetry_shutdown_seconds`
- [ ] 1.3 `shared/infrastructure/telemetry.py`: resource, sampler `ParentBased(ServerRoots)`, batch-экспорт трёх сигналов, View без метрик FastStream, `OtlpLogHandler`, инструментация asyncpg, ограниченный `shutdown`, `tag_request`
- [ ] 1.4 Журнал: `trace_id`/`span_id` в записи и в `PLATFORM_FIELDS`; `EventBus` выполняет отложенных подписчиков в контексте события
- [ ] 1.5 Проверка группы: unit-тесты — выключенный SDK не ставит провайдеры, запись с span несёт `trace_id`/`span_id`, OTLP-запись содержит только allowlist, подписчик получает контекст события
- [x] 1.1 Зависимости `opentelemetry-sdk`, `opentelemetry-exporter-otlp-proto-http`, `opentelemetry-instrumentation-fastapi`, `opentelemetry-instrumentation-asyncpg`, `faststream[rabbit,otel]` в `pyproject.toml` и `uv.lock`
- [x] 1.2 `PlatformSettings`: `otel_sdk_disabled`, `otel_exporter_otlp_endpoint`, `otel_service_name`, `telemetry_shutdown_seconds`
- [x] 1.3 `shared/infrastructure/telemetry.py`: resource, sampler `ParentBased(ServerRoots)`, batch-экспорт трёх сигналов, View без метрик FastStream, `OtlpLogHandler`, инструментация asyncpg, ограниченный `shutdown`, `tag_request`
- [x] 1.4 Журнал: `trace_id`/`span_id` в записи и в `PLATFORM_FIELDS`; `EventBus` выполняет отложенных подписчиков в контексте события
- [x] 1.5 Проверка группы: unit-тесты — выключенный SDK не ставит провайдеры, запись с span несёт `trace_id`/`span_id`, OTLP-запись содержит только allowlist, подписчик получает контекст события
## 2. Зона `attachments`