1

This is a part of my Promtail scrape configuration on various hosts to collect journald log entries to a Loki instance:

- job_name: journald
  journal:
    labels:
      job: journald
  relabel_configs:
    - source_labels:
        - __journal__systemd_unit
      target_label: unit
  pipeline_stages:
    - match:
        selector: '{unit=~"session-\\d+\\.scope"}'
        stages:
          - regex:
              source: unit
              expression: "^session-(?P<session_id>\\d+)\\.scope$"
          - template:
              source: message
              template: "id={{ .session_id }} {{ .Entry }}"
          - output:
              source: message
          - labels:
              unit: session.scope

The aim is to relabel all entries captured with unit labels like session-1.scope, session-10.scope etc. with the common label session.scope and to prepend the session_id extracted from the initially assigned label to the entry text. While that latter aspect, adding the prefix, is working and thus implying that most of the configuration is working, the last of the defined stages, that asignment of a static literal to the unit label does not. All entries that were correctly selected and altered still have their distinct labels session-1.scope, session-10.scope etc.

Can anyone hint me what I'm missing to make that last bit work as expected?

funky-future
  • 187
  • 10

2 Answers2

1

You have refer to some actual value in labels section, session.scope doesn't exists so it fails to alter the value.

You can use template to generate some value for it like:

           - template:
                source: session.scope
                template: 'session.scope'
            - labels:
                unit: session.scope
jnauska
  • 11
  • 1
1

With support in the community forums i came to this solution:

    pipeline_stages:
      - match:
          selector: '{service=~"session-\\d+\\.scope"}'
          stages:
            - regex:
                source: service
                expression: "^session-(?P<session_id>\\d+)\\.scope$"
            - template:
                source: message
                template: "{% raw %}id={{ .session_id }} {{ .Entry }}{% endraw %}"
            - output:
                source: message
            - template:
                source: label
                template: session.scope
            - labels:
                service: label

It is doing what @jnauska mentions.

funky-future
  • 187
  • 10