解析 LogQL 子串为 JSON

huangapple go评论53阅读模式
英文:

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

可以的:

  1. 将日志中的 JSON 部分提取到标签中,
  2. 使用 line_format 输出提取的部分,
  3. 将结果输出到 json
{job="pm2", filename=~".*server-out.*.log"}
 | 正则表达式匹配 `(?P<json_part>\{"msg":.*)`
 | 行格式化 {{.json_part}}
 | JSON
英文:

Yes you can do that:

  1. Extract json part of your log into label,
  2. use line_format to output extracted part
  3. output result into json
{job=&quot;pm2&quot;, filename=~&quot;.*server-out.*.log&quot;}
 | regexp `(?P&lt;json_part&gt;\{&quot;msg&quot;:.*)`
 | line_format {{.json_part}}
 | json

huangapple
  • 本文由 发表于 2023年7月18日 03:31:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定