Writing Metrics¶
Writer¶
from metrics_sdk import MetricsWriter
with MetricsWriter("/data/metrics", "node-1", "cluster-1", "http://agent:8080", token,
k=3, trusted_clusters=[controller_key]) as w:
w.write("tokens_total", 512, block_id="llm-1", kind="counter", labels={"model": "a"})
| Argument | Default | Meaning |
|---|---|---|
path |
required | Directory of the local store |
node_id, cluster_id |
required | Who writes |
agent_url, agent_token |
required | Signing agent |
k |
3 | Number of aggregators to write to |
batch_size, batch_seconds |
1000, 5 | A batch is created when either is reached. The size is at most 5000 |
queue_size |
100000 | Metrics held in memory |
max_stored |
1000000 | Metrics held on disk |
packet_ttl |
3000 | Seconds after which undelivered packets expire |
health_seconds, failures |
15, 2 | Health check interval, and failures before replacement |
trusted_l2, trusted_clusters, trust_any |
none | Trust. One is required |
policy, choose |
none | Selection policy |
write¶
write(name, value, block_id="", unit="", labels=None, kind="gauge", ts_ms=None) never blocks. It returns False if the queue is full, and raises ValueError for invalid metrics.
| Field | Limit |
|---|---|
name |
Letters, digits and _ . : / -, at most 200 characters |
value |
Finite, magnitude at most 1e150 |
unit |
32 bytes |
labels |
16. Names of 64 characters, values of 128 bytes |
Delivery¶
Metrics go to a local database, are sealed into signed packets, and are sent to all selected aggregators. Failed sends are repeated with increasing delay. When an aggregator is replaced, undelivered packets go to its replacement. Packets survive restarts of the process.
flush(timeout) sends everything now and returns whether it was delivered. close() flushes and stops. stats() reports the selected aggregators, sent packets and dropped metrics.