0

我有以下嵌套 JSON 中的日志

{"level":30,"time":1627625600625,"pid":15676,"hostname":"admin-hp-elitebook-840-g2","reqId":"req-2","req":{"method":"POST","url":"/v1/login","hostname":"127.0.0.1:3000","remoteAddress":"127.0.0.1","remotePort":55884},"msg":"incoming request"}

从那开始,我想为方法、URL、主机创建标签,我在 promtail 中尝试过如下 JSON 表达式。我尝试解析 JSON,我能够提取 req,但我不知道如何解析在promtail中嵌套一个

scrape_configs:
- job_name: plainlog
  pipeline_stages:
  - json:
      expressions:
        req: req
  - labels:
      req:
  - output:
      source: req
  static_configs:
  - targets:
      - localhost
    labels:
      job: plainlog
      __path__: /home/nidhin/Desktop/plainlog/*log
4

1 回答 1

0

您需要添加另一个 json 阶段。对于嵌套 json 的每一层,您都添加一个阶段来解析从上一层提取的数据中的数据。例如:

  - json:
      expressions:
        req: 
  - json:
      expressions:
        method: 
      source: req

更多信息:https ://grafana.com/docs/loki/latest/clients/promtail/stages/json/#using-extracted-data

于 2021-09-02T05:02:30.360 回答