Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to promtail parse json to label and timestamp

Tags:

I have a probleam to parse a json log with promtail, please, can somebody help me please. I try many configurantions, but don't parse the timestamp or other labels.

log entry:

{timestamp=2019-10-25T15:25:41.041-03, level=WARN, thread=http-nio-0.0.0.0-8080-exec-2, mdc={handler=MediaController, ctxCli=127.0.0.1, ctxId=FdD3FVqBAb0}, logger=br.com.brainyit.cdn.vbox.
controller.MediaController, message=[http://localhost:8080/media/sdf],c[500],t[4],l[null], context=default}

promtail-config.yml

server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: vbox-main
  static_configs:
  - targets:
    - localhost
    labels:
      job: vbox
      appender: main
      __path__: /var/log/vbox/main.log        

  pipeline_stages:
  - json:
      expressions:
        timestamp: timestamp
        message: message
        context: context
        level: level
      timestamp:
        source: timestamp
        format: RFC3339Nano
      labels:
        context:
        level:
      output:
        source: message
like image 427
skopp Avatar asked Oct 25 '19 19:10

skopp


People also ask

What is JSON parse () method?

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

How do I parse a JSON file?

If you need to parse a JSON string that returns a dictionary, then you can use the json. loads() method. If you need to parse a JSON file that returns a dictionary, then you can use the json. load() method.

How do I check my Promtail output?

Enable the inspection output using the --inspect command-line option. The --inspect option can be used in combination with --stdin and --dry-run . The output uses color to highlight changes. Additions are in green, modifications in yellow, and removals in red.

How do I display JSON data in Grafana?

One way to display Json data is through grafana's own json datasource (grafana-simple-json-datasource) which would then convert into data. You might want to check if that would meet your needs. One other way would be developing a panel plugin that would plot a graph given the json you give it.


1 Answers

I've tried the setup of Promtail with Java SpringBoot applications (which generates logs to file in JSON format by Logstash logback encoder) and it works.

The example log line generated by application:

{"timestamp":"2020-06-06T01:00:30.840+02:00","version":1,"message":"Started ApiApplication in 1.431 seconds (JVM running for 6.824)","logger_name":"com.github.pnowy.spring.api.ApiApplication","thread_name":"main","level":"INFO","level_value":20000}

The prometail config:

# Promtail Server Config
server:
  http_listen_port: 9080
  grpc_listen_port: 0

# Positions
positions:
  filename: /tmp/positions.yaml
    
clients:
  - url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: springboot
  pipeline_stages:
  - json:
      expressions:
        level: level
        message: message
        timestamp: timestamp
        logger_name: logger_name
        stack_trace: stack_trace
        thread_name: thread_name
  - labels:
      level:
  - template:
      source: new_key
      template: 'logger={{ .logger_name }} threadName={{ .thread_name }} | {{ or .message .stack_trace }}'
  - output:
      source: new_key
  static_configs:
  - targets:
      - localhost
    labels:
      job: applogs
      __path__: /Users/przemek/tools/promtail/*.log

Please notice that the output (the log text) is configured first as new_key by Go templating and later set as the output source. The logger={{ .logger_name }} helps to recognise the field as parsed on Loki view (but it's an individual matter of how you want to configure it for your application).

Here you will find quite nice documentation about entire process: https://grafana.com/docs/loki/latest/clients/promtail/pipelines/

The example was run on release v1.5.0 of Loki and Promtail (Update 2020-04-25: I've updated links to current version - 2.2 as old links stopped working).

The section about timestamp is here: https://grafana.com/docs/loki/latest/clients/promtail/stages/timestamp/ with examples - I've tested it and also didn't notice any problem. Hope that help a little bit.

The JSON configuration part: https://grafana.com/docs/loki/latest/clients/promtail/stages/json/

Result on Loki:

enter image description here

like image 189
Przemek Nowak Avatar answered Sep 19 '22 22:09

Przemek Nowak