英文:
Parse LogQL substring as JSON
问题
可以将| json
应用于日志行的子字符串吗?
示例日志行
2023-07-17T19:24:51: {"msg":"LOGIN_SUCCESS","meta":{"userNameOrEmail":"spencer@gmail.com","user":{"id":1611561848468,"user_group_id":1530117853159,"username":null,"primary_email":"spencer@gmail.com","num_additional_groups":1},"user_groups":[{"id":1110467853159,"name":"Demo"},{"id":1657113131838,"name":"Agency"}]}}
当前的LogQL
{job="pm2", filename=~".*server-out.*.log"} |~ `{"msg":.*`
我想解析JSON子字符串,以便可以按消息、ID等进行分组
我尝试做的bash等效操作是:
echo -n '2023-07-17T19:24:51: {"msg":"LOGIN_SUCCESS","meta":{"userNameOrEmail":"spencer@gmail.com","user":{"id":1611561848468,"user_group_id":1530117853159,"username":null,"primary_email":"spencer@gmail.com","num_additional_groups":1},"user_groups":[{"id":1110467853159,"name":"Demo"},{"id":1657113131838,"name":"Agency"}]}}' \
| grep -Eo '{"msg":.*' \
| jq
英文:
Is it possible to apply | json
to a substring of a log line?
Example Log line
2023-07-17T19:24:51: {"msg":"LOGIN_SUCCESS","meta":{"userNameOrEmail":"spencer@gmail.com","user":{"id":1611561848468,"user_group_id":1530117853159,"username":null,"primary_email":"spencer@gmail.com","num_additional_groups":1},"user_groups":[{"id":1110467853159,"name":"Demo"},{"id":1657113131838,"name":"Agency"}]}}
Current LogQL
{job="pm2", filename=~".*server-out.*.log"} |~ `{"msg":.*`
I would like to parse JSON substring so I can group by the message, IDs, etc
The bash equivalent of what I'm trying to do is:
echo -n '2023-07-17T19:24:51: {"msg":"LOGIN_SUCCESS","meta":{"userNameOrEmail":"spencer@gmail.com","user":{"id":1611561848468,"user_group_id":1530117853159,"username":null,"primary_email":"spencer@gmail.com","num_additional_groups":1},"user_groups":[{"id":1110467853159,"name":"Demo"},{"id":1657113131838,"name":"Agency"}]}}' \
| grep -Eo '{"msg":.*' \
| jq
答案1
得分: 1
可以的:
- 将日志中的 JSON 部分提取到标签中,
- 使用
line_format
输出提取的部分, - 将结果输出到
json
中
{job="pm2", filename=~".*server-out.*.log"}
| 正则表达式匹配 `(?P<json_part>\{"msg":.*)`
| 行格式化 {{.json_part}}
| JSON
英文:
Yes you can do that:
- Extract json part of your log into label,
- use
line_format
to output extracted part - output result into
json
{job="pm2", filename=~".*server-out.*.log"}
| regexp `(?P<json_part>\{"msg":.*)`
| line_format {{.json_part}}
| json
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论