Promtail:如何从日志中删除非 JSON 部分

发布于 2025-01-17 07:51:18 字数 2992 浏览 2 评论 0原文

我有多行日志,其中包含正确的 json 部分(一行或多行),后面是堆栈跟踪。 是否可以将日志的第一部分解析为 json,并为堆栈跟踪创建新标签(例如“stackTrace”)并将第一部分之后的所有行都放在那里?

不幸的是,日志可能包含不同数量的 json 格式字段,因此不太可能使用正则表达式来解析它们。

{ "timestamp" : "2022-03-28 14:33:00,000", "logger" : "appLog", "level" : "ERROR", "thread" : "ktor-8080", "url" : "/path","method" : "POST","httpStatusCode" : 400,"callId" : "f7a22bfb1466","errorMessage" : "Unexpected JSON token at offset 184: Encountered an unknown key 'a'. Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys. JSON input: {     \"entityId\" : \"TGT-8c8d950036bf\",     \"processCode\" : \"test\",     \"tokenType\" : \"SSO_CCOM\",     \"ttlMills\" : 600000,     \"a\" : \"a\" }" }
    com.example.info.core.WebApplicationException: Unexpected JSON token at offset 184: Encountered an unknown key 'a'.
    Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
    JSON input: {
        "entityId" : "TGT-8c8d950036bf",
        "processCode" : "test",
        "tokenType" : "SSO_CCOM",
        "ttlMills" : 600000,
        "a" : "a"
    }
       at com.example.info.signtoken.SignTokenApi$signTokenModule$2$1$1.invokeSuspend(SignTokenApi.kt:94)
       at com.example.info.signtoken.SignTokenApi$signTokenModule$2$1$1.invoke(SignTokenApi.kt)
       at com.example.info.signtoken.SignTokenApi$signTokenModule$2$1$1.invoke(SignTokenApi.kt)
       at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:248)
       at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:116)
       at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:136)
       at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:78)
       at io.ktor.routing.Routing.executeResult(Routing.kt:155)
       at io.ktor.routing.Routing.interceptor(Routing.kt:39)
       at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:107)
       at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
       at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)

UPD。 我已经像这样制作了 promtail 管道

scrape_configs:
  - job_name: Test_AppLog
    static_configs:
    - targets:
        - ${HOSTNAME}
      labels:
        job: INFO-Test_AppLog
        host: ${HOSTNAME}
        __path__: /home/adm_web/app.log
    pipeline_stages:
    - multiline:
        firstline: ^\{\s?\"timestamp\"
        max_lines: 128
        max_wait_time: 1s
    - match:
        selector: '{job="INFO-Test_AppLog"}'
        stages:
        - regex:
            expression: '(?P<log>^\{ ?\"timestamp\".*\}[\s])(?s)(?P<stacktrace>.*)'
        - labels:
            log:
            stacktrace:
        - json:
            expressions:
              logger: logger
              url: url
              method: method
              statusCode: httpStatusCode
              sla: sla
            source: log

但事实上,json 配置块不起作用,Grafana 中的结果只有两个字段 - log 和 stacktrace。

任何帮助将不胜感激

I have multiline log that consists correct json part (one or more lines), and after it - stack trace.
Is it possile to parse first part of the log as json, and for stack-trace make new label ("stackTrace" for example) and put there all the lines after first part?

Unfortunately, logs can contain a different number of fields in json format, and therefore it is unlikely to parse them using regex.

{ "timestamp" : "2022-03-28 14:33:00,000", "logger" : "appLog", "level" : "ERROR", "thread" : "ktor-8080", "url" : "/path","method" : "POST","httpStatusCode" : 400,"callId" : "f7a22bfb1466","errorMessage" : "Unexpected JSON token at offset 184: Encountered an unknown key 'a'. Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys. JSON input: {     \"entityId\" : \"TGT-8c8d950036bf\",     \"processCode\" : \"test\",     \"tokenType\" : \"SSO_CCOM\",     \"ttlMills\" : 600000,     \"a\" : \"a\" }" }
    com.example.info.core.WebApplicationException: Unexpected JSON token at offset 184: Encountered an unknown key 'a'.
    Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
    JSON input: {
        "entityId" : "TGT-8c8d950036bf",
        "processCode" : "test",
        "tokenType" : "SSO_CCOM",
        "ttlMills" : 600000,
        "a" : "a"
    }
       at com.example.info.signtoken.SignTokenApi$signTokenModule$2$1$1.invokeSuspend(SignTokenApi.kt:94)
       at com.example.info.signtoken.SignTokenApi$signTokenModule$2$1$1.invoke(SignTokenApi.kt)
       at com.example.info.signtoken.SignTokenApi$signTokenModule$2$1$1.invoke(SignTokenApi.kt)
       at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:248)
       at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:116)
       at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:136)
       at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:78)
       at io.ktor.routing.Routing.executeResult(Routing.kt:155)
       at io.ktor.routing.Routing.interceptor(Routing.kt:39)
       at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:107)
       at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
       at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)

UPD.
I've made promtail pipeline like so

scrape_configs:
  - job_name: Test_AppLog
    static_configs:
    - targets:
        - ${HOSTNAME}
      labels:
        job: INFO-Test_AppLog
        host: ${HOSTNAME}
        __path__: /home/adm_web/app.log
    pipeline_stages:
    - multiline:
        firstline: ^\{\s?\"timestamp\"
        max_lines: 128
        max_wait_time: 1s
    - match:
        selector: '{job="INFO-Test_AppLog"}'
        stages:
        - regex:
            expression: '(?P<log>^\{ ?\"timestamp\".*\}[\s])(?s)(?P<stacktrace>.*)'
        - labels:
            log:
            stacktrace:
        - json:
            expressions:
              logger: logger
              url: url
              method: method
              statusCode: httpStatusCode
              sla: sla
            source: log

But in fact, json config block does not work, the result in Grafana is only two fields - log and stacktrace.

Any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2025-01-24 07:51:18

如果样式总是这样,也许最简单的方法是分析整个日志字符串,找到最后一个符号“}”的索引 - 然后使用其索引+1分割字符串,结果应该位于输出数组的第一部分

if the style is constantly like this maybe the easiest way is to analyze whole log string find index of last symbol "}" - then split the string using its index+1 and result should be in the first part of output array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文