So to be clear, if you want your targets file to look like this:
- targets:
- SVR-DS01 172.25.X0.XXX:9182
- SRV-DS02 172.21.X1.XXX:9182
you can do something like this:
relabel_configs:
# When __address__ is a single item, set the instance
# label to the part without the port
- source_labels: [__address__]
regex: '([^ ]+):[0-9]+'
replacement: '${1}'
target_label: instance
# When __address__ is space-separated "name address:port",
# put the first part in the "instance" label and leave the second part
# in "__address__"
- source_labels: [__address__]
regex: '(.+) (.+)'
target_label: instance
replacement: '${1}'
- source_labels: [__address__]
regex: '(.+) (.+)'
target_label: __address__
replacement: '${2}'
If your target entries *always* consist of two items separated by a space,
then you don't need the first rule. It's only there in case you have
entries in the old format, i.e.
- targets:
- 172.25.X0.XXX:9182
(in which case, the instance is set to "172.25.X0.XXX")
On Thursday, 27 April 2023 at 16:16:38 UTC+1 Brian Candler wrote:
> Well, there is a good reason to keep the port out of the "instance" label:
> it's very awkward to make a query to join two different different metrics
> collected from two different exporters on the same host, if one has (say)
> instance="foo:9100" and another has instance="foo:9104". A second reason
> is that it's nicer in dashboards to see "foo" rather than "foo:9100"
>
> See https://www.robustperception.io/controlling-the-instance-label
>
> IMO the cleanest way to achieve this is to leave the port out of the
> targets file, copy __address__ to instance, and then append the exporter
> port statically to __address__.
>
> However, you could leave the port number in the targets file, and strip it
> out to create the instance label:
>
> - source_labels: [__address__]
> regex: '(.*):[0-9]+'
> target_label: instance
> replacement: '${1}'
>
> This would be useful if the same exporter were running on different ports
> on different hosts (fairly uncommon I think).
>
> Also, you risk a collision of metrics if targets list includes the same
> host twice on two different ports in the same scrape job (which should be
> very unlikely).
>
> On Thursday, 27 April 2023 at 15:14:03 UTC+1 Kolja Krückmann wrote:
>
>> Hi Brian,
>>
>> thank you very much for your response!
>> I just wanted to clarify, if it's better to "regex" the port to the end
>> of the address, or if it is equally fine to just add the port in the
>> target.yml to each target? (That's what im currently doing) Is it just best
>> practice with the regex expression or is there actually a technical reason
>> behind that?
>>
>> Kind regards
>> Kolja
>>
>> Brian Candler schrieb am Dienstag, 18. April 2023 um 13:34:53 UTC+2:
>>
>>> Alternatively, you use rewriting rules.
>>>
>>> - targets:
>>> - SVR-DS01 172.25.X0.XXX
>>> - SRV-DS02 172.21.X1.XXX
>>> ... etc
>>>
>>> and the corresponding relabel_configs in your scrape job could be
>>> something like this:
>>>
>>> relabel_configs:
>>> # When __address__ consists of just a name or IP address,
>>> # copy it to the "instance" label. This keeps the port
>>> # number out of the instance label.
>>> - source_labels: [__address__]
>>> regex: '([^ ]+)'
>>> target_label: instance
>>>
>>> # When __address__ is of the form "name address", extract
>>> # name to "instance" label and address to "__address__"
>>> - source_labels: [__address__]
>>> regex: '(.+) (.+)'
>>> target_label: instance
>>> replacement: '${1}'
>>> - source_labels: [__address__]
>>> regex: '(.+) (.+)'
>>> target_label: __address__
>>> replacement: '${2}'
>>>
>>> # Append port number to __address__ so that scrape gets
>>> # sent to the right port
>>> - source_labels: [__address__]
>>> target_label: __address__
>>> replacement: '${1}:9182'
>>>
>>> On Tuesday, 18 April 2023 at 11:14:23 UTC+1 Julius Volz wrote:
>>>
>>>> The labels in the SD file can only be provided for each target group
>>>> (the top-level list item type of the YAML file), so you'd have to do
>>>> something like this:
>>>>
>>>> - targets:
>>>> - [...first list of targets...]
>>>> labels:
>>>> instance: "A"
>>>> - targets:
>>>> - [...second list of targets...]
>>>> labels:
>>>> instance: "B"
>>>> - targets:
>>>> - [...third list of targets...]
>>>> labels:
>>>> instance: "C"
>>>>
>>>>
>>>> On Tue, Apr 18, 2023 at 11:20 AM Kolja Krückmann <[email protected]>
>>>> wrote:
>>>>
>>>>> Hi y'all I'm trying to get a label for each target in my file_sd_config
>>>>>
>>>>> my prom.yml is:
>>>>> (redacted)
>>>>>
>>>>> # my global config
>>>>> global:
>>>>> scrape_interval: 1m # Set the scrape interval to every 15 seconds.
>>>>> Default is every 1 minute.
>>>>> evaluation_interval: 30s # Evaluate rules every 15 seconds. The
>>>>> default is every 1 minute.
>>>>> scrape_timeout: 30s
>>>>>
>>>>> scrape_configs:
>>>>> # The job name is added as a label `job=<job_name>` to any
>>>>> timeseries scraped from this config.
>>>>> - job_name: 'node'
>>>>> file_sd_configs:
>>>>> - files:
>>>>> -
>>>>> C:/Prometheus/prometheus-2.41.0.windows-amd64/target_cluster_b.yml
>>>>>
>>>>> my target_cluster_b.yml:
>>>>> - targets:
>>>>> - 172.25.X0.XXX:9182
>>>>> labels:
>>>>> instance: "SVR-DS01"
>>>>> - 172.21.X1.XXX:9182
>>>>> - 172.25.X2.XXX:9182
>>>>> - 172.25.X3.XXX:9182
>>>>>
>>>>> as seen in the target.yml I want to try to get a label to each target
>>>>> - to see in the prom dashboard which ip is which targetname
>>>>>
>>>>> Thanks in advance.
>>>>> Kind regards Kolja
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Prometheus Users" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/prometheus-users/fa4e5c9e-807b-4680-9640-592c8172e568n%40googlegroups.com
>>>>>
>>>>> <https://groups.google.com/d/msgid/prometheus-users/fa4e5c9e-807b-4680-9640-592c8172e568n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>>
>>>>
>>>> --
>>>> Julius Volz
>>>> PromLabs - promlabs.com
>>>>
>>>
--
You received this message because you are subscribed to the Google Groups
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/prometheus-users/918f213c-3b62-4390-b125-1acab2bf6913n%40googlegroups.com.