# Expose metrics behind hostname subpath

**URL:** <https://community.traefik.io/t/expose-metrics-behind-hostname-subpath/24414>\
**Category:** Traefik v3 (latest)\
**Tags:** metrics\
**Created:** [September 13, 2024, 4:25am UTC](https://community.traefik.io/t/expose-metrics-behind-hostname-subpath/24414 "2024-09-13T04:25:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![outranker](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/outranker/32/9306_2.png) [@outranker](https://community.traefik.io/u/outranker)\
**Post date:** [September 13, 2024, 4:25am UTC](https://community.traefik.io/t/expose-metrics-behind-hostname-subpath/24414/1 "2024-09-13T04:25:32Z")

</div>

I have setup traefik v3 and have a couple of services sitting behind traefik. Everything is working fine so far. But I am having difficulty setting up metrics and exposing it behind a domain subpath.  
Right now I got my own domain and the services are exposed via a single domain such as requests to [mydomain.com](http://mydomain.com) goes to service 1 and requests to [mydomain.com/app](http://mydomain.com/app) goes to service 2 (app service) and [mydomain.com/api](http://mydomain.com/api) goes to service 3 (api service). Now what I would like to do is requests to [mydomain.com/metrics](http://mydomain.com/metrics) should go to traefik metrics. Also I would like to setup basic auth for this subpath. My prometheus is located at another remote server hence the need to expose the metrics.

So far this is what I came up and can access the metrics via localhost:8080/metrics.  
This is my traefik.yml

```auto
api:
  insecure: true

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
  websecure:
    address: ":443"
  metrics:
    address: ":8081"

metrics:
  prometheus:
    addEntryPointsLabels: true
    addRoutersLabels: true
    addServicesLabels: true
    entryPoint: metrics

log:
  level: DEBUG

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"

certificatesResolvers:
  letsEncrypt:
    acme:
      email: myemail@email.com
      storage: /tls/acme.json
      httpChallenge:
        entryPoint: web

http:
  middlewares:
    compressionHeaders:
      headers:
        customRequestHeaders:
          Accept-Encoding: "gzip"
    mycompress:
      compress: 
        defaultEncoding: gzip
        minResponseBodyBytes: 0
    

```

This is my compose file for traefik

```auto
services:
  traefik:
    container_name: traefik
    image: traefik:3.1
    ports:
      - "80:80"
      - "8080:8080"
      - "8081:8081"
      - "443:443"
    restart: unless-stopped
    volumes:
      - ./traefik.yml:/traefik.yml:ro
      - /var/run/docker.sock:/var/run/docker.sock
      - tls:/tls
    networks:
      - traefik
      - monitoring
    labels:
      - "traefik.http.routers.traefik-metrics.rule=(Host(`mydomain.com`) || Host(`www.mydomain.com`)) && PathPrefix(`/traefik`)"
      - "traefik.http.routers.traefik-metrics.entrypoints=websecure"
      - "traefik.http.routers.traefik-metrics.service=prometheus@internal"
      - "traefik.http.middlewares.strippathprefixmetrics.stripprefix.prefixes=/traefik"
      - "traefik.http.middlewares.metricsauth.basicauth.users=user:$$password"
      - "traefik.http.routers.traefik-metrics.middlewares=metricsauth,strippathprefixmetrics"
volumes:
  tls:
networks:
  traefik:
    name: traefik
    external: true
  monitoring:
    name: monitoring
    external: true

```

---

<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:** [September 13, 2024, 4:47am UTC](https://community.traefik.io/t/expose-metrics-behind-hostname-subpath/24414/2 "2024-09-13T04:47:47Z")

</div>

Check metrics `manualRouting` in [doc](https://doc.traefik.io/traefik/observability/metrics/prometheus/#manualrouting).

---

<div class="post-metadata">

**Author:** ![outranker](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/outranker/32/9306_2.png) [@outranker](https://community.traefik.io/u/outranker)\
**Post date:** [September 13, 2024, 6:31am UTC](https://community.traefik.io/t/expose-metrics-behind-hostname-subpath/24414/3 "2024-09-13T06:31:20Z")

</div>

Thanks for the response. The url that you provided only talks about a single option for metrics. However, I figured out the solution

---

<div class="post-metadata">

**Author:** ![outranker](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/outranker/32/9306_2.png) [@outranker](https://community.traefik.io/u/outranker)\
**Post date:** [September 13, 2024, 6:37am UTC](https://community.traefik.io/t/expose-metrics-behind-hostname-subpath/24414/4 "2024-09-13T06:37:29Z")

</div>

here is the solution. i had to separate my config files into static and dynamic. i added a volume for my traefik compose file:

```auto
services:
  traefik:
    container_name: traefik
    image: traefik:3.1
    ports:
      - "80:80"
      - "8080:8080"
      - "443:443"
    restart: unless-stopped
    volumes:
      - ./traefik.yml:/traefik.yml:ro
      - ./provider:/provider # <- this is the newly added bind mount
      - /var/run/docker.sock:/var/run/docker.sock
      - tls:/tls
    networks:
      - traefik
      - monitoring
volumes:
  tls:
networks:
  traefik:
    name: traefik
    external: true
  monitoring:
    name: monitoring
    external: true

```

this is my static config file. nothing fancy

```auto
api:
  debug: true
  dashboard: true
entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
  websecure:
    address: ":443"
providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
  file:
    directory: "/provider" # <- added new provider for my dynamic config
    watch: true

metrics:
  prometheus:
    addEntryPointsLabels: true
    addRoutersLabels: true
    addServicesLabels: true
    manualRouting: true # <- i had to enable this one as well

log:
  level: DEBUG

certificatesResolvers:
  letsEncrypt:
    acme:
      email: myemail@email.com
      storage: /tls/acme.json
      httpChallenge:
        entryPoint: web

```

and this is the main one. dynamic config file

```auto

http:
  middlewares:
    compressionHeaders:
      headers:
        customRequestHeaders:
          Accept-Encoding: "gzip"
    mycompress:
      compress: 
        defaultEncoding: gzip
        minResponseBodyBytes: 0
    treafikauth:
      basicAuth:
        users:
          - "user1:$password"
    treafikmetricsauth:
      basicAuth:
        users:
          - "user2:$password"
  routers:
    api:
      entryPoints:
        - websecure
      rule: "Host(`traefik.mydomain.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))"
      service: api@internal
      tls:
        certResolver: letsEncrypt
      middlewares:
        - treafikauth
      
    metrics:
      entryPoints:
        - websecure
      rule: "Host(`traefik.mydomain.com`) && PathPrefix(`/metrics`)"
      service: prometheus@internal
      tls:
        certResolver: letsEncrypt
      middlewares:
        - treafikmetricsauth

```

however, this was not what i wanted originally. i wanted to serve metrics under a subpath but a subdomain seemed to take less hassle. leaving this here for anyone who wants to go down the rabbithole

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd81ebdb578656e76e56ff3cc3eed021d3ba132d.png) [@system](https://community.traefik.io/u/system)\
**Post date:** [September 16, 2024, 6:38am UTC](https://community.traefik.io/t/expose-metrics-behind-hostname-subpath/24414/5 "2024-09-16T06:38:03Z")

</div>

Closed on September 16, 2024, 6:38am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
