#http #server #metrics

metrics_server

A hassle-free, single-responsibility, safe HTTP/S server used to easily expose metrics in an application

18 releases (breaking)

0.13.0 Aug 28, 2023
0.12.0 Feb 23, 2023
0.11.0 Jan 3, 2023
0.10.1 Dec 27, 2022
0.1.1 Feb 28, 2022

#179 in HTTP server

Download history 3/week @ 2023-11-02 19/week @ 2023-11-09 2/week @ 2023-11-16 19/week @ 2023-11-23 56/week @ 2023-11-30 18/week @ 2023-12-14 36/week @ 2023-12-21 18/week @ 2024-01-04 22/week @ 2024-01-18 20/week @ 2024-01-25 19/week @ 2024-02-01 23/week @ 2024-02-08 214/week @ 2024-02-15

276 downloads per month

MIT license

17KB
186 lines

metrics_server

CI Version Docs License

Note: This lib's API should NOT currently be considered stable - it might change before v1.

A hassle-free, single-responsibility, safe HTTP/S server used to easily expose metrics in an application.

This crate provides a thread safe, minimalstic HTTP/S server used to buffer metrics and serve them via a standard /metrics endpoint. It's aim is to remove the boilerplate needed to create such simple mechanisms. It is currently somewhat oppinionated and naive in order to maintain little complexity.

Usage

Include the lib in your Cargo.toml dependencies:

[dependencies]
metrics_server = "0.13"

To enable TLS support, pass the optional feature flag:

[dependencies]
metrics_server = { version = "0.13", features = ["tls"] }

HTTP

use metrics_server::MetricsServer;

// Create a new HTTP server and start listening for requests in the background.
let server = MetricsServer::http("localhost:8001");

// Publish your application metrics.
let bytes = server.update("my_awesome_metric = 10".into());
assert_eq!(22, bytes);

// Stop the server.
server.stop().unwrap();

HTTPS

Note: there is currently no option to skip TLS cert verification.

use metrics_server::MetricsServer;

// Load TLS config.
let cert = include_bytes!("/path/to/cert.pem").to_vec();
let key = include_bytes!("/path/to/key.pem").to_vec();

// Create a new HTTPS server and start listening for requests in the background.
let server = MetricsServer::https("localhost:8443", cert, key);

// Publish your application metrics.
let bytes = server.update("my_awesome_metric = 10".into());
assert_eq!(22, bytes);

// Stop the server.
server.stop().unwrap();

Serve a custom URL

use metrics_server::MetricsServer;

// Create a new server and specify the URL path to serve.
let mut server = MetricsServer::new("localhost:8001", None, None);
server.serve_uri("/path/to/metrics");

// Publish your application metrics.
let bytes = server.update("my_awesome_metric = 10".into());
assert_eq!(22, bytes);

// Stop the server.
server.stop().unwrap();

For more comprehensive usage, see the included examples.

Dependencies

~0.8–12MB
~124K SLoC