27 lines
811 B
Python
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)
|