Coverage for src/lexigram/admin/audit/correlation.py: 0%
11 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-24 23:18 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-24 23:18 +0800
1"""Correlation ID propagation via contextvars."""
3from __future__ import annotations
5from contextvars import ContextVar
6import uuid
8correlation_id_ctx: ContextVar[str | None] = ContextVar(
9 "admin_correlation_id", default=None
10)
13def get_correlation_id() -> str | None:
14 """Return the current correlation ID (or None if not set)."""
15 return correlation_id_ctx.get()
18def set_correlation_id(cid: str) -> None:
19 """Set the correlation ID for the current context."""
20 correlation_id_ctx.set(cid)
23def new_correlation_id() -> str:
24 """Generate a new unique correlation ID (UUID hex)."""
25 return uuid.uuid4().hex
28__all__ = [
29 "correlation_id_ctx",
30 "get_correlation_id",
31 "new_correlation_id",
32 "set_correlation_id",
33]