Observability¶
Structured logging, metrics, and audit trail.
The observability module provides in-memory structured logging for the
replication system. It captures events, metrics, and security audit
decisions as structured records rather than text, making them easy to
filter, query, and export.
Constants¶
| Name | Value | Description |
|---|---|---|
DEFAULT_MAX_ENTRIES |
100,000 | Default capacity for event/metric deques |
Classes¶
Metric¶
A single metric data point.
| Field | Type | Description |
|---|---|---|
name |
str | Metric identifier |
value |
Any | Numeric or categorical value |
timestamp |
datetime | UTC collection time |
labels |
Optional[Dict[str, str]] | Key-value tags for filtering |
StructuredLogger¶
In-memory structured logging with bounded retention and audit support.
Constructor:
Both parameters accept None for unbounded storage (not recommended
for long-running simulations).
Attributes:
| Attribute | Type | Description |
|---|---|---|
events |
deque[dict] | Structured event records |
metrics |
deque[Metric] | Collected metric data points |
dropped_events |
int | Count of evicted events (overflow) |
dropped_metrics |
int | Count of evicted metrics (overflow) |
Methods:
| Method | Description |
|---|---|
log(event, **fields) |
Append a structured event with UTC timestamp |
emit_metric(name, value, **labels) |
Record a metric data point |
audit(decision, **fields) |
Log a security-relevant decision (prefixed audit) |
When the deque reaches capacity, the oldest entry is silently dropped
and the dropped_* counter is incremented.
Usage¶
from replication.observability import StructuredLogger
logger = StructuredLogger(max_events=1000)
# Log an event
logger.log("worker_started", worker_id="abc", depth=0)
# Record a metric
logger.emit_metric("cpu_usage", 0.75, worker_id="abc")
# Audit trail
logger.audit("deny_quota", reason="max_replicas", count=10)
# Query events
recent = [e for e in logger.events if e["event"] == "audit"]
observability
¶
StructuredLogger
¶
In-memory structured logging with audit trail support.
Parameters¶
max_events : int or None
Maximum event records to retain. When exceeded, the oldest
events are silently dropped. Pass None for unbounded
(not recommended for long-running simulations).
max_metrics : int or None
Maximum metric records to retain. Same eviction policy.