{"id":17215393,"url":"https://github.com/igosuki/actix-web-metrics-mw","last_synced_at":"2026-01-24T15:07:02.285Z","repository":{"id":56696226,"uuid":"201080043","full_name":"Igosuki/actix-web-metrics-mw","owner":"Igosuki","description":"Generic middleware library for actix-web metrics aggregation, can send to various outlets.","archived":false,"fork":false,"pushed_at":"2020-10-24T16:16:01.000Z","size":346,"stargazers_count":15,"open_issues_count":3,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-13T12:18:43.425Z","etag":null,"topics":["actix","actix-web","metrics","rust","statsd"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Igosuki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-07T15:44:09.000Z","updated_at":"2024-03-25T09:18:06.000Z","dependencies_parsed_at":"2022-08-15T23:31:04.658Z","dependency_job_id":null,"html_url":"https://github.com/Igosuki/actix-web-metrics-mw","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Igosuki%2Factix-web-metrics-mw","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Igosuki%2Factix-web-metrics-mw/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Igosuki%2Factix-web-metrics-mw/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Igosuki%2Factix-web-metrics-mw/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Igosuki","download_url":"https://codeload.github.com/Igosuki/actix-web-metrics-mw/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248790835,"owners_count":21162097,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actix","actix-web","metrics","rust","statsd"],"created_at":"2024-10-15T03:24:16.378Z","updated_at":"2026-01-24T15:07:02.022Z","avatar_url":"https://github.com/Igosuki.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# actix-web-metrics-mw\nGeneric middleware library for actix-web metrics aggregation, can send to various outlets.\n\n[![Build Status](https://github.com/Igosuki/actix-web-metrics-mw/workflows/Standard%20matrix%20build/badge.svg)](https://github.com/Igosuki/actix-web-metrics-mw/actions)\n[![docs.rs](https://docs.rs/actix-web-metrics-mw/badge.svg)](https://docs.rs/actix-web-metrics-mw)\n[![crates.io](https://img.shields.io/crates/v/actix-web-metrics-mw.svg)](https://crates.io/crates/actix-web-metrics-mw)\n[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nlopes/actix-web-metrics-mw/blob/master/LICENSE)\n\nMetrics middleware instrumentation for [actix-web](https://github.com/actix/actix-web) using the [metrics-rs](https://crates.io/crates/metrics) crate .\n\nBy default two metrics are tracked (this assumes the namespace `actix_web_metrics_mw`):\n\nAvailable exporters :\n  - Statsd : uses a statsd rust client that buffers metrics through UDP, dogstats format is supported for metric labels\n\nDefault metrics :\n  - `http_requests_total` (labels: endpoint, method, status): request counter for each\n   endpoint and method.\n  - `http_requests_duration` (labels: endpoint, method,\n   status): histogram of request durations for each endpoint.\n\n## Dependencies\n\n- actix 3\n- futures 0.3\n- metrics 0.12\n\n## Issues\nPlease feel free to submit issues for evolutions you feel are necessary.\n\n## Usage\n\nFirst add `actix_web_metrics_mw` to your `Cargo.toml`:\n\n```toml\n[dependencies]\nactix_web_metrics_mw = \"0.2.0\"\n```\n\nYou then instantiate the prometheus middleware and pass it to `.wrap()`:\n\n```rust\nuse actix_web::{web, App, HttpResponse, HttpServer};\nuse actix_web_metrics_mw::Metrics;\n\nfn health() -\u003e HttpResponse {\n    HttpResponse::Ok().finish()\n}\n\nfn main() -\u003e std::io::Result\u003c()\u003e {\n    let metrics = Metrics::new(\"/metrics\", \"actix_web_mw_test\");\n    HttpServer::new(move || {\n        App::new()\n            .wrap(metrics.clone())\n            .service(web::resource(\"/health\").to(health))\n    })\n    .bind(\"127.0.0.1:8080\")?\n    .run();\n    Ok(())\n}\n```\n\nUsing the above as an example, a few things are worth mentioning:\n - `api` is the metrics namespace\n - `/metrics` will be auto exposed (GET requests only)\n\nA call to the /metrics endpoint will expose your metrics:\n\n```shell\n$ curl http://localhost:8080/metrics\n{\"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]\"}\n```\n\n## Custom metrics\n\nYou can instantiate `Metrics` and then use its sink to register your custom\nmetric.\n\nYou can also use the metrics library macros or the entire metrics runtime to add new metrics and labels as suit your needs.\n\n```rust\nuse actix_web::{web, App, HttpResponse, HttpServer};\nuse actix_web_metrics_mw::Metrics;\n\nfn health() -\u003e HttpResponse {\n    counter!(\"endpoint.method.status\", 1);\n    HttpResponse::Ok().finish()\n}\n\nfn main() -\u003e std::io::Result\u003c()\u003e {\n    let metrics = Metrics::new(\"/metrics\", \"actix_web_mw_test\");\n\n    HttpServer::new(move || {\n        App::new()\n            .wrap(metrics.clone())\n            .service(web::resource(\"/health\").to(health))\n    })\n    .bind(\"127.0.0.1:8080\")?\n    .run();\n    Ok(())\n}\n```\n\n### Live functional testing\n\nUse the docker-compose file. Actual result :\n\n![Alt text](/screenshot.png \"Tag based metrics in influx and grafana\")\n\n### Special Thanks\n\n- The middleware integration is influenced by the work in [nlopes/actix-web-prom](https://github.com/nlopes/actix-web-prom).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figosuki%2Factix-web-metrics-mw","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Figosuki%2Factix-web-metrics-mw","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figosuki%2Factix-web-metrics-mw/lists"}