Crates.io | actix_web_metrics_mw |
lib.rs | actix_web_metrics_mw |
version | 0.3.1 |
source | src |
created_at | 2019-08-23 09:05:54.034012 |
updated_at | 2020-10-24 16:07:17.09002 |
description | Actix middleware library for metrics |
homepage | https://github.com/Igosuki/actix-web-metrics-mw |
repository | https://github.com/Igosuki/actix-web-metrics-mw |
max_upload_size | |
id | 159151 |
size | 473,515 |
Generic middleware library for actix-web metrics aggregation, can send to various outlets.
![Build Status](https://github.com/Igosuki/actix-web-metrics-mw/workflows/build.yml/badge.svg
Metrics middleware instrumentation for actix-web using the metrics-rs crate .
By default two metrics are tracked (this assumes the namespace actix_web_metrics_mw
):
Available exporters :
Default metrics :
http_requests_total
(labels: endpoint, method, status): request counter for each
endpoint and method.http_requests_duration
(labels: endpoint, method,
status): histogram of request durations for each endpoint.Please feel free to submit issues for evolutions you feel are necessary.
First add actix_web_metrics_mw
to your Cargo.toml
:
[dependencies]
actix_web_metrics_mw = "0.2.0"
You then instantiate the prometheus middleware and pass it to .wrap()
:
use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web_metrics_mw::Metrics;
fn health() -> HttpResponse {
HttpResponse::Ok().finish()
}
fn main() -> std::io::Result<()> {
let metrics = Metrics::new("/metrics", "actix_web_mw_test");
HttpServer::new(move || {
App::new()
.wrap(metrics.clone())
.service(web::resource("/health").to(health))
})
.bind("127.0.0.1:8080")?
.run();
Ok(())
}
Using the above as an example, a few things are worth mentioning:
api
is the metrics namespace/metrics
will be auto exposed (GET requests only)A call to the /metrics endpoint will expose your metrics:
$ curl http://localhost:8080/metrics
{"http_requests_total":"1570","http_requests_duration":"[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"}
You can instantiate Metrics
and then use its sink to register your custom
metric.
You can also use the metrics library macros or the entire metrics runtime to add new metrics and labels as suit your needs.
use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web_metrics_mw::Metrics;
fn health() -> HttpResponse {
counter!("endpoint.method.status", 1);
HttpResponse::Ok().finish()
}
fn main() -> std::io::Result<()> {
let metrics = Metrics::new("/metrics", "actix_web_mw_test");
HttpServer::new(move || {
App::new()
.wrap(metrics.clone())
.service(web::resource("/health").to(health))
})
.bind("127.0.0.1:8080")?
.run();
Ok(())
}
Use the docker-compose file. Actual result :