aiokpl.metrics¶
metrics ¶
In-process metric accumulator + scheduled flush onto a :class:MetricsSink.
Mirrors aws/metrics/metrics_manager.{h,cc} plus aws/metrics/accumulator.h
of the C++ KPL. The data model is intentionally simple: each named metric owns
a rolling 60-second window of (count, sum, min, max) bucketed by integer
monotonic second. Metric names follow the C++ KPL constants verbatim so
operators reading aiokpl dashboards next to native-KPL dashboards see the
same labels.
The :class:MetricsManager is off by default: when
level == MetricsLevel.NONE :meth:put returns immediately without any
allocation, and __aenter__ spawns no upload task. The Manager flushes
aggregated snapshots onto a :class:aiokpl.sinks.MetricsSink. The library
itself knows nothing about CloudWatch, OpenTelemetry, or Datadog — those are
first-party sinks shipped under :mod:aiokpl.sinks.
MetricsLevel ¶
Bases: Enum
How much detail :class:MetricsManager records.
NONE is the zero-overhead default: :meth:MetricsManager.put returns
immediately, no upload task is spawned, no sink is entered.
SUMMARY keeps only the global+stream dimensions (shard_id and
error_code are dropped). DETAILED keeps every dimension.
MetricKey
dataclass
¶
MetricKey(
name: str,
stream: str | None = None,
shard_id: str | None = None,
error_code: str | None = None,
)
The identity of a metric: name plus optional dimensions.
Dimensions are independent — two metrics with the same name but
different stream are distinct. Frozen so it is hashable and safe as a
dict key.
dimensions ¶
Render this key as the (name, value) tuple sinks consume.
Source code in aiokpl/metrics.py
Metric ¶
MetricsManager ¶
MetricsManager(
*,
level: MetricsLevel = MetricsLevel.NONE,
sink: MetricsSink | None = None,
upload_interval_ms: float = 60000.0,
clock: Callable[[], float] = time.monotonic,
sleep_fn: Callable[
[float], Awaitable[None]
] = anyio.sleep,
)
Owns the metric registry and schedules flushes onto a :class:MetricsSink.
When level == MetricsLevel.NONE: :meth:put is a no-op, no upload
task is spawned, the sink is not entered. This is the zero-overhead path
the Producer relies on for the default config.
When level == MetricsLevel.SUMMARY: shard_id and error_code
dimensions are dropped before registry lookup so only coarse keys exist.
When level == MetricsLevel.DETAILED: every dimension is preserved.
The sink owns its own transport lifecycle; the manager enters and exits
it as part of its own async with so callers only manage one context.
Source code in aiokpl/metrics.py
put ¶
put(
name: str,
value: float = 1.0,
*,
stream: str | None = None,
shard_id: str | None = None,
error_code: str | None = None,
) -> None
Record an observation. No-op when level == NONE.
When level == SUMMARY the shard_id and error_code
dimensions are dropped at lookup so the registry stays coarse.
When the underlying sink implements :class:EventfulMetricsSink,
:meth:record is invoked synchronously with a :class:MetricEvent.
Source code in aiokpl/metrics.py
snapshot ¶
Return (count, sum, min, max) per metric for the live window.
Kept for backward compatibility with in-process inspectors (tests,
embedded callers). Sinks consume :meth:snapshots instead.
Source code in aiokpl/metrics.py
snapshots ¶
Build :class:MetricSnapshot instances for the live window.
Source code in aiokpl/metrics.py
flush
async
¶
Build a snapshot and call sink.export. No-op when level is NONE.