# Is it possible to have RequestPath as a label in the prometheus metrics?

**URL:** <https://community.traefik.io/t/is-it-possible-to-have-requestpath-as-a-label-in-the-prometheus-metrics/19204>\
**Category:** Traefik v2\
**Tags:** metrics\
**Created:** [July 11, 2023, 3:09pm UTC](https://community.traefik.io/t/is-it-possible-to-have-requestpath-as-a-label-in-the-prometheus-metrics/19204 "2023-07-11T15:09:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![z16z](https://avatars.discourse-cdn.com/v4/letter/z/a587f6/32.png) [@z16z](https://community.traefik.io/u/z16z)\
**Post date:** [July 11, 2023, 3:09pm UTC](https://community.traefik.io/t/is-it-possible-to-have-requestpath-as-a-label-in-the-prometheus-metrics/19204/1 "2023-07-11T15:09:38Z")

</div>

I would like to check the response time by RequestPath. Thanks!

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [July 11, 2023, 4:47pm UTC](https://community.traefik.io/t/is-it-possible-to-have-requestpath-as-a-label-in-the-prometheus-metrics/19204/2 "2023-07-11T16:47:45Z")

</div>

IMHO this is not possible by default, check the [doc](https://doc.traefik.io/traefik/observability/metrics/prometheus/).

You could try to find a middleware that places the path in a header and then add that header to the metrics. But I am not sure if middlewares are processed before metrics.

If you want to measure only some specific routes, you could just duplicate the router, use `Host() && PathPrefix()` (this rule is longer, therefore has a higher priority and would be used first if matched) and then add the router name to the metrics.

---

<div class="post-metadata">

**Author:** ![z16z](https://avatars.discourse-cdn.com/v4/letter/z/a587f6/32.png) [@z16z](https://community.traefik.io/u/z16z)\
**Post date:** [July 20, 2023, 7:31pm UTC](https://community.traefik.io/t/is-it-possible-to-have-requestpath-as-a-label-in-the-prometheus-metrics/19204/3 "2023-07-20T19:31:54Z")

</div>

Thanks! I will try to have different route then. Weird that this is not in the built-in metrics 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![kchristidis](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kchristidis](https://community.traefik.io/u/kchristidis)\
**Post date:** [July 25, 2024, 12:42pm UTC](https://community.traefik.io/t/is-it-possible-to-have-requestpath-as-a-label-in-the-prometheus-metrics/19204/4 "2024-07-25T12:42:12Z")

</div>

@bluepuma77 this was helpful.

I have managed to get it done using some of the official middlewares. Indeed middlewares are processed before the metrics, which is good.

I have used

> **[Traefik ReplacePathRegex Documentation - Traefik](https://doc.traefik.io/traefik/middlewares/http/replacepathregex/)**
>
> In Traefik Proxy's HTTP middleware, ReplacePathRegex updates paths before forwarding requests, using a regex. Read the technical documentation.

This middleware performs path overwrites and preserves the original path to a header called X-Replaced-Path. I used this middleware, not for its replacement features but rather to have it write the original path to a header.

> **[Traefik Prometheus Documentation - Traefik](https://doc.traefik.io/traefik/observability/metrics/prometheus/#headerlabels)**
>
> Traefik supports several metrics backends, including Prometheus. Learn how to implement it for observability in Traefik Proxy. Read the technical documentation.

This is a metrics exporter configuration option in order to fetch the X-Replaced-Path as an exporter label.

* * *

Here's a working config using Traefik on Docker.

```auto
>cat traefik.yml
entryPoints:
  web:
    address: ":80"
api:
  insecure: true

log:
  level: "DEBUG"

accessLog: {}

providers:
  docker: {}

metrics:
  prometheus:
    buckets:
      - 0.1
      - 0.3
      - 1.2
      - 5.0
    addEntryPointsLabels: true
    addServicesLabels: true
    addRoutersLabels: true
    headerLabels:
      useragent: "User-Agent"
      xforwardedhost: "X-Forwarded-Host"
      xrequestpath: "X-Replaced-Path"

```

Run traefik

```auto
>docker rm -f traefik; docker run -d -p 80:80 -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v $(pwd)/traefik.yml:/etc/traefik/traefik.yml --name traefik traefik:v2.10; docker logs -f traefik

```

Run a whoami service

```auto
>docker run -d \
--label "traefik.http.routers.whoami.rule=Host(\`whoami.localhost\`)" \
--label "traefik.http.routers.whoami.middlewares=replacepathRegex@docker" \
--label "traefik.http.services.whoami.loadbalancer.server.port=80" \
--label "traefik.http.middlewares.replacepathRegex.replacepathregex.regex=^/(.*)" \
--label "traefik.http.middlewares.replacepathRegex.replacepathregex.replacement=/\$1" \
--name whoami traefik/whoami

```

make some calls to the service

```auto
>grep whoami /etc/hosts
127.0.0.1 whoami.localhost

>curl "http://whoami.localhost/"
>curl "http://whoami.localhost/api"
>curl "http://whoami.localhost/health"
>curl "http://whoami.localhost/bench"
>curl "http://whoami.localhost/data?size=1&unit=KB"
>curl "http://whoami.localhost/data?size=2&unit=KB"
>curl "http://whoami.localhost/bench?wait=2s"

```

Monitor traefik metrics

```auto
>curl -s http://localhost:8080/metrics | grep traefik_router_requests_total.*whoami@docker
traefik_router_requests_total{code="200",method="GET",protocol="http",router="whoami@docker",service="whoami@docker",useragent="curl/7.81.0",xforwardedhost="whoami.localhost",xrequestpath="/"} 5
traefik_router_requests_total{code="200",method="GET",protocol="http",router="whoami@docker",service="whoami@docker",useragent="curl/7.81.0",xforwardedhost="whoami.localhost",xrequestpath="/api"} 6
traefik_router_requests_total{code="200",method="GET",protocol="http",router="whoami@docker",service="whoami@docker",useragent="curl/7.81.0",xforwardedhost="whoami.localhost",xrequestpath="/bench"} 7
traefik_router_requests_total{code="200",method="GET",protocol="http",router="whoami@docker",service="whoami@docker",useragent="curl/7.81.0",xforwardedhost="whoami.localhost",xrequestpath="/data"} 3
traefik_router_requests_total{code="200",method="GET",protocol="http",router="whoami@docker",service="whoami@docker",useragent="curl/7.81.0",xforwardedhost="whoami.localhost",xrequestpath="/health"} 1

```
