Relabel Instance to Hostname in Prometheus
Relabeling in Prometheus is a powerful feature that allows you to modify labels in your metrics before they are stored. If you want to relabel the instance
label to hostname
, you can do this using the relabel_configs
configuration in your prometheus.yml
file. Here’s how you can set this up.
Steps to Relabel instance
to hostname
- Open Your Prometheus Configuration File:
Typically named
prometheus.yml
, this file contains the scrape configurations and other settings for Prometheus. - Locate the Scrape Configuration:
Find the relevant
scrape_configs
section where you define your target endpoints. Add Relabeling Rules: You will add a
relabel_configs
section to modify theinstance
label. Here’s an example configuration:scrape_configs: - job_name: 'your_job_name' static_configs: - targets: ['localhost:9090'] # Replace with your actual targets relabel_configs: - source_labels: [__address__] target_label: instance - source_labels: [instance] target_label: hostname replacement: '${1}' # Use the value of instance as hostname regex: '([^:]+).*' # Capture everything before the colon
Breakdown of the Configuration
source_labels
: This specifies the labels from which to derive new values. In this case, it usesinstance
as the source.target_label
: This is the label you want to create or modify. Here, we are setting thehostname
label.replacement
: This determines what the new value of the target label will be. You can directly use the captured value from the regex.regex
: This regex pattern is used to match the value of theinstance
label. In this example,([^:]+).*
captures everything before the colon (typically the hostname or IP).
Example Configuration
Here’s a complete example with a sample job:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'my_application'
static_configs:
- targets: ['localhost:9090', '127.0.0.1:8080']
relabel_configs:
- source_labels: [__address__]
target_label: instance
- source_labels: [instance]
target_label: hostname
regex: '([^:]+).*'
replacement: '${1}'
Verifying the Changes
- Restart Prometheus: After making changes to the
prometheus.yml
file, restart your Prometheus server to apply the configuration. - Check the Targets: Go to the Prometheus web UI (usually at
http://localhost:9090/targets
) to see if your targets are being scraped correctly and that thehostname
label appears as expected. Query Metrics: You can also run a query in the Prometheus UI to check if the metrics reflect the new
hostname
label:{job="my_application"}
Conclusion
By using the relabel_configs
feature in Prometheus, you can effectively transform the instance
label into hostname
, allowing for better organization and clarity in your metrics. This is especially useful when working with dynamic environments, like containers or cloud instances, where hostnames may differ from IP addresses.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github