Traefik Essentials Monitoring with Grafana, Prometheus & Loki

Traefik exposes metrics on EntryPoints, Routers, Services, and more. This guide shows you how to collect these metrics with Prometheus and aggregate logs with Loki via Grafana Alloy for a complete monitoring solution.

Prerequisites

Metrics

Enable Metrics in Traefik

Add the following command flags to your Traefik docker-compose.yml:

docker-compose.yml
command:
  # ... existing commands ...
  - "--metrics.prometheus=true"
  - "--metrics.prometheus.addEntryPointsLabels=true"
  - "--metrics.prometheus.addRoutersLabels=true"
  - "--metrics.prometheus.addServicesLabels=true"
Click to expand and view more

Re-create the Traefik container to apply the changes:

BASH
docker compose -f traefik/docker-compose.yml up -d --force-recreate
Click to expand and view more

Verify metrics are available at http://<traefik-ip>:8080/metrics.

Configure Prometheus

Add a scrape job to your prometheus.yml:

prometheus.yml
scrape_configs:
  - job_name: 'traefik'
    scrape_interval: 5s
    static_configs:
      - targets: ['<traefik-ip>:8080']
Click to expand and view more

Replace <traefik-ip> with your Traefik server’s IP address, then restart Prometheus:

BASH
docker restart prometheus
Click to expand and view more

Verify Metrics

Query for Traefik metrics in Grafana’s Explore view:

PROMQL
traefik_router_requests_total
Click to expand and view more

Log Files

Enable Logging in Traefik

Add the following command flags to your Traefik docker-compose.yml:

docker-compose.yml
command:
  # ... existing commands ...
  - "--accesslog=true"
  - "--accesslog.filepath=/log/access.log"
  - "--accesslog.format=json"
  - "--accesslog.fields.defaultmode=keep"
  - "--accesslog.fields.names.StartUTC=drop"
  - "--log.filepath=/log/traefik.log"
  - "--log.format=json"
Click to expand and view more

Note: Setting a filepath redirects logs to a file — they will no longer appear in docker logs.

Also add a volume mapping for the log directory:

docker-compose.yml
volumes:
  # ... existing volumes ...
  - /var/log/traefik:/log
Click to expand and view more

Re-create the Traefik container:

BASH
docker compose -f traefik/docker-compose.yml up -d --force-recreate
Click to expand and view more

Configure Grafana Alloy

Create a new Alloy config file for Traefik log collection:

BASH
nano alloy/config/traefik.alloy
Click to expand and view more
traefik.alloy
// Traefik logs collection
local.file_match "traefik" {
  path_targets = [{
    __path__ = "/var/log/traefik/*.log",
  }]
}

loki.source.file "traefik" {
  targets    = local.file_match.traefik.targets
  forward_to = [loki.process.traefik.receiver]
}

loki.process "traefik" {
  stage.static_labels {
    values = {
      job = "traefik",
    }
  }
  forward_to = [loki.write.default.receiver]
}
Click to expand and view more

Ensure your Alloy docker-compose.yml has /var/log mounted:

docker-compose.yml
volumes:
  - ./config/:/etc/alloy/config/:ro
  - /var/log:/var/log:ro
  - alloy-data:/var/lib/alloy/data
Click to expand and view more

Restart Alloy to pick up the new configuration:

BASH
docker restart alloy
Click to expand and view more

Verify Logs

Open the Alloy Web UI and confirm the loki.source.file component is healthy, then query in Grafana:

LOGQL
{job="traefik"}
Click to expand and view more

Grafana Dashboard

You can use the pre-built Traefik Dashboard from GitHub, or explore available metrics from the official Traefik metrics overview .

Copyright Notice

Author: Sven van Ginkel

Link: https://svenvg.com/posts/traefik-essentials-monitoring-with-grafana-prometheus-loki/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut