Skip to content

Implement a contextvars-based system to carry session_id, request_id, model_id, and sampling_iteration through async flows #459

Description

@psschwei

Description:
Implement a contextvars-based system to carry session_id, request_id, model_id, and sampling_iteration through async flows. This context should automatically appear in logs and traces.

Detailed Requirements:

  1. Create mellea/telemetry/context.py module with:
    • ContextVar declarations for: session_id, request_id, model_id, sampling_iteration
    • Context manager with_context(**kwargs) for setting context
    • Helper functions: get_session_id(), get_request_id(), etc.
    • get_current_context() returning dict of all context values
  2. Create MelleaContextFilter (extends logging.Filter) that injects context into log records
  3. Add filter to FancyLogger's handlers
  4. Update JsonFormatter to include context fields when present
  5. Integrate context setting in key code paths:
    • MelleaSession.__enter__() - Set session_id
    • BaseSamplingStrategy.sample() - Set sampling_iteration
    • Backend calls - Set request_id and model_id
  6. Propagate context to child spans automatically

Files to Create:

  • mellea/telemetry/context.py

Files to Modify:

  • mellea/core/utils.py - Add MelleaContextFilter, update JsonFormatter
  • mellea/stdlib/session.py - Set session context
  • mellea/stdlib/sampling/base.py - Set sampling iteration context
  • mellea/backends/*.py - Set request/model context

Acceptance Criteria:

  • Context flows correctly through async call chains
  • Logs include session_id, request_id when available
  • Context automatically propagated to child spans
  • Context cleared appropriately when scope exits

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions