Monitoring Rust Applications with Prometheus
Professional
You can instrument your Rust application with the official Prometheus Rust client library and let the Glouton agent scrape its /metrics endpoint.
Install the client library
Section titled “Install the client library”Add the dependencies to your Cargo.toml:
[dependencies]prometheus-client = "0.22"tokio = { version = "1", features = ["full"] }axum = "0.7"Minimal example
Section titled “Minimal example”The snippet below defines a counter and exposes it on http://localhost:8080/metrics:
use axum::{routing::get, Router};use prometheus_client::encoding::text::encode;use prometheus_client::metrics::counter::Counter;use prometheus_client::registry::Registry;use std::sync::Arc;
#[tokio::main]async fn main() { let mut registry = Registry::default(); let requests_total = Counter::<u64>::default(); registry.register( "myapp_requests", "Total number of processed requests", requests_total.clone(), );
let registry = Arc::new(registry); let app = Router::new().route( "/metrics", get({ let registry = registry.clone(); move || async move { let mut buffer = String::new(); encode(&mut buffer, ®istry).unwrap(); buffer } }), );
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap(); axum::serve(listener, app).await.unwrap();}Scrape with Glouton
Section titled “Scrape with Glouton”Configure Glouton to scrape your application by adding the following to /etc/glouton/conf.d/50-prometheus-metrics.conf:
metric: prometheus: targets: - url: "http://localhost:8080/metrics" name: "my_rust_application" allow_metrics: - "myapp_requests_total"Restart the agent to apply the configuration. See Prometheus Monitoring for the full list of options, including Docker label auto-discovery and Kubernetes annotations.