0

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

Vencat
  • 1,272
  • 11
  • 36

3 Answers3

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.

Danny Kopping
  • 4,862
  • 2
  • 29
  • 38
  • is it possible to hash the value before I assign it to OriginId? – Vencat Mar 17 '22 at 16:12
  • The docs reference a `hashmod` option: > hashmod: Set target_label to the modulus of a hash of the concatenated source_labels. That's probably not exactly what you're looking for, but that's about the extent of it in relabel_configs I believe. – Danny Kopping Mar 17 '22 at 20:40
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 ;).

Jorge Luis
  • 3,098
  • 2
  • 16
  • 21
  • Thanks it works! I have another question, is it possible to do relabel_configs in the pipeline_stages? – Vencat Mar 17 '22 at 13:59
  • `relabel_configs` is specific to the scraping stage (i.e log collection) so it cannot be used directly in the `pipeline_stages`. That being said, depending on what you want to do it is posible to transform the log line and label set in the `pipeline_stage`. These are the stages that are currently available: https://grafana.com/docs/loki/latest/clients/promtail/pipelines/#stages. – Jorge Luis Mar 18 '22 at 10:12
0

This can be achieved via template stage.

Below example maps team and space from filename to create a new label called namespace.

scrape_configs:
- job_name: logs
  static_configs:
  - targets:
      - localhost
    labels:
      hostname: ${HOSTNAME}
      job: logs
      __path__: /logs/**/**/*.{log,txt,out}
  pipeline_stages:
    - match:
        selector: '{job="logs"}'
        stages:
        - regex:
            source: filename
            expression: "/logs/(?P<team>.+)/(?P<space>.+)/(?P<pod>.+)/.+"
        - template:
            source: namespace
            template: '{{ .team }}-{{ .space }}'
        - labels:
            team:
            namespace:
            pod:
freedomson
  • 57
  • 2
  • 7