Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

prometheus label __meta_docker_container_name with docker-compose

# docker-compose.yml
version: "3"
services:
  prometheus:
    image: prom/prometheus

  test-service:  # <- this
    image: nginx
    deploy:
      replicas: 3
---
# prometheus.yml
scrape_configs:
  - job_name: test
    dns_sd_configs:
      - names:
          - test-service  # goes here
        type: A
        port: 80

------------------------------------------------------------------

Apart from the target list, it gives you more data in labels (e.g. container name, image version, etc) but it also requires a connection to the Docker daemon to get this data. In my opinion, this is an overkill for a development environment, but it might be essential in production. Here is an example configuration, boldly copy-pasted from https://github.com/prometheus/prometheus/blob/release-2.33/documentation/examples/prometheus-docker.yml :

###prometheus-docker.yml
# A example scrape configuration for running Prometheus with Docker.

scrape_configs:
  # Make Prometheus scrape itself for metrics.
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  # Create a job for Docker daemon.
  #
  # This example requires Docker daemon to be configured to expose
  # Prometheus metrics, as documented here:
  # https://docs.docker.com/config/daemon/prometheus/
  - job_name: "docker"
    static_configs:
      - targets: ["localhost:9323"]

  # Create a job for Docker Swarm containers.
  #
  # This example works with cadvisor running using:
  # docker run --detach --name cadvisor -l prometheus-job=cadvisor
  #     --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock,ro
  #     --mount type=bind,src=/,dst=/rootfs,ro
  #     --mount type=bind,src=/var/run,dst=/var/run
  #     --mount type=bind,src=/sys,dst=/sys,ro
  #     --mount type=bind,src=/var/lib/docker,dst=/var/lib/docker,ro
  #     google/cadvisor -docker_only
  - job_name: "docker-containers"
    docker_sd_configs:
      - host: unix:///var/run/docker.sock # You can also use http/https to connect to the Docker daemon.
    relabel_configs:
      # Only keep containers that have a `prometheus-job` label.
      - source_labels: [__meta_docker_container_label_prometheus_job]
        regex: .+
        action: keep
      # Use the task labels that are prefixed by `prometheus-`.
      - regex: __meta_docker_container_label_prometheus_(.+)
        action: labelmap
        replacement: $1
Comment

PREVIOUS NEXT
Code Example
Shell :: raspberry pi autostart gui application 
Shell :: how to add mods to linux minecraft server 
Shell :: msg="could not create cluster: could not create pod disruption budget: poddisruptionbudgets.policy "postgres-fx-postgres-pdb" already exists" 
Shell :: remove gpg error on your installed app or package 
Shell :: change privilege in running container 
Shell :: Authenticate with a GitHub host. 
Shell :: Case/switch 
Shell :: deleting compizconfig 
Shell :: wget health check 
Shell :: get file from branch git 
Shell :: comand moves or renames the old file to the new name 
Shell :: macos check if file is binary 
Shell :: docker run from docker hub 
Shell :: png to jpg linux command line 
Shell :: linux zip current directory 
Shell :: how to change home directory of git bash 
Shell :: bash how to create directories in all subdirectories 
Shell :: cholocatey install 
Shell :: copy commit in another branch 
Shell :: flutter release mode 
Shell :: start with in shell script 
Shell :: install youtube-dl on ubuntu 20.04 
Shell :: xubuntu desktop 
Shell :: count specific number in array powershell 
Shell :: failed at the node-sass@4.13.1 postinstall script. 
Shell :: brew 
Php :: drupal 8 show php errors 
Php :: laravel back button 
Php :: php header utf8 json 
Php :: downgrade php 7.4 to 7.3 mac 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =