document how to remove port from the instance label
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 717
- Forks
- 1.2k
- Avg merge
- 3d 15h
- Merged PRs (30d)
- 19
Description
Hey.
AFAICS the following is not yet "officially" documented (sorry in case I just missed it).
What many people seem to want (given the number of stackoverflow/etc. questions and blog entries about it) is to have their instance labels consisting of just the hostname without the port.
There many different (unofficial) FAQs, blog posts, etc. ... all giving some different description on how to do it, using relabel_configs.
One (nearly) particular good one is IMO https://stackoverflow.com/a/63414542/6646161 , which has the nice feature of not overriding any manually set "<labelname>": "<labelvalue>" like e.g. in a <file_sd_config> - assuming that if an admin explicitly set something there, it should be kept as is (including the :port, which should removed when it was just inherited from the host:port of the target setting.
However, IMO there's a problem with the above stackoverflow solution, namely that it would fail at least in the case when the instance label contains a ;, which could be the case again, when manually set.
I came up with my own solution, which I think should work even in that case:
relabel_configs:
- source_labels: [instance]
target_label: instance
regex: '(.+)'
replacement: '_${1}:0'
- source_labels: [instance,__address__]
#separator: ;
target_label: instance
regex: '^;(.+)'
replacement: ';${1}'
- source_labels: [instance]
target_label: instance
regex: '^.(.+):\d+$'
replacement: '${1}'
- The first rule is only for the case when the
instancelabel has been manually set by the config (and not via the target’shost:port) ... if so, it will be non-zero in length and.+will match (otherwise it won't).
It merely adds a leading_(indicating that this manually set) and a dummy:0port to be removed again later. - The second rule is only to bring in the
__address__and only matches if there is a leading;(which is only the case ifinstanceis empty - if not, there would be a leading_from the first rule). It leaves the leading;so that in both cases there's some leading character (either_or;). - The third rule strips the leading character (either
_or;) and the port.
This strictly requires the value of separator to be ; so the above setting might be uncommented if this is added to the documentation, in case the default value should ever change.
Please feel free to improve that (or come up with something even better)... but in any case, I think it (or some better version) would be worth to be added to the official documentation at some place.
Thanks,
Chris.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the relabel_configs and file_sd_config examples in the issue, then compare the linked Stack Overflow guidance with the proposed semicolon-safe approach. Document an official method for removing the inherited port while preserving manually set instance labels, including the separator assumption and relevant edge cases.
Written by the indexing model from the issue text.
Assessment
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100