Skip to content

Getting Started

1. Start the stack

cd src/metrics
deploy/scripts/up.sh

This starts two relays, three aggregators, each with its own Redis and TimescaleDB, and a signing agent for a node.

2. Open a Python shell in the network

The services are reachable inside the Docker network only.

docker run --rm -it --network metrics_default --env-file deploy/.state/secrets.env --user "$(id -u):$(id -g)" metrics-python:dev python

3. Register the node

import os, httpx
admin = {"Authorization": "Bearer " + os.environ["MANAGEMENT_TOKEN"]}
node = {"Authorization": "Bearer " + os.environ["NODE_AGENT_TOKEN"]}
me = httpx.get("http://node-agent:8080/v1/identity", headers=node).json()["pubkey"]
trusted = []
for i in (1, 2, 3):
    httpx.put(f"http://aggregator-{i}:8080/v1/nodes/{me}", headers=admin, json={"node_id": "node-1", "cluster_id": "cluster-1"}).raise_for_status()
    trusted.append(httpx.get(f"http://aggregator-{i}:8080/v1/identity", headers=admin).json()["pubkey"])

4. Write metrics

from metrics_sdk import MetricsWriter
w = MetricsWriter("/tmp/metrics", "node-1", "cluster-1", "http://node-agent:8080", os.environ["NODE_AGENT_TOKEN"], k=3, trusted_l2=trusted, batch_seconds=1).start()
w.write("gpu_utilization", 0.75, block_id="block-1", unit="ratio")
w.flush(30), w.stats()

5. Query

Values appear once their window closes:

httpx.get("http://aggregator-1:8080/v1/query", headers=admin, params={"scope": "block", "metric": "gpu_utilization"}).json()
[{"scope": "block", "metric": "gpu_utilization", "count": 1, "sum": 0.75, "min": 0.75, "max": 0.75, "last": 0.75, "block_id": "block-1"}]

Some fields are omitted here.

6. Stop

Close the writer with w.close(), leave the shell, then:

deploy/scripts/down.sh