jmx_exporter icon indicating copy to clipboard operation
jmx_exporter copied to clipboard

How to add prefix to all metrics?

Open cutiechi opened this issue 4 years ago • 3 comments
trafficstars

I want to add prefix to all metrics

cutiechi avatar Jul 16 '21 07:07 cutiechi

This can be configured in the Prometheus server, not in the jmx_exporter. In the Prometheus server configuration, add metric_relabel_configs to the job in your prometheus.yml. Example:

scrape_configs:
  - job_name: 'jmx_exporter'
    static_configs:
    - targets: ['localhost:8080']
    metric_relabel_configs:
      - source_labels: [ __name__ ]
        target_label: __name__
        regex: '(.*)'
        action: replace
        replacement: my_prefix_${1}

This assumes that jmx_exporter exports metrics on http://localhost:8080/metrics. Without the metric_relabel_configs you would see metrics like jvm_buffer_pool_used_bytes. With the config, you see them prefixed like my_prefix_jvm_buffer_pool_used_bytes. The regex and replacement give you fine control over which metrics you want to rename how.

fstab avatar Jul 18 '21 19:07 fstab

This can be configured in the Prometheus server, not in the jmx_exporter. In the Prometheus server configuration, add metric_relabel_configs to the job in your prometheus.yml. Example:

scrape_configs:
  - job_name: 'jmx_exporter'
    static_configs:
    - targets: ['localhost:8080']
    metric_relabel_configs:
      - source_labels: [ __name__ ]
        target_label: __name__
        regex: '(.*)'
        action: replace
        replacement: my_prefix_${1}

This assumes that jmx_exporter exports metrics on http://localhost:8080/metrics. Without the metric_relabel_configs you would see metrics like jvm_buffer_pool_used_bytes. With the config, you see them prefixed like my_prefix_jvm_buffer_pool_used_bytes. The regex and replacement give you fine control over which metrics you want to rename how.

Thank you, but I can't make configuration changes to Prometheus. At present, I have added metric prefix to TextFormat

cutiechi avatar Jul 19 '21 01:07 cutiechi

Adding the prefix to TextFormat certainly works. I don't think there is a better workaround.

If you want to solve this with configuration, you can configure rules with pattern and name, so that name starts with your desired prefix. However, I fear there is no generic pattern and name that works for all JMX beans, so you likely have to configure several rules to catch all the cases that are relevant for you.

fstab avatar Jul 19 '21 21:07 fstab

Closing as stale. Please reopen if you would like to continue the discussion.

dhoard avatar Jun 24 '23 01:06 dhoard