Coverage for src/lexigram/admin/audit/correlation.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 15:04 +0800

1"""Correlation ID propagation via contextvars.""" 

2 

3from __future__ import annotations 

4 

5from contextvars import ContextVar 

6import uuid 

7 

8correlation_id_ctx: ContextVar[str | None] = ContextVar( 

9 "admin_correlation_id", default=None 

10) 

11 

12 

13def get_correlation_id() -> str | None: 

14 """Return the current correlation ID (or None if not set).""" 

15 return correlation_id_ctx.get() 

16 

17 

18def set_correlation_id(cid: str) -> None: 

19 """Set the correlation ID for the current context.""" 

20 correlation_id_ctx.set(cid) 

21 

22 

23def new_correlation_id() -> str: 

24 """Generate a new unique correlation ID (UUID hex).""" 

25 return uuid.uuid4().hex 

26 

27 

28__all__ = [ 

29 "correlation_id_ctx", 

30 "get_correlation_id", 

31 "new_correlation_id", 

32 "set_correlation_id", 

33]