Prometheus has rightfully become an industry standard for collecting metrics. Learn how to collect system and custom Prometheus metrics in a Rust web service.

Prometheus is an open-source tool for collecting, analyzing, and alerting on time series data. It’s widely used and highly regarded in the DevOps community.

In this tutorial, we’ll build a warp web service in Rust, which collects some interesting metrics using Prometheus.

We’ll show you how to add this metrics collection capability to a Rust web service and then how to get those metrics into Prometheus.

Setup

To follow along, all you need is a reasonably recent Rust installation (1.39+) and a way to start a local Prometheus instance — for example, with Docker.

First, create a new Rust project:

cargo new rust-web-prometheus-example
cd rust-web-prometheus-example

Next, edit the Cargo.toml file and add the dependencies you’ll need.

[dependencies]
tokio = { version = "0.2", features = ["macros", "rt-threaded", "time"] }
warp = "0.2"
prometheus = { version = "0.9", features = ["process"] }
lazy_static = "1.4"
futures = { version = "0.3", default-features = false }
rand = "0.7"

Besides the prometheus crate, which we’ll use to record metrics, we have the warp web framework, the tokio async runtime, and rand for generating some random metrics data.

Since we’re also going to implement a small websockets server here with warp, we’ll need the futures crate as well. The lazy_static crate will be used to initialize the metrics collectors and to make them globally available.

#rust #web-development #developer

Using Prometheus Metrics in a Rust Web Service
8.30 GEEK