Skip to main content
Async-safe context propagation for Mellea telemetry. Carries session_id, request_id, model_id, and sampling_iteration through async call chains via :mod:contextvars. Values automatically appear in log records (via :class:MelleaContextFilter) and can be attached to OpenTelemetry spans. Example:: from mellea.telemetry.context import with_context, async_with_context, get_current_context

Synchronous (also works inside async functions):

with with_context(session_id=“s-1”, model_id=“granite”): result = backend.generate(…)

Async-with syntax:

async with async_with_context(session_id=“s-1”, model_id=“granite”): result = await backend.generate(…)

logs emitted here carry session_id and model_id automatically

Functions

FUNC get_session_id

get_session_id() -> str | None
Return the session_id for the current async context, or None. Returns:
  • The active session ID string, or None if not set.

FUNC get_request_id

get_request_id() -> str | None
Return the request_id for the current async context, or None. Returns:
  • The active request ID string, or None if not set.

FUNC get_model_id

get_model_id() -> str | None
Return the model_id for the current async context, or None. Returns:
  • The active model ID string, or None if not set.

FUNC get_sampling_iteration

get_sampling_iteration() -> int | None
Return the sampling_iteration for the current async context, or None. Returns:
  • The current sampling iteration integer, or None if not set.

FUNC get_current_context

get_current_context() -> dict[str, Any]
Return a snapshot of all non-None context values. Returns:
  • Mapping of field names to their current values, omitting keys whose
  • value is None.

FUNC generate_request_id

generate_request_id() -> str
Generate a new unique request ID (UUID4 hex string). Returns:
  • A short hex string suitable for use as a request_id.

FUNC with_context

with_context(**kwargs: Any) -> Generator[None, None, None]
Synchronous context manager that sets telemetry context for the block duration. On exit the previous values are restored, making this safe for nested usage and concurrent asyncio tasks (each asyncio.Task owns an isolated copy of its ContextVar state). Accepted keyword arguments: session_id, request_id, model_id, sampling_iteration. Unknown keys raise :exc:ValueError. Args:
  • **kwargs: Context fields to set within the block.
Raises:
  • ValueError: If an unknown context field name is passed.
Example:: with with_context(session_id=“s-1”, model_id=“granite-4.0”): logger.info(“generating”) # log record includes both fields

FUNC async_with_context

async_with_context(**kwargs: Any) -> AsyncGenerator[None, None]
Async-with variant of :func:with_context. Identical semantics but usable with async with syntax. Note that :func:with_context also works inside async functions — use this only when you specifically need async with syntax. Args:
  • **kwargs: Context fields to set within the block.
Raises:
  • ValueError: If an unknown context field name is passed.

Classes

CLASS MelleaContextFilter

Logging filter that injects telemetry context fields into every log record. Fields from :func:get_current_context (session_id, request_id, model_id, sampling_iteration) are copied onto each :class:logging.LogRecord so they appear automatically in structured JSON output produced by :class:~mellea.core.utils.JsonFormatter.
Methods:

FUNC filter

filter(self, record: logging.LogRecord) -> bool
Attach telemetry context fields to record. Args:
  • record: The log record being processed.
Returns:
  • Always True — the record is never suppressed.