Relabel Instance to Hostname in Prometheus

Better Stack Team
Updated on November 18, 2024

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

  1. Open Your Prometheus Configuration File: Typically named prometheus.yml, this file contains the scrape configurations and other settings for Prometheus.
  2. Locate the Scrape Configuration: Find the relevant scrape_configs section where you define your target endpoints.
  3. Add Relabeling Rules: You will add a relabel_configs section to modify the instance 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 uses instance as the source.
  • target_label: This is the label you want to create or modify. Here, we are setting the hostname 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 the instance 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

  1. Restart Prometheus: After making changes to the prometheus.yml file, restart your Prometheus server to apply the configuration.
  2. 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 the hostname label appears as expected.
  3. 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.

Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

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 us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build 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.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github