I would like to check the response time by RequestPath. Thanks!
IMHO this is not possible by default, check the doc.
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.
Thanks! I will try to have different route then. Weird that this is not in the built-in metrics
@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
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.
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.
>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
>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
>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
>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
>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