Skip to content

Instantly share code, notes, and snippets.

@RichiH
Last active July 24, 2023 13:06
Show Gist options
  • Save RichiH/f2ffb824d428d8ca0c7c40cd62022b56 to your computer and use it in GitHub Desktop.
Save RichiH/f2ffb824d428d8ca0c7c40cd62022b56 to your computer and use it in GitHub Desktop.
Prometheus relabelling to get rid of port number
============================================
============================================
============================================
This does what it should:
regex: (.+?)(:80)?
Also see https://golang.org/pkg/regexp/syntax/
============================================
============================================
============================================
This returns 'instance="XX"'
- targets:
- XX:80
relabel_configs:
- source_labels: [ __address__ ]
target_label: instance
regex: '(.*):80'
replacement: '${1}'
============================================
But auto-attached :80 does not go away and results in 'instance="XX:80"'
- targets:
- XX
relabel_configs:
- source_labels: [ __address__ ]
target_label: instance
regex: '(.*):80'
replacement: '${1}'
============================================
Returns 'instance="XX"'
- targets:
- XX
relabel_configs:
- source_labels: [ __address__ ]
target_label: instance
regex: '(.*)(:80)?'
replacement: '${1}'
============================================
Returns 'instance="XX:80"'
- targets:
- XX:80
relabel_configs:
- source_labels: [ __address__ ]
target_label: instance
regex: '(.*)(:80)?'
replacement: '${1}'
@jnovack
Copy link

jnovack commented Jul 24, 2023

In cases where you use a difference scheme (such as https) or port (e.g. 8080), the regex you supplied above will not work.

You should consider making the regex independent of the port.

    relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        regex: "([^:]+).*"
        replacement: '${1}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment