Client Metrics¶
The client can record what it observes while executing commands — latency histograms, byte counts, connection lifecycle, retry and timeout counters — and hand it back as a structured snapshot. Collection happens inside the client core and is off by default.
What collection costs¶
Measured on a 3-node cluster, 50/50 read/update over 100K keys with 1KB records, comparing each configuration against the same build with metrics disabled:
Configuration |
Throughput vs disabled |
|---|---|
Disabled (default) |
baseline |
|
within ±2% |
|
within ±2% |
|
within ±2% |
That held across every client shape measured — blocking calls from 96 to 180 threads, and asyncio at task counts from 32 to 256, single-loop and pooled. Latency percentiles were unchanged: p50 and p90 matched the disabled build in nearly every cell. In short, collection is cheap enough that it is not a reason to leave metrics off, and the sampler exists for tail fidelity and recording volume rather than for throughput.
Enabling and polling¶
Metrics are cluster-scoped: enable them on the
Cluster and poll snapshots at your export
interval.
from aerospike_sdk.metrics import LatencyType, MetricsPolicy
cluster.enable_metrics(MetricsPolicy(operational_enabled=True))
# ... application traffic ...
snapshot = cluster.metrics_snapshot() # plain on both the async and sync Cluster
reads = snapshot.latency(LatencyType.READ)
print(f"{reads.count} reads, avg {reads.average:.1f} ms")
Snapshot values are cumulative since metrics were enabled — they are not
deltas since the last poll. Connection gauges (in_use, in_pool) are
point-in-time. Snapshotting drains and aggregates per-node state, so poll on
an interval (for example every 30 seconds), not per operation.
A snapshot can be taken whether or not collection is on; while it is off the
counters are frozen at their last values. metrics_enabled,
operational_metrics_enabled and usage_metrics_enabled on the snapshot (and
in its canonical document) say which tiers were collecting when it was taken.
Collection tiers¶
Collection comes in tiers, so the cost of measuring is opt-in:
Tier |
Contents |
Switched on by |
|---|---|---|
Always on |
Pool occupancy, connections opened/closed, tend counts, node membership |
|
Operational |
Latency and byte histograms, result codes, retry and error counters, the per-call command count |
|
Usage |
Which SDK features the application uses |
|
enable_metrics() on its own gives the always-on gauges and nothing more: no
per-command timing is recorded, and the latency histograms read zero. Ask for
the operational tier when you want them.
cluster.enable_metrics(MetricsPolicy()) # gauges only
cluster.enable_metrics(MetricsPolicy(operational_enabled=True)) # + histograms
The two extended tiers are independent — usage counters are recorded by this SDK rather than by the client core, and ignore the sampler.
Configuring collection¶
MetricsPolicy controls the histogram shape and how
much is recorded. The shape settings configure the operational tier and do
nothing while it is off:
from aerospike_sdk.metrics import LatencyUnit, MetricsPolicy, Sampler
# Millisecond view (the default): 7 buckets covering
# <=1, >1, >2, >4, >8, >16, >32 ms.
cluster.enable_metrics(MetricsPolicy(operational_enabled=True))
# Sub-millisecond resolution for fast clusters.
cluster.enable_metrics(MetricsPolicy(
operational_enabled=True,
latency_unit=LatencyUnit.MICROSECONDS,
latency_columns=18,
))
# Record ~10% of calls on a high-throughput deployment.
cluster.enable_metrics(MetricsPolicy(
operational_enabled=True,
sampler=Sampler.probability(0.1),
))
latency_shiftspaces the bucket boundaries: each boundary after the first multiplies by2**latency_shift, soshift=3gives<=1, >1, >8, >64, .... Bucket i holds samples in the range(2**i, 2**(i+1)], the same layout server-side latency tools report, so the two line up bucket for bucket.A fractional
samplerreduces how much is recorded, at the cost of tail fidelity. The decision is taken once per API call, before any retry, so a retried command is either fully recorded or not at all and the effective rate is the rate you set.Re-enabling with a changed latency unit or histogram shape discards the accumulated latency samples. Counters are always retained.
Reading the snapshot¶
The canonical detail is per-node and per-command-type. Each node snapshot
(and the cluster_aggregated roll-up) carries a latency histogram per
command category, ~20 lifecycle counters, and per-namespace detail:
from aerospike_sdk.metrics import CommandType
agg = snapshot.cluster_aggregated
# One command category, cluster-wide.
gets = agg.command_histogram(CommandType.GET)
print(f"{gets.count} reads, {gets.average:.0f} avg")
# The same command, narrowed to one namespace.
detail = agg.detailed_metric("prod-ns", CommandType.GET)
if detail is not None:
print(detail.latency.count, detail.bytes_received.count)
latency() derives the classic five-way
grouping (conn/read/write/batch/query) from those categories, and
to_canonical_dict() renders the
snapshot as the cross-client document for logging or shipping to an external
system.
Attributing a snapshot¶
Every snapshot names the application it came from, so a dashboard can group
client-side latency and errors the same way the server groups its own. The
app_id field is whichever of these applies first:
the identifier passed to
app_id(),the authenticated user, when the connection has one,
not-set.
It is never an empty string, so a consumer never needs a missing-value case.
The server’s own user-agent record uses the same field, but only the first two
of those steps agree with it: it carries an identifier you set, otherwise
not-set. The authenticated-user fallback is metrics-only, so a connection
that authenticates as reporting_svc without naming an application appears as
reporting_svc in snapshots and not-set in the server’s user-agent list. Set
app_id explicitly to make the two read alike.
Give distinct workloads distinct identifiers: app_id is the supported way to
separate them, since snapshots are not broken down by set.
The raw per-command tier¶
There are three levels of detail, and the two documented above are not the deepest. The canonical document is the cross-SDK shape: it merges the per-command histograms the client core records into the five latency groups and per-namespace counters. Underneath it sits everything core actually recorded — roughly ten times as many values.
Reach it through cluster_aggregated for the whole cluster, or through any
entry of nodes for one node; both expose the same methods:
from aerospike_sdk.metrics import CommandType
snapshot = cluster.metrics_snapshot()
agg = snapshot.cluster_aggregated
for namespace in agg.detailed_namespaces():
for command in (CommandType.GET, CommandType.OPERATE, CommandType.BATCH_READ):
detail = agg.detailed_metric(namespace, command)
if detail is None:
continue # that command was never issued
print(namespace, command,
detail.latency.count,
detail.latency.average,
detail.parsing.average,
detail.connection_aq.average,
detail.bytes_sent.average)
# The same, for a single node.
node = snapshot.nodes["10.0.0.1:3000"]
node.detailed_metric("prod-ns", CommandType.OPERATE)
detailed_metric returns None for a command the client never issued, so
check before use. Each CommandMetric carries five histograms, and every
histogram exposes count, min, max, sum, average, and buckets:
field |
what it measures |
|---|---|
|
round-trip time for that command |
|
time spent parsing the response |
|
time spent acquiring a connection |
|
request size |
|
response size |
Two of those exist nowhere else. parsing and connection_aq are absent
from the canonical document and from the log file — so client-side response
handling and connection-pool acquisition time are only visible at this tier.
If a read looks slow and the server disagrees, this is where to look.
Note
Commands are recorded as the client issues them, not as you named them.
session.upsert(...).put(...) records under CommandType.OPERATE, not
PUT, because that is the wire command it sends — so detailed_metric(ns, CommandType.PUT) returns None for a workload of upserts.
This tier is specific to this SDK and the client core beneath it. The
cross-SDK specification defines the canonical document, not this, so anything
built on detailed_metric is not portable to the other Aerospike clients and
may change shape as the core evolves. Prefer
to_canonical_dict() unless you need
detail it does not carry.
Feature usage counters¶
Separately from latency, the SDK can count which of its features an application uses — point versus batch versus query, AEL versus expression filters, transactions, background jobs. This is adoption insight rather than operational monitoring, so it is off by default and independent of the histograms:
cluster.enable_metrics(MetricsPolicy(usage_enabled=True))
# ... application traffic ...
snapshot = cluster.metrics_snapshot()
print(snapshot.usage)
# {'feature.api.deferred': 1042, 'feature.shape.point': 900,
# 'feature.shape.batch': 142, 'feature.filter.ael': 37}
Or from the configuration file, which is the same key the other Aerospike clients use:
system:
DEFAULT:
metrics:
enabled: true
extended:
usage:
enabled: true
Counters are cumulative for the life of the client and are never sampled. They are recorded in this SDK, so they count calls made through this API — traffic issued through the underlying client directly does not appear.
The execution_mode of a call is carried in the counter name rather than as
a separate dimension: feature.api.blocking, feature.api.deferred, and
feature.api.background. feature.transaction counts transactions opened,
once each however many calls run inside it; the calls themselves are counted
under their own shape and mode.
Three counters defined by the cross-SDK specification have no equivalent here
and are never emitted: feature.object_mapping.read,
feature.object_mapping.write, and feature.batch.mixed. This SDK has no
object-mapping API for them to count, so their absence is by definition
rather than an oversight.
Exporting snapshots¶
Rather than polling, let the client push a snapshot on an interval, either to
the built-in log file writer or to exporters the
application registers. An exporter implements a single method,
export(snapshot); it never sees the cluster or its lifecycle, and whatever
it writes to — a file, an HTTP client — it opens itself and the application
closes when done. The client never closes an exporter it did not create.
The async and sync clients take different protocols, because an exporter does I/O and the async client must not block its event loop on it:
from aerospike_sdk.metrics import AsyncMetricsExporter
class JsonExporter:
async def export(self, snapshot):
await self._post(snapshot.to_canonical_dict())
cluster.add_exporter(JsonExporter())
Implement MetricsExporter (plain def export) for the sync client and
AsyncMetricsExporter (async def export) for the async one. Exporters are
called in registration order with the same snapshot, which is an owned copy —
keeping it past the call is safe. One exporter raising never silences the
others: the error is logged and the rest still run. An exporter that fails
three exports in a row is suspended, then retried every tenth interval until
it succeeds, at which point its normal cadence resumes.
Disabling metrics and closing the cluster each push one last snapshot, so the
activity since the previous interval is not lost. It is taken before
collection stops. The sync client’s disable_metrics() returns once every
exporter has received it. The async client’s disable_metrics() is a plain
method that returns straight away, while the closing snapshot is delivered in
the background; await cluster.close() waits for that delivery, and
re-enabling holds the next export until it finishes, so an exporter never
sees two exports at once. Changing the policy with enable_metrics() while
metrics are on restarts the push without a closing snapshot.
A node that has left the cluster appears once under the snapshot’s
nodes_departed, same shape as nodes with its final counters, so an
exporter can flush that node’s series. Only the export push tracks
departures — a snapshot from cluster.metrics_snapshot() always carries an
empty nodes_departed.
Choosing who receives snapshots¶
metrics.exporter in the configuration file selects the recipients of each
interval’s snapshot. The value is case-insensitive:
|
Each interval goes to |
|---|---|
|
the built-in log file writer only, into |
|
the exporters registered with |
|
nobody; snapshots are still available by polling |
unset |
|
The modes do not combine: with exporter: file a registered exporter is not
called, and with exporter: custom no file is written. Since the default is
file, an application that registers its own exporter also selects custom,
in the configuration file or in code on the cluster definition:
from aerospike_sdk.metrics import MetricsExporterType
from aerospike_sdk.policy.system_settings import MetricsSettings, SystemSettings
definition = ClusterDefinition(host).with_system_settings(
SystemSettings(metrics=MetricsSettings(exporter=MetricsExporterType.CUSTOM))
)
to_canonical_dict() is the payload an
exporter should serialize: a stable snake_case document independent of how
the underlying client names its own fields.
Within each namespace object, each failed command is counted under exactly
one of timeouts, key_busy, record_too_big, device_overload or
errors; errors holds whatever has none of the four named causes. The
total failure count is therefore their sum:
namespace = document["nodes"][0]["namespaces"][0]
causes = ("errors", "timeouts", "key_busy", "record_too_big", "device_overload")
failures = sum(namespace[k] for k in causes)
These are server answers. A command that runs out of client-side deadline
never receives a result code, so it is in none of them. It is counted once,
cluster-wide, under command_timeout_client; command_timeout_server beside
it is the namespace timeouts summed, so the two read as a pair and never
overlap. See Known limitations.
Writing metrics to files¶
The built-in MetricsWriter writes
the line-oriented metrics log shared by the Aerospike clients, for existing log
shippers and tools such as asloglatency. It is the default recipient: set a
directory and the client writes there on every interval.
system:
DEFAULT:
metrics:
enabled: true
export_interval: 30s
exporter: file
report_dir: /var/log/aerospike/metrics
report_size_limit: 10mb
With no report_dir nothing is written. Files are named
metrics-<yyyyMMddHHmmss>.log; once one reaches report_size_limit the next
line starts a new file. The limit is 0 (never rotate, the default) or at least
1,000,000 bytes. Re-enabling with a different histogram shape also starts a new
file, so that each file’s header describes every line beneath it.
Every line begins with a local timestamp (yyyy-MM-dd HH:mm:ss.SSS). The
first line of a file is the header declaring the schema, and each later line
is one snapshot, positional rather than named:
header(5) cluster[cluster_name,client_type,client_version,app_id,labels[],cpu,mem,recover_queue_size,nodes_invalid,command_count,blocking_count,deferred_count,background_count,tran_count,command_retries,nodes[]] labels[name,value] nodes[name,address,port,conns_in_use,conns_in_pool,conns_opened,conns_closed,namespaces[]] namespaces[name,errors,timeouts,key_busy,bytes_in,bytes_out,latency[]] latency(MILLISECONDS,7,1)[type[l1,l2,l3...]]
client_typeispython-sdk.cpuandmemare this process’s CPU share and resident memory, sampled when the snapshot is built.cpuis CPU time consumed since the previous snapshot as a percentage of the wall time elapsed, so a busy process on several cores can read above 100.memis the resident set size in bytes: current on Linux, and the process’s peak on macOS, which is what the platform reports without a third-party dependency.command_countandcommand_retriesare the cluster counters described in Known limitations. They read 0 unless the operational tier is on.blocking_count,deferred_count,background_countandtran_countare thefeature.api.*andfeature.transactionusage counters. They read 0 unless the usage tier is on. The other feature counters are not written to the file; they reach a consumer through the canonical snapshot.Nodes that left the cluster since the previous line follow the live ones in the node list, with their final counters.
Each namespace carries all five latency types,
conn,write,read,batchandquery, in that order.
The file can also be written from code: construct the writer and register it
like any exporter, under custom mode. Its first export opens the file and
writes the header, and the application closes it:
from aerospike_sdk.metrics import MetricsWriter
exporter = MetricsWriter("/var/log/aerospike/metrics")
cluster.add_exporter(exporter)
...
exporter.close()
Warning
The header names the latency unit, but tools that predate microsecond buckets
read every histogram as milliseconds. With latency_unit: microseconds the
numbers are still correct but such a tool reads them as 1000× larger than they
are. Set it for file export only where whatever consumes the files reads the
unit; the canonical snapshot carries latency_unit explicitly.
Configuring metrics from a file¶
Everything above can come from the SDK configuration file instead of code, so collection can be turned on in a deployment without a rebuild:
system:
DEFAULT:
metrics:
enabled: true
labels:
owner: platform-team
extended:
operational:
enabled: true
latency_unit: microseconds
latency_columns: 18
latency_shift: 1
sampler:
range: 1000
threshold: 100
usage:
enabled: true
The file mirrors the tiers: metrics.enabled turns collection on at all, and
each extended tier has its own enabled beneath it. The histogram shape and
the sampler live inside metrics.extended.operational with the tier they
configure — at the metrics root they are reported as unrecognized, since
there is nothing there for them to shape.
A block that never sets enabled leaves collection as it is, so a file that
only tunes the histogram shape does not switch collection on by itself.
Changing enabled in the file takes effect on reload, without reconnecting.
While a loaded file sets metrics.enabled, the file owns that switch:
enable_metrics() and disable_metrics() raise AerospikeError with
PARAMETER_ERROR rather than make a change the next reload would undo. A
file that leaves enabled unset leaves the switch to code.
Known limitations¶
Client metrics are collected by the underlying client core, and several parts of the model it implements differ from the cross-SDK specification. These are current behavior, not bugs in configuration:
Area |
What to expect |
|---|---|
Two latency measures |
The per-namespace |
Error counters follow the histograms |
The operational tier is one switch: there is no way to record result codes and retry counters without also recording latency histograms. |
Command count scope |
The cluster |
Client-side timeouts |
A command that exhausts its client-side deadline or retry budget receives no result code, so it is outside the per-namespace counters: |
Latency unit in the log file |
Older consumers of the line format read latency as milliseconds regardless of the unit the header names. Microsecond buckets are written unchanged and will be misread by those tools. The canonical snapshot is unaffected — it carries |
Usage counter scope |
Usage counters are recorded in this SDK, so they cover calls made through this API only, and do not appear in the underlying client’s own snapshot. |
The canonical snapshot carries the log file’s cpu and mem values as
cpu_percent and memory_bytes.
What the latencies represent¶
Histograms measure client-core transaction latency: node selection, connection acquisition, serialization, socket I/O, parsing, and retries — what the caller waited for, per command. On the async surface, event-loop submission queuing ahead of command start is not included.