Promtail match selector for plain text file


I am using promtail to push messages from a plaintext logfile to loki. For extracting fields from the log messages, I am using the regex stage.
However, this logfile contains different types of messages, and therefore I need to use different regex expressions for different types of messages.
It should be possible to achieve this using match, but I am having trouble defining a working selector.

I thought I could use something like this:

selector: '|~ ^log-message-type-1$'

However, this results in the following error: invalid selector syntax for match stage: parse error at line 0, col 1: syntax error: unexpected |~, expecting {

In all the examples in the docs, the selector works with labels. These labels are usually already defined, because e.g. a journald scraper sets various labels for each log message. A static_config (plaintext) scraper does not.

I think I could probably extract the whole message to a label like this:

      - regex:
          expression: ^(?P<msg>.*?)$
      - labels:
     (... match stages....)
      - labeldrop:
        - msg

And then use a selector like this:

selector: '{msg=~"^log-message-type-1$"}'

But this seems really complicated for such a simple task.

Is there a better solution for this?

1 Like