【问题标题】:How to make Logstash multiline filter merge lines based on some dynamic field value?如何根据一些动态字段值使 Logstash 多行过滤器合并行?
【发布时间】:2023-03-13 02:12:01
【问题描述】:

我是 logstash 的新手,很想为其中一个用例设置 ELK。我发现这个问题与我的Why won't Logstash multiline merge lines based on grok'd field? 相关 如果多行过滤器不合并 grok 字段上的行,那么如何合并以下日志示例中的第 2 行和第 10 行?请帮忙。

使用 grok 模式,我创建了一个字段 'id',它的值是 715。

Line1 - 5/08/06 00:10:35.348 [BaseAsyncApi] [qtp19303632-51]: INFO: [714] CMDC flowcxt=[55c2a5fbe4b0201c2be31e35] method=contentdetail uri=http://10.126.44.161:5600/cmdc/content/programid%3A%2F%2F317977349~programid%3A%2F%2F9?lang=eng&catalogueId=30&region=3000~3001&pset=pset_pps header={}   
Line2 - 2015/08/06 00:10:35.348 [BaseAsyncApi] [qtp19303632-53]: INFO: [715] CMDC flowcxt=[55c2a5fbe4b0201c2be31e36] method=contentdetail uri=http://10.126.44.161:5600/cmdc/content/programid%3A%2F%2F1640233758~programid%3A%2F%2F1073741829?lang=eng&catalogueId=30&region=3000~3001&pset=pset_pps header={}   
Line3 - 2015/08/06 00:10:35.349 [TWCAsyncProcessor] [TWC-pool-3-thread-2]: INFO: [714:426] TWC request=MercurySortRequest   
Line4 - 2015/08/06 00:10:35.349 [TWCAsyncProcessor] [TWC-pool-3-thread-1]: INFO: [715:427] TWC request=MercurySortRequest   
Line5 - 2015/08/06 00:10:35.352 [BaseAsyncApi] [qtp19303632-54]: INFO: [716] CMDC flowcxt=[55c2a5fbe4b0201c2be31e37] method=contentdetail uri=http://10.126.44.161:5600/cmdc/content/programid%3A%2F%2F2144942810~programid%3A%2F%2F1953281601?lang=eng&catalogueId=30&region=3000~3001&pset=pset_pps header={}   
Line6 - 2015/08/06 00:10:35.354 [TWCAsyncProcessor] [TWC-pool-3-thread-1]: INFO: [716:428] TWC request=MercurySortRequest   
Line7 - 2015/08/06 00:10:35.359 [BaseAsyncApi] [qtp19303632-49]: INFO: [717] CMDC flowcxt=[55c2a5fbe4b0201c2be31e38] method=contentdetail uri=http://10.126.44.161:5600/cmdc/content/programid%3A%2F%2F2144942448~programid%3A%2F%2F2147355770?lang=eng&catalogueId=30&region=3000~3001&pset=pset_pps header={}   
Line8 - 2015/08/06 00:10:35.360 [TWCAsyncProcessor] [TWC-pool-3-thread-2]: INFO: [717:429] TWC request=MercurySortRequest   
Line9 - 2015/08/06 00:10:35.366 [TWCAsyncProcessor$TWCAsyncProcessorCallback$ReceiveCallback] [CMDC-pool-2-thread-41]: INFO: [715:427] TWC response status=200 hits=1 time=17 internal=10.42   
Line10 - 2015/08/06 00:10:35.367 [BaseAsyncApi] [CMDC-pool-2-thread-41]: INFO: [715] CMDC response status=200 CMDC=19ms TWC=17ms #TWC=1

【问题讨论】:

    标签: logstash logstash-grok logstash-configuration elastic-stack logstash-file


    【解决方案1】:

    您需要使用带有stream_identity 集的multiline 过滤器。文档here 并不清楚它的用途,但您的基本策略是这样的:

    if (!"multiline" in [tags]) {
      grok { // parse out your identity field }
      multiline { 
        stream_identity => "%{id}"
        pattern => "." // match anything because we're gathering by id field
        what => "previous"
        periodic_flush => true
        max_age => 5 // however many seconds it takes to get all of your lines together
        add_tags => ["multiline" ]
      }
    } else {
      // process multiline event that's been flushed
    }
    

    自从 1.5 发布以来,我还没有尝试过这样的事情,但是文档说它应该可以工作(在 1.4.2 和之前的版本中,刷新机制不起作用,所以你可能会丢失事件)。

    【讨论】:

    • 感谢您的回答。您的建议有效,我能够合并这些行。我正在做一个 poc,看看 ELK 如何为我们工作。
    • 我还遇到了一个问题,即刷新可能会导致消息碎片化。目前,我正在处理静态测试数据,并没有遇到这个问题。这里有 Mulitline 的替代版本 (github.com/johnarnold/logstash-multiline)。一定要试一试。
    • 我相信刷新机制是在 1.4.2 和 1.5 之间“修复”的。在 1.4.2 中,刷新是实验性的,并以固定速率(即每 5 秒)刷新......我认为在较新的版本中,它基于 max_age 刷新 - 即自上次事件以来的许多秒添加了一个新的线。我认为在大多数情况下,这会很好。我还写了一个 multiline_fixed 插件——如果你知道第一行和最后一行的样子,它会在到达最后一行时刷新——不过我现在不能动手:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2015-03-24
    • 2019-07-03
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多