https://github.com/igosuki/actix-web-metrics-mw
Generic middleware library for actix-web metrics aggregation, can send to various outlets.
https://github.com/igosuki/actix-web-metrics-mw
actix actix-web metrics rust statsd
Last synced: 2 months ago
JSON representation
Generic middleware library for actix-web metrics aggregation, can send to various outlets.
- Host: GitHub
- URL: https://github.com/igosuki/actix-web-metrics-mw
- Owner: Igosuki
- License: mit
- Created: 2019-08-07T15:44:09.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-24T16:16:01.000Z (over 4 years ago)
- Last Synced: 2025-04-13T12:18:43.425Z (2 months ago)
- Topics: actix, actix-web, metrics, rust, statsd
- Language: Rust
- Size: 338 KB
- Stars: 15
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# actix-web-metrics-mw
Generic middleware library for actix-web metrics aggregation, can send to various outlets.[](https://github.com/Igosuki/actix-web-metrics-mw/actions)
[](https://docs.rs/actix-web-metrics-mw)
[](https://crates.io/crates/actix-web-metrics-mw)
[](https://github.com/nlopes/actix-web-metrics-mw/blob/master/LICENSE)Metrics middleware instrumentation for [actix-web](https://github.com/actix/actix-web) using the [metrics-rs](https://crates.io/crates/metrics) crate .
By default two metrics are tracked (this assumes the namespace `actix_web_metrics_mw`):
Available exporters :
- Statsd : uses a statsd rust client that buffers metrics through UDP, dogstats format is supported for metric labelsDefault 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.## Dependencies
- actix 3
- futures 0.3
- metrics 0.12## Issues
Please feel free to submit issues for evolutions you feel are necessary.## Usage
First add `actix_web_metrics_mw` to your `Cargo.toml`:
```toml
[dependencies]
actix_web_metrics_mw = "0.2.0"
```You then instantiate the prometheus middleware and pass it to `.wrap()`:
```rust
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:
```shell
$ 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]"}
```## Custom metrics
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.
```rust
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(())
}
```### Live functional testing
Use the docker-compose file. Actual result :

### Special Thanks
- The middleware integration is influenced by the work in [nlopes/actix-web-prom](https://github.com/nlopes/actix-web-prom).