audit_trail.context¶
Request context: who, where and how an audited change happened.
The context reaches the audit writer in one of three ways, checked in this
order by :func:resolve_context:
- bound to a session with :func:
bind(session.info["audit_context"]); - returned by a host-supplied
context_provider; - the built-in context variable, set with :func:
context.
When none of them yields a context, an anonymous one is used.
Actor
dataclass
¶
Who performed an action.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
Kind of actor, defined by the host (for example |
id |
str | None
|
Opaque identifier of the actor, stored in |
label |
str | None
|
Human-readable snapshot (for example an e-mail address) that survives deletion of the actor. |
AuditContext
dataclass
¶
AuditContext(
actor_type: str = _ANONYMOUS,
actor_id: str | None = None,
actor_label: str | None = None,
remote_addr: str | None = None,
user_agent: str | None = None,
method: str | None = None,
path: str | None = None,
channel: str | None = None,
auth_method: str | None = None,
request_id: UUID | None = None,
correlation_id: UUID | None = None,
scope_id: str | None = None,
extra: dict[str, Any] = dict(),
)
Who, where and how of the current unit of work.
The object is mutable on purpose: it is set once (for example by a
middleware) and later updated in place with :func:set_actor, so changes
made in a worker thread are visible to the code that started it.
Attributes:
| Name | Type | Description |
|---|---|---|
actor_type |
str
|
Kind of actor. Defaults to |
actor_id |
str | None
|
Opaque identifier of the actor. |
actor_label |
str | None
|
Human-readable snapshot of the actor. |
remote_addr |
str | None
|
Client IP address. |
user_agent |
str | None
|
Client user agent. |
method |
str | None
|
Request method, for example |
path |
str | None
|
Request path. |
channel |
str | None
|
Entry point, for example |
auth_method |
str | None
|
How the actor authenticated. |
request_id |
UUID | None
|
Identifier of the request. |
correlation_id |
UUID | None
|
Links several database transactions. When not given,
it is set to |
scope_id |
str | None
|
Scope of the change, for example a tenant. |
extra |
dict[str, Any]
|
Additional host-defined context, stored as |
ContextScope ¶
Sync and async context manager that activates one AuditContext.
Returned by :func:context; entering it yields the activated context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
AuditContext
|
The context to activate. |
required |
Source code in audit_trail/context.py
ContextSnapshot ¶
Bases: TypedDict
The data.context snapshot stored on each audit entry.
Every key is optional: empty values are left out.
current_context ¶
Return the context set with :func:context, if any.
Returns:
| Type | Description |
|---|---|
AuditContext | None
|
The active context object, or |
context ¶
context(
ctx: AuditContext | None = None,
/,
*,
actor_type: str | None = None,
actor_id: str | None = None,
actor_label: str | None = None,
remote_addr: str | None = None,
user_agent: str | None = None,
method: str | None = None,
path: str | None = None,
channel: str | None = None,
auth_method: str | None = None,
request_id: UUID | None = None,
correlation_id: UUID | None = None,
scope_id: str | None = None,
extra: dict[str, Any] | None = None,
) -> ContextScope
Activate an audit context for a block of code.
Usable as with context(...) and async with context(...). The
previous context is restored on exit. A nested block replaces the outer
context; it does not inherit its fields.
Pass either a ready :class:AuditContext or the fields of a new one; the
keyword arguments mirror the :class:AuditContext attributes.
Example::
with context(actor_type="system", actor_label="cleanup", channel="worker"):
...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
AuditContext | None
|
A ready context to activate. Mutually exclusive with the keyword arguments. |
None
|
actor_type
|
str | None
|
Kind of actor. |
None
|
actor_id
|
str | None
|
Opaque identifier of the actor. |
None
|
actor_label
|
str | None
|
Human-readable snapshot of the actor. |
None
|
remote_addr
|
str | None
|
Client IP address. |
None
|
user_agent
|
str | None
|
Client user agent. |
None
|
method
|
str | None
|
Request method. |
None
|
path
|
str | None
|
Request path. |
None
|
channel
|
str | None
|
Entry point, for example |
None
|
auth_method
|
str | None
|
How the actor authenticated. |
None
|
request_id
|
UUID | None
|
Identifier of the request. |
None
|
correlation_id
|
UUID | None
|
Links several database transactions. Defaults to
|
None
|
scope_id
|
str | None
|
Scope of the change, for example a tenant. |
None
|
extra
|
dict[str, Any] | None
|
Additional host-defined context, stored as |
None
|
Returns:
| Type | Description |
|---|---|
ContextScope
|
A context manager yielding the active :class: |
Raises:
| Type | Description |
|---|---|
TypeError
|
If both |
Source code in audit_trail/context.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
set_actor ¶
Set the actor on the active context, in place.
The active object is mutated rather than replaced, so an actor set from a thread pool (for example a synchronous dependency) is visible to the caller.
A context bound with :func:bind takes precedence, so set_actor on
the active object does not reach that session; set the actor fields on
the bound object instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actor
|
Actor
|
The actor. All three actor fields are overwritten. |
required |
Returns:
| Type | Description |
|---|---|
AuditContext
|
The updated context. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no :func: |
Source code in audit_trail/context.py
bind ¶
Attach a context to one session.
Takes precedence over the provider and the built-in context variable. Binding does not enable auditing on the session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session | AsyncSession
|
The session to attach the context to. |
required |
context
|
AuditContext
|
The context used for audit entries written by this session. |
required |
Source code in audit_trail/context.py
resolve_context ¶
resolve_context(
session: Session | AsyncSession,
context_provider: Callable[[], AuditContext | None]
| None = None,
) -> AuditContext
Return the context that applies to session.
Checks, in order: the context bound with :func:bind, then
context_provider(), then the built-in context variable. The object is
returned as is, not copied, so later :func:set_actor calls stay visible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session | AsyncSession
|
The session being audited. |
required |
context_provider
|
Callable[[], AuditContext | None] | None
|
Optional host hook returning the current context,
or |
None
|
Returns:
| Type | Description |
|---|---|
AuditContext
|
The first context found, or a new anonymous one. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in audit_trail/context.py
context_snapshot ¶
Build the data.context snapshot stored on each audit entry.
Empty values (None, "", an empty extra) are left out.
actor_id is never included: it is stored in its own column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
AuditContext
|
The context to snapshot. |
required |
Returns:
| Type | Description |
|---|---|
ContextSnapshot
|
The snapshot; |
ContextSnapshot
|
|