'combine value of two lables in promtail config

How to add the values of multiple labels and assign them to another label in promtail config?

scrape_configs:
  - job_name: journal
    journal:
      max_age: 12h
    relabel_configs:
    - source_labels: ['__journal__machine_id']
      target_label: 'HostId'
    - source_labels: ['__journal__hostname']
      target_label: 'HostName'
    - source_labels: ['__journal_syslog_identifier']
      target_label: 'ApplicationName'
    pipeline_stages:
    - match:
        selector: '{ApplicationName="test-app"}'
        stages:
        - static_labels:
            OriginId: //here I want to asign HostId+HostName+ApplicationName

In the end, I expect the value of label OriginId would be HostId+HostName+ApplicationName



Solution 1:[1]

static_labels only allows adding a static label to the label set, i.e you cannot use the value of other labels. Since you already have a relabel_configs section maybe you can generate the OriginId directly from the relabeling step? Something like:

- source_labels: ['__journal__machine_id', '__journal__hostname', '__journal_syslog_identifier']
  separator: '_'
  target_label: 'OriginId'

In this case if the input label set looks like:

__journal__machine_id: "machine-id-1"
__journal__hostname: "host1"
__journal_syslog_identifier: "abcde-123"

OriginId would end up with the value: machine-id-1_host1_abcde-123. The default separator (if none is specified in the configuration is ;).

Solution 2:[2]

You can use the replace action with a separator in a relabel_config.

Here's an example:

...
- action: replace
  separator: "+"
  source_labels:
    - source_labels: 
      - __journal__machine_id
      - __journal__hostname
      - __journal_syslog_identifier
      target_label: 'OriginId'
...

I think that should work for you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jorge Luis
Solution 2 Danny Kopping