Skip to content
v1
Docs

Python SDK

Custom metrics from Python

You need a metric source token (WT15_METRICS_TOKEN, wt15ms_…): see metric sources and the ingest API it wraps.

from datetime import datetime, timezone
result = client.ingest.metrics.push([
{"metric": "orders.completed", "metric_type": "count", "value": 1},
{"metric": "queue.depth", "metric_type": "level", "value": 42, "recorded_at": datetime.now(timezone.utc)},
])
result.accepted # True when any chunk was accepted
result.count # points stored
result.rejected # every rejection reason, 0 where none
  • A point that names a metric carries metric_type, count or level; one by metric_uuid may omit it. A value must be a finite number.
  • The SDK stamps no time. A point without recorded_at is stamped by the server; a timezone-aware datetime becomes epoch milliseconds.
  • push() cuts the points into chunks of at most 1,000 points and 1,000,000 bytes, gzips each and sends them in order. A chunk that fails raises with partial_result holding the chunks before it.
  • Keys: each chunk is sent under an Idempotency-Key. A chunk holding a point older than 30 days is keyed by its content, so a retry is the same batch; any other chunk gets a per-call nonce. Pass idempotency_key= (1 to 100 visible ASCII characters) to choose your own: one chunk uses it as given, several use <key>/0, <key>/1, …. Never reuse your own key for other points.
  • client.ingest.metrics.list() returns the source’s metrics with their status, the most it may hold and how many it holds. On AsyncClient both are awaited.
from watchtower15 import Client
from watchtower15.metrics import Aggregator
client = Client.from_environment()
metrics = Aggregator(
client,
allow_list={"orders.created": "count", "queue.depth": "level"},
heartbeat="app.alive",
)
metrics.increment("orders.created") # microseconds, never raises, never sends from here
metrics.gauge("queue.depth", 812)
# at shutdown
metrics.close()

increment() and gauge() only update memory. Buckets of 60 seconds are closed 5 seconds after their end and sent by a flush: one push per closed bucket, oldest first, without retries, within max_requests (10) and 90 seconds a run. An allow-list names every key you count with its type, count or level: a key not listed is counted not_allowed and nothing is stored; each entry and the heartbeat key is checked at construction and a bad one is listed in the report’s config_problems. enabled=False stores and sends nothing. The aggregator needs a sync Client: its flusher is a thread, and an AsyncClient is refused.

  • The thread. The first increment() or gauge() starts a daemon thread (flush_interval, 10 seconds by default, None for none) that sends closed buckets, and registers an atexit hook that sends what is left. Nothing starts at import or at construction, so an aggregator you build and never use costs nothing. A daemon thread never keeps the process alive.
  • flush(final=False) returns a FlushReport (status, buckets_sent, requests, points, rejected, stop_reason, errors, config_problems), never raises. final=True sends the open bucket too. flush_if_due() flushes when a minute has passed, for a process with no thread.
  • close(timeout=10.0) stops the thread and sends every bucket within its timeout, returning the report.
  • Failures are counted, never raised. A 401, a bad configuration, a redirect or a transport failure ends the run with the matching stop_reason and keeps the bucket for the next run.
  • One store per process. Custom metrics keep every sample, so two processes’ points both count; there is nothing to share. A forked child starts with empty buckets of its own: see servers, workers and serverless.

A function may freeze before the next minute: send what you counted before the invocation answers, with flush(final=True). See servers, workers and serverless.