Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

RustintermediatedevopsAI generated

Local Metrics Agent

Build a small monitoring agent in Rust that runs on a machine, measures its health every few seconds (CPU, memory, disk and network use, plus the busiest processes), and serves the numbers on a local HTTP endpoint in the Prometheus format, so a monitoring system can collect them. It uses Tokio to take measurements and answer requests at the same time, keeps the last hour of samples in memory for a quick summary, and never uses much CPU itself. You'll learn async Rust with Tokio, reading system information with the sysinfo crate, sharing data between tasks, serving HTTP with axum, the Prometheus text format, and testing.

Estimate
~11h
Steps
6
Completed by
0
Proposed by
codeseed.app

Cargo · Tokio

Project roadmap

  1. 01

    Define what the agent does

    ~1.5h

    Write README.md: which metrics are collected (CPU usage overall and per core, memory used and total, disk usage per mount, network bytes per interface, and the top 5 processes by CPU and memory), how often (a --interval option, default 5 seconds), the endpoints (/metrics in Prometheus format, /summary as JSON, /health), and that it listens on 127.0.0.1 only by default. Done when: README.md lists every metric and endpoint.

  2. 02

    Collect the metrics

    ~2.5h

    Create a project with tokio, sysinfo, axum and serde. Write a collector that refreshes sysinfo's System and returns a Snapshot struct with a timestamp. CPU usage needs two readings some time apart, so keep the System between samples. Run it on a tokio interval in a background task. Done when: the agent prints a sensible snapshot every interval.

  3. 03

    Store recent samples

    ~1.5h

    Keep the latest snapshot and a ring buffer (a VecDeque) of the last hour of samples, shared between the collector and the web handlers through Arc<RwLock<...>>. Keep the lock only briefly, cloning what's needed. Done when: the buffer never grows past an hour of samples.

  4. 04

    Serve the endpoints

    ~2h

    Serve /metrics with axum in the Prometheus text format: HELP and TYPE lines, metric names like host_cpu_usage_percent, and labels such as core, mount and interface, escaped correctly. Serve /summary with averages and peaks over 1, 5 and 15 minutes, and /health. Done when: curl localhost:9100/metrics shows valid Prometheus output that promtool check metrics accepts.

  5. 05

    Harden it

    ~1.5h

    Handle missing information gracefully (for example disks that disappear or processes that end during a sample), cap the number of labels, measure how long each collection takes and warn when it's slow, and shut down cleanly on Ctrl+C. Check the agent's own CPU and memory use and note them in README.md. Done when: the agent runs for an hour with low, stable resource use.

  6. 06

    Test it

    ~2h

    Put the collector behind a trait so tests can use fake snapshots. Test the Prometheus formatting (including label escaping), the ring buffer limit, the summary averages, and the endpoints with axum's test tools. Optionally, run Prometheus in Docker and scrape the agent. Done when: cargo test passes.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~11h · 6 steps

Tech stack

CargoTokio