{"id":13562297,"url":"https://github.com/ContainerSolutions/prometheus-swarm-discovery","last_synced_at":"2025-04-03T18:33:17.070Z","repository":{"id":87118066,"uuid":"86697178","full_name":"ContainerSolutions/prometheus-swarm-discovery","owner":"ContainerSolutions","description":"This is a POC for Prometheus service discovery on Docker Swarm","archived":true,"fork":false,"pushed_at":"2020-08-31T07:23:59.000Z","size":30,"stargazers_count":76,"open_issues_count":4,"forks_count":35,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-02-23T09:32:42.538Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ContainerSolutions.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-03-30T11:55:22.000Z","updated_at":"2024-01-25T21:43:56.000Z","dependencies_parsed_at":"2023-06-19T18:08:35.075Z","dependency_job_id":null,"html_url":"https://github.com/ContainerSolutions/prometheus-swarm-discovery","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ContainerSolutions%2Fprometheus-swarm-discovery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ContainerSolutions%2Fprometheus-swarm-discovery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ContainerSolutions%2Fprometheus-swarm-discovery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ContainerSolutions%2Fprometheus-swarm-discovery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ContainerSolutions","download_url":"https://codeload.github.com/ContainerSolutions/prometheus-swarm-discovery/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247057000,"owners_count":20876493,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T13:01:06.829Z","updated_at":"2025-04-03T18:33:12.060Z","avatar_url":"https://github.com/ContainerSolutions.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Prometheus-Swarm service discovery\n\n*Prometheus now has [native support for Docker Swarm](https://prometheus.io/docs/prometheus/2.20/configuration/configuration/#dockerswarm_sd_config). We have therefore archived this repository.*\n\nThis is a POC that demonstrates Prometheus service discovery in Docker Swarm. At the moment, this POC only discovers Swarm services and their respective tasks, without attempting to discover nodes or other Swarm concepts.\n\n## How it works\n\nIt is implemented as a standalone tool that writes the scrape targets to a file, that is then read by Prometheus. This uses the `\u003cfile_sd_config\u003e` config\ndirective available in Prometheus.\n\nThe discovery tool expects to have access to the Swarm API using `/var/run/docker.sock`. The provided `docker-compose.yaml`\nis configured to mount the `docker.sock` file inside the container, and this requires that the `swarm-discover` container\nis placed on the Swarm manager node.\n\nThe discovery loop has 3 steps:\n* read the Swarm API and collect all the services (and their tasks) + the networks they are connected to\n* write the scrape targets in the configuration file (`\u003cfile_sd_config\u003e`)\n* connect the Prometheus container to all the networks that belong to the discovered services.\n\nThe rest is done by Prometheus. Whenever the scrape target configuration file is updated, Prometheus re-reads it and loads the current scrape targets.\n\n## Running it\n\nThe only thing required to run Prometheus and the discovery tool is to launch a Swarm stack using the provided docker-compose.yaml\nfile.\n\n```\n$ docker stack deploy -c docker-compose.yaml prometheus\n```\n\n## Port discovery\n\nBy default, Prometheus will use port 80 to connect to a scrape target. But there are 2 ways in which a scrape target can have a different port configured:\n\n* have a port exposed in Docker. That way, the discovery tool can figure out on its own which port to configure for the scrape target.\n* annotate a service using Docker labels. A label with format `prometheus.port: \u003cport_nr\u003e` can be added to each service that requires a custom port configuration.\n\nAs an example here is the following Docker Compose service definition that uses this feature:\n\n```\nversion: '3'\n\nservices:\n  front-end:\n    image: weaveworksdemos/front-end\n    labels:\n        prometheus.port: 8079\n```\n\n## Metadata labels\n\nThe discovery tool attaches a set of metadata labels to each target that are available during the [relabeling phase](https://prometheus.io/docs/operating/configuration/#\u003crelabel_config\u003e) of the service discovery in Prometheus:\n\n* `__meta_docker_service_label_\u003clabelname\u003e`: The value of this service label.\n* `__meta_docker_task_label_\u003clabelname\u003e`: The value of this task label.\n* `__meta_docker_task_name`: The name of the Docker task.\n\nLabels starting with `__` are removed after the relabeling phase, so that these labels will not show up on time series directly.\n\n## Excluding services\n\nTo exclude a specific service from being included in the scrape targets, add a label of format `prometheus.ignore: \"true\"`.\n\nExample Docker Compose service:\n\n```\nversion: '3'\n\nservices:\n  front-end:\n    image: weaveworksdemos/front-end\n    labels:\n        prometheus.ignore: \"true\"\n```\n\n## Configuration options\n\n```\n$ ./prometheus-swarm discover --help\nStarts Swarm service discovery\n\nUsage:\n  promswarm discover [flags]\n\nFlags:\n  -c, --clean               Disconnects unused networks from the Prometheus container, and deletes them. (default true)\n  -d, --discovery           Discovery time: implicit, explicit. Implicit scans all the services found while explicit scans only labeled services. (default \"implicit\")\n  -i, --interval int        The interval, in seconds, at which the discovery process is kicked off (default 30)\n  -l, --loglevel string     Specify log level: debug, info, warn, error (default \"info\")\n  -o, --output string       Output file that contains the Prometheus endpoints. (default \"swarm-endpoints.json\")\n  -p, --prometheus string   Name of the Prometheus service (default \"prometheus\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FContainerSolutions%2Fprometheus-swarm-discovery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FContainerSolutions%2Fprometheus-swarm-discovery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FContainerSolutions%2Fprometheus-swarm-discovery/lists"}