Prometheus Certified Associate (PCA)
Service Discovery
File
In this lesson, we'll explore file service discovery—a straightforward yet flexible mechanism to import jobs and target configurations into Prometheus from an external file. Although this method is less dynamic compared to other alternatives, it proves especially useful when integrating with service discovery systems that Prometheus does not directly support.
File service discovery supports both JSON and YAML file formats, allowing you to manage configurations in multiple files. This approach provides the flexibility to use any file containing valid configuration data, and you can even leverage glob patterns (e.g., *.json
) to include multiple files at once.
Tip
When using glob patterns, ensure the pattern correctly matches all intended files to avoid missing any configurations.
Configuring File Service Discovery in Prometheus
To enable file service discovery, add a file_sd_configs
block to your Prometheus configuration file (typically named prometheus.yaml
). Within this block, specify the file or files holding your configuration details. For example, you can list files individually or use a glob pattern to import all JSON files.
Example JSON Configuration
Below is an example configuration file in JSON format defining three job setups:
[
{
"targets": ["node1:9100", "node2:9100"],
"labels": {
"team": "dev",
"job": "node"
}
},
{
"targets": ["localhost:9090"],
"labels": {
"team": "monitoring",
"job": "prometheus"
}
},
{
"targets": ["db1:9090"],
"labels": {
"team": "db",
"job": "database"
}
}
]
Example Prometheus YAML Configuration
Here is how you can reference the JSON configuration in your Prometheus YAML file:
scrape_configs:
- job_name: file-example
file_sd_configs:
- files:
- 'file-sd.json'
- '*.json'
In this setup, Prometheus imports labels and targets defined in the JSON file. For instance:
- A job with the label
job: node
targets specific nodes. - Another job is dedicated to Prometheus monitoring.
- A third job is defined for database monitoring.
After configuring file service discovery in your prometheus.yaml
, restart Prometheus. On restart, you should see all endpoints appear as if they were defined directly in the configuration file.
Restart Prometheus
Remember to restart Prometheus after updating the configuration to ensure that all new targets are properly detected.
Viewing Discovered Targets
Once Prometheus restarts, navigate to the Status section and then to Service Discovery in the Prometheus dashboard. Here, you can review detailed information about how Prometheus has discovered the endpoints. The interface displays both the original target labels and any additional labels from your file configuration which are used to annotate the scraped metrics.
The following figures illustrate key aspects of file service discovery:
Summary
File service discovery offers a simple and flexible way to integrate external configuration data into Prometheus. By using external files (JSON or YAML) and incorporating glob patterns, you can easily manage a dynamic set of targets without modifying the Prometheus configuration directly. For additional details, consider reviewing the Prometheus Documentation.
This approach is particularly helpful when working with service discovery systems that are not natively supported by Prometheus, providing a seamless bridge that enhances your monitoring capabilities.
Watch Video
Watch video content