Skip to content

Instantly share code, notes, and snippets.

@ulexxander
Last active September 17, 2023 20:13
Show Gist options
  • Save ulexxander/b4554a95f3e8140a508779ee337c1b41 to your computer and use it in GitHub Desktop.
Save ulexxander/b4554a95f3e8140a508779ee337c1b41 to your computer and use it in GitHub Desktop.
Prometheus docker_sd relabel configs to be able to use prometheus.io scrape, path and port container labels, just like we do in Kubernetes!
global:
scrape_interval: 10s
scrape_configs:
- job_name: docker_sd
docker_sd_configs:
- host: unix:///var/run/docker.sock
refresh_interval: 10s
relabel_configs:
- action: keep
source_labels:
- __meta_docker_container_label_prometheus_io_scrape
regex: true
- source_labels:
- __meta_docker_container_label_prometheus_io_path
regex: (.+)
target_label: __metrics_path__
- source_labels:
- __address__
- __meta_docker_container_label_prometheus_io_port
regex: (.*):(\d+);(\d+)
replacement: ${1}:${3}
target_label: __address__
- source_labels:
- __meta_docker_container_id
target_label: container_id
- source_labels:
- __meta_docker_container_name
target_label: container_name
@ulexxander
Copy link
Author

ulexxander commented Sep 17, 2023

If the container exposes single port with metrics on /metrics path, it is enough to just add prometheus.io/scrape=true label.

If container exposes multiple ports and serves metrics on only one of them, specify label prometheus.io/port containing port on which metrics are served, for example prometheus.io/port=8080

If container does not expose port at all, Prometheus will fall back to port 80. To override it in that case also use prometheus.io/port label.

If container serves metrics on path that is different from /metrics, override it with prometheus.io/path label like so: prometheus.io/path=/api/v1/metrics/prometheus.

@ulexxander
Copy link
Author

ulexxander commented Sep 17, 2023

Example Docker Compose:

version: "3.8"

services:
  traefik:
    image: traefik:2.10
    ports:
      - "80:80"
    command:
      - --accessLog=true
      - --metrics.prometheus=true
      - --providers.docker.exposedByDefault=false
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    restart: always
    labels:
      - traefik.enable=true
      - traefik.http.routers.traefik-dashboard.rule=Host(`traefik.localhost`)
      - traefik.http.routers.traefik-dashboard.service=api@internal
      - prometheus.io/scrape=true
      - prometheus.io/port=8080

  prometheus:
    image: prom/prometheus:v2.47.0
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus-data:/prometheus
      - /var/run/docker.sock:/var/run/docker.sock
    user: root
    restart: always
    labels:
      - traefik.enable=true
      - traefik.http.routers.prometheus.rule=Host(`prometheus.localhost`)
      - traefik.http.services.prometheus.loadbalancer.server.port=9090
      - prometheus.io/scrape=true

  grafana:
    image: grafana/grafana:10.0.5
    volumes:
      - grafana-data:/var/lib/grafana
    user: root
    restart: always
    labels:
      - traefik.enable=true
      - traefik.http.routers.grafana.rule=Host(`grafana.localhost`)
      - traefik.http.services.grafana.loadbalancer.server.port=3000
      - prometheus.io/scrape=true

  fluent-bit:
    image: fluent/fluent-bit:2.1.9
    volumes:
      - ./fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf
      - ./fluent-bit-parsers.conf:/fluent-bit/etc/parsers.conf
    restart: always
    labels:
      - prometheus.io/scrape=true
      - prometheus.io/path=/api/v1/metrics/prometheus

volumes:
  prometheus-data:
  grafana-data:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment