Audit and reports
Table of contents
- Introduction
- Recording an action
- Recording inside a write
- Storing the trail
- Reporting a degraded service
- Reading the request id
- Hearing the readiness probe
- Reference
Introduction
Your feature can tell the people who run the site what an account did, that a service it needs failed while it carried on, and which readiness check failed. The framework only holds the API. A feature of the product stores the trail or alerts someone, so you call the API always and never import that feature.
from splent_framework.audit import record_event
record_event("token.revoked", target_type="api_token", target_id=token.id)
Without a feature that keeps the trail, the call does nothing and returns False.
Recording an action
record_event(
"note.withdrawn",
target_type="note",
target_id=note.id,
data={"reason": reason},
)
Name the action area.verb in small letters. Anything else raises ValueError, even without a recorder, so your tests catch it. The actor is the signed-in account, anonymous when nobody is, and system outside a request.
The framework adds the client’s address, its user agent, the request id, the method and the endpoint, never the URL. In data, a key that names a credential, such as password or token, keeps no value.
Recording inside a write
@connect_signal("note-deleting", "splent_feature_reports")
def forget(sender, note, session, **kwargs):
record_event("report.entries_deleted", target_type="note", target_id=note.id, session=session)
Pass the session of a write that is not committed yet, such as the one a signal sent inside a transaction gives you. The event is then saved or rolled back with that write. Without session, it is kept at once.
Storing the trail
from splent_framework.audit import register_audit_recorder
register_audit_recorder(record, app)
A feature that stores the trail registers one function per app, from its init_feature. record(event) gets an AuditEvent and either adds it to event.session or keeps it at once. audit_enabled() tells you whether a recorder is registered.
A recorder that fails is logged and record_event returns False. It never raises, since the action already happened.
Reporting a degraded service
from splent_framework.observability import report_degraded
try:
client.index(note)
except ConnectionError as exc:
report_degraded("notes", "The search index is unreachable.", exc=exc, note_id=note.id)
When you catch a failure and carry on, say so. The framework logs a warning and hands a Degradation to every reporter registered with register_degradation_reporter(name, reporter, app). It never raises.
Keep message the same every time, with no personal data. Alerts show it and group by it. Put what varies in the keyword arguments.
Reading the request id
from splent_framework.observability import request_id
request_id() is the id of the current request, empty outside one. It takes the X-Request-ID a reverse proxy sets, when it looks like an id, and makes one otherwise. The audit trail and the degraded reports carry it.
proxy_set_header X-Request-ID $request_id;
Hearing the readiness probe
from splent_framework.health import register_readiness_listener
register_readiness_listener("alerts", on_probe, app)
After every /readyz probe, on_probe(failed) gets the names of the checks that failed, an empty list when the app is ready. A check that did not answer before SPLENT_READY_TIMEOUT counts as failed. See health probes.
The listener runs in the probe’s thread, so return at once and send anything slow from a thread of your own. One that raises is logged and changes nothing.
Only a probe tells the listeners. Calling
run_readiness_checks(app)yourself does not.
Reference
record_event(action, *, target_type, target_id, actor, outcome, data, session)
| Argument | Means |
|---|---|
action |
area.verb in small letters, such as account.deleted |
target_type, target_id |
What it was done to. The id is kept as text |
actor |
An account, an account’s id or "system". Left out, the signed-in account |
outcome |
success (default), failure or denied |
data |
Details as JSON values. Credentials lose their value, and past 4096 characters only the keys are kept |
session |
The session of the uncommitted write the event belongs to |
AuditEvent
action, outcome, actor, actor_id, target_type, target_id, data, ip, user_agent, request_id, method, endpoint, occurred_at (UTC) and session.
Degradation
feature, message, exc, context, request_id and occurred_at.