Files
mtusi2026lab7/app/observability/propagation.py
T
vozzhinandClaude Opus 5.5 31d441184b Перенести реализацию мессенджера из ЛР 6
Файлы скопированы без изменений из solution/BPI2301-Vozzhin
репозитория mtusi2026lab6. Доработки ЛР 7 идут отдельными коммитами.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-12-03 19:48:41 +05:00

27 lines
811 B
Python

from __future__ import annotations
from collections.abc import Mapping
from opentelemetry import propagate
_ALLOWED = {'traceparent', 'tracestate'}
def inject_trace_context(context=None) -> dict[str, str]:
carrier: dict[str, str] = {}
propagate.inject(carrier, context=context)
return {key: str(value) for key, value in carrier.items() if key in _ALLOWED}
def extract_trace_context(carrier: Mapping[str, object] | None):
normalized: dict[str, str] = {}
for key, value in (carrier or {}).items():
lowered = str(key).lower()
if lowered not in _ALLOWED or value is None:
continue
if isinstance(value, bytes):
value = value.decode('ascii', errors='ignore')
normalized[lowered] = str(value)
return propagate.extract(normalized)