nlopes / actix-web-prom

Actix-web middleware to expose Prometheus metrics
MIT License
92 stars 56 forks source link

Option to map "path" to something else #20

Closed kodieg closed 3 years ago

kodieg commented 4 years ago

I have an endpoint that is parametrized with and entity id

#[get("/get/{id}")]
async fn get(...) -> impl Responder {}

Using PrometheusMetric middleware I get different metric for each id (because thay make different path). Would be great to have an option to somehow map real path to /get/{id} or something. I'm not sure if it's feasible to automatically find mapping provided in get/post/... macros. However, maybe it would be enough to add option to provide custom function to modify path for purposes of storing metrics.

Maybe something like (or something similar) could work:

   fn map_fn<'a>(path: &'a str) -> Cow<'a, str> {
       ...
   }
   let prometheus = PrometheusMetrics::new("api", Some("/metrics"), None, map_fn);

Is there an option to add such functionality? Would that make sens or is there any better option?

nazar-pc commented 4 years ago

Created upstream ticket to expose API necessary to implement essential fix for this efficiently: https://github.com/actix/actix-web/issues/1565

nlopes commented 4 years ago

Thank you for doing this @nazar-pc.

nazar-pc commented 4 years ago

Implemented upstream and should be a part of some future release of actix-web

nazar-pc commented 4 years ago

I think with dependency on actix-web 3.0 it makes sense to switch to new API from https://github.com/actix/actix-web/issues/1565