# 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`

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:
    
    ```yaml
    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:

```yaml
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.