Prometheus metrics of Traefik

Traefik can be configured to give some metrics for Prometheus. Learn how here.

When Traefik is up and running and your routers/services are configured, it is important to monitor them. To do it, I’m going to set up Traefik to be scraped by Prometheus, among other things.

Goal: Configure Traefik and Prometheus

Environment: Debian 13, Docker 28.x, docker compose (plugin) 2.32.x, Traefik 3.6.

Execution context:

bash
jho@vmi866042:/opt/docker/dc$ tree
.
├── conf
│   ├── acme.json
│   ├── traefik.yml
│   ├── traefikdynamic
│   │   ├── dynamic.yml
├── docker-compose.yml
└── logs
    ├── traefikAccess.log
    ├── traefik.log

Result of the “tree” command in the /opt/docker/dc folder

  • path where are every folder and files : /opt/docker/dc
  • path of the principal configuration file for Traefik : /opt/docker/dc/conf/traefik.yml
  • folder where are every dynamic configuration files : /opt/docker/dc/conf/traefikdynamic
  • path of the file which is used to store SSL certificates for let’s encrypt (or other provider) : /opt/docker/dc/conf/acme.json
  • folder to store logs : /opt/docker/dc/logs/

This post is inspired by the official documentation . Traefik supports several monitoring backends: Datadog, StatsD, Prometheus, InfluxDB v2, OpenTelemetry and more. Metrics give you some information in real-time, like stats about configuration reloading, number of entry requests in HTTP or HTTPS, number of redirections, number of open connections…

Traefik configuration#

I will use Prometheus to scrape Traefik metrics and store them locally. To do it, the main configuration file traefik.yml needs to be updated with few options. In the entry points block, insert these lines : (port number can be changed)

yaml
entryPoints:
  metrics:
    address: ":9090"

At the end of the file, add this bloc (YAML format) :

yaml
metrics:
  prometheus:
    entryPoint: metrics
    addEntryPointsLabels: true
    addServicesLabels: true
    addRoutersLabels: true
    buckets:
      - 0.1
      - 0.3
      - 1.2
      - 5.0

Don’t forget to restart Traefik to take into account. Beware of, do not expose this entry point to the web!

Complete traefik.yml file :

bash
---
global:
  sendAnonymousUsage: false
  checkNewVersion: false

api:
  dashboard: true

log:
  filePath: "/etc/traefik/applog.log"
  format: json
  level: "ERROR"

providers:
  docker:
    endpoint: unix:///var/run/docker.sock
    exposedByDefault: false
    watch: true
  file:
    directory: "/dynamic"
    watch: true

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
  websecure:
    address: ":443"
  metrics:
    address: ":9090"

metrics:
  prometheus:
    entryPoint: metrics
    addEntryPointsLabels: true
    addServicesLabels: true
    addRoutersLabels: true
    buckets:
      - 0.1
      - 0.3
      - 1.2
      - 5.0

Prometheus configuration#

Simply, add this bloc in the prometheus.yml file:

yaml

...
scrape_configs:
  - job_name: "traefik"
    static_configs:
      - targets: ["traefik:9090"]

Don’t forget to change the port number if you changed it before in the traefik.yml file. Now, Prometheus can scrape Traefik by the specific metrics entry point. Every Traefik metrics in Prometheus are entitled traefik_.

Source

Stay Updated

Subscribe to the RSS feed or follow for new articles.

Related articles

Latest in #traefik