Spe*_*pen 12 grafana grafana-loki logql
对于同一个应用程序,我们有不同类型的日志。有些来自我们的应用程序,以 JSON 格式记录,有些则来自不同类型的日志消息。
例如这 3 行日志:
"{\"written_at\": \"2022-03-30T07:51:04.934Z\", \"written_ts\": 1648626664934052000, \"msg\": \"Step 'X' started at 2022-03-30 07:51:04\", \"type\": \"log\", \"logger\": \"my-logger\", \"thread\": \"MainThread\", \"level\": \"DEBUG\", \"module\": \"my.module\", \"line_no\": 48}\n"
" ERROR Data processing error: Did not work \n"
"FileNotFoundError: [Errno 2] No such file or directory: '/local.json'\n"
Run Code Online (Sandbox Code Playgroud)
为了解析应用程序 JSON 日志,我们执行以下 LogQL 查询:
| json log="log"
| line_format "{{.log}}"
| json | line_format "{{.msg}}"
| __error__ != "JSONParserErr"
Run Code Online (Sandbox Code Playgroud)
正如我们的查询已经指出的那样,我们无法解析其他行日志,因为它们不是 JSON 格式。
我们能否根据条件定义不同的解析和格式?或者作为 JSONParserErr 发生时的后备?
不确定您是否设法得到了这个问题的答案,因为我想看看这是否可以在单个查询中实现,但是您可以通过多个查询来做到这一点\xe2\x80\xa6
\n对于 JSON 行
\n| json log="log" \n| line_format "{{.log}}"\n| json\n| line_format "{{.msg}}"\n| __error__ != "JSONParserErr"\n# \xe2\x80\xa6 more processing\nRun Code Online (Sandbox Code Playgroud)\n对于非 JSON 行\xe2\x80\xa6
\n{swarm_stack="apiv2-acme", swarm_service="apiv2-acme_tenant-import"}\n| json\n| __error__ = "JSONParserErr"\n| drop __error__\n# \xe2\x80\xa6 more processing\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4978 次 |
| 最近记录: |