简体   繁体   English

在 Grafana 中组合来自 Loki 的多个日志行

[英]Combining multiple log lines from Loki in Grafana

I have stream of http logs (fastify pino format) via Loki that look like:我通过 Loki 有 http 日志(fastify pino 格式)的 stream 看起来像:

[2022-07-25T16:59:40.796Z] INFO: incoming request {"req":{"method":"GET","url":"/api/v1/teams/6vYE9rpOPl/members","hostname":"forge.flowforge.loc","remoteAddress":"10.1.106.162","remotePort":38422},"reqId":"req-t6"}
[2022-07-25T16:59:40.810Z] INFO: request completed {"res":{"statusCode":200},"responseTime":13.292339086532593,"reqId":"req-t6"}

I'd like to display average response time by path, but I'm struggling to work out how to combine the 2 log lines correlated by the reqId to get the url and responseTime together.我想按路径显示平均响应时间,但我正在努力研究如何组合由reqId相关的 2 条日志行以将urlresponseTime结合在一起。

I can extract and parse the json for the 2 lines separately but not together.我可以分别提取和解析两行的 json 但不能一起解析。

I don't think it's doable with Loki alone.我认为仅靠 Loki 是行不通的。 For that you would need either:为此,您需要:

One solution I can think of is using Grafana transformations :我能想到的一种解决方案是使用Grafana 转换

  • use LogQL pattern and line_format to extract the JSON part from the logs, so that the line field becomes a valid JSON.使用 LogQL 模式line_format从日志中提取 JSON 部分,使line字段变为有效的 JSON。 Do not parse the JSON in LogQL: the extracted fields wouldn't be recognized by the Grafana transformations later on.不要在 LogQL 中解析 JSON:提取的字段稍后不会被 Grafana 转换识别。
  • apply "extract fields" transformation in order to parse the JSON (the transformation is currently in an Alpha version)应用“提取字段”转换以解析 JSON(转换当前处于 Alpha 版本)
  • add merge transformation添加合并转换
  • outer join by reqId通过 reqId 进行外部连接
  • group by reqId reqId 分组

Grafana transformations are pretty powerful but a bit non-intuitive too, so the solution would require some experimenting. Grafana 转换非常强大,但也有点不直观,因此解决方案需要一些试验。 The debug transformation functionality might be helpful. 调试转换功能可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM