Promtail use template as an input for timestamp

Hi there,

I have a logfile, which has only the time inside. The date of the log is available in the filename. By using two regex and a template step, I was able to construct the correct string with the template section, but the timestamp stage does not recognise the value, just says none.

 pipeline_stages:
#Example line: 11:57:12.405|core/main.cpp|main|INFO|Hello ... I am the here
  - regex:
       expression: "(?P<date>\\d+)$"
       source: filename
  - regex:
       expression: "^(?P<time>[^|]+)\\|(?P<process>[^|]+)\\|(?P<logger>[^|]+)\\|(?P<level>[^|]+)\\|(?P<message>.+)"
  - template:
      source: timestamp
      template: "{{ .date }}-{{ .time }} "
  - timestamp:
      source: timestamp
      format: "20060102-15:04:05.9"
      location: "Europe/Budapest"
  - labels:
      process:
      logger:
      timestamp:
  - drop:
      source: message
      expression: "^\\s*$"
  - output:
      source: message
      +: 20240425
  [inspect: regex stage]: 
  {stages.Entry}.Extracted["level"]:
      +: INFO
  {stages.Entry}.Extracted["logger"]:
      +: handleDisconnect
  {stages.Entry}.Extracted["message"]:
      +: Closing connection due to remote host disconnect request
  {stages.Entry}.Extracted["process"]:
      +: Thread#10
  {stages.Entry}.Extracted["time"]:
      +: 07:26:45.895
  [inspect: template stage]: 
  {stages.Entry}.Extracted["timestamp"]:
      +: 20240425-07:26:45.895 
  [inspect: timestamp stage]: none
  [inspect: labels stage]: 
  {stages.Entry}.Entry.Labels:
      -: {filename="/home/log/srvd.20240425" }
      +: {filename="/home/log/srvd.20240425", logger="handleDisconnect", process="Thread#10", timestamp="20240425-07:26:45.895 "}
  [inspect: output stage]: 
  {stages.Entry}.Entry.Entry.Line:
      -: 07:26:45.895|Thread#10|handleDisconnect|INFO|Closing connection due to remote host disconnect request
      +: Closing connection due to remote host disconnect request
  level=info ts=2024-04-25T12:07:50.544364919Z caller=filetargetmanager.go:372 msg="Adding target" key="srvd.20240425:{ }"

Is this use-case not supported or did I something incorrectly?

Hello,

Everything seems to be fine, I think the problem might be in the fraction of the seconds. Make it to “20060102-15:04:05.999”

Fraction of second: .000 (ms zero prefixed), .000000 (μs), .000000000 (ns), .999 (ms without trailing zeroes), .999999 (μs), .999999999 (ns)

Unfortunately it is not working with 999 neither.

If I manually add the date in the front of the timestamp in the file, then this pattern works (used in other place too)

According to the code, it should work too with .9 https://cs.opensource.google/go/go/+/refs/tags/go1.22.2:src/time/format.go;l=1007

I made the source: filename before the expression clause and it worked for me.

- job_name: test
  static_configs:
  - targets:
      - localhost
    labels:
      job: testprom
      __path__: /var/log/cpp/*
  pipeline_stages:
  - regex:
       source: filename
       expression: "(?P<date>\\d+)$"
  - regex:
       expression: "^(?P<time>[^|]+)\\|(?P<process>[^|]+)\\|(?P<logger>[^|]+)\\|(?P<level>[^|]+)\\|(?P<message>.+)"
  - template:
      source: timestamp
      template: "{{ .date }}-{{ .time }} "
  - timestamp:
      source: timestamp
      format: "20060102-15:04:05.9"
      location: "Europe/Budapest"
  - labels:
      process:
      logger:
      timestamp:
  - drop:
      source: message
      expression: "^\\s*$"
  - output:
      source: message

Hello,

As the pipeline stages are defined as map structure, I would be very surprised, if the order of fields has any impact on the end result.

But I didn’t mention yet, that I used the latest version of promtail 3.0.0 on linux

Can you tell me which version are you using on which platform (win/linux)

At first it was showing me <no-value>-..., but after rewriting the pipeline it worked. I thought the order mightve been the problem.

Ehh… I’ve found it, there was an extra space in the template… I feel so stupid now :slight_smile:

It happens to the best of us!

Look up YAML linters online to reformat and validate your YAML files before using them.