monitoring - 如何 promtail 解析 json 以标记和时间戳

标签 monitoring grafana

我在用 promtail 解析 json 日志时遇到问题,请有人帮我。我尝试了很多配置,但不解析时间戳或其他标签。

日志条目:

{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

最佳答案

我已经尝试使用 Java SpringBoot 应用程序设置 Promtail(它通过 Logstash logback 编码器以 JSON 格式生成日志文件)并且它工作正常。
应用程序生成的示例日志行:

{"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}
prometail 配置:
# 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
请注意,输出(日志文本)首先由 Go 模板配置为 new_key,然后设置为输出源。 logger={{ .logger_name }} 有助于识别在 Loki View 上解析的字段(但这取决于您希望如何为应用程序配置它)。
在这里你会找到关于整个过程的非常好的文档:https://grafana.com/docs/loki/latest/clients/promtail/pipelines/
该示例在 Loki 和 Promtail 的 v1.5.0 版上运行( 更新 2020-04-25 :我已经更新了当前版本的链接 - 2.2,因为旧链接停止工作)。
关于时间戳的部分在这里:https://grafana.com/docs/loki/latest/clients/promtail/stages/timestamp/ with examples - 我已经测试过了,也没有发现任何问题。希望能有点帮助。
JSON 配置部分:https://grafana.com/docs/loki/latest/clients/promtail/stages/json/
洛基的结果:
enter image description here

关于monitoring - 如何 promtail 解析 json 以标记和时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58564836/

相关文章:

docker - Kamon、Statsd、Grafana 磁盘空间

docker - Grafana仪表板中的Kubernetes Pane 显示当前,请求,限制和缓存的内存使用情况。缓存指示什么?

influxdb - 在 grafana 单一统计中使用来自 influxdb 的时间字段

mysql - 如何在grafana中查询当前日期postgres/mysql

amazon-ec2 - 如何使用 CloudWatch 从 Cloudformation 查看 EC2 实例的详细监控?

sql-server - 数据库的CPU利用率?

.net - 监视 .NET 应用程序的最佳方法是什么?

java - java中如何监控应用程序信息?

spring-cloud - Spring Cloud Data Flow Grafana Prometheus 未显示流数据

Spring WebSockets 运行时监控 STOMP 帧 - 解释