无法将多行事件从Filebeat S3输入推送到Logstash

问题描述

我正在ELK上进行PoC,遇到一个问题。我看了很多关于describe.elastic.co和StackOverflow的主题,但似乎没有帮助。

我试图通过Filebeat(使用S3输入)配置多行事件,并在Logstash中使用它们。我面临的问题是,即使在Filebeat中设置了多行配置之后,我仍将Stacktrace的行视为Logstash中的单个事件。

由于Logstash并非将堆栈跟踪的行作为单个行接收,而是作为单独的行接收,因此最终导致_grokparsefailure,这是完全可以理解的,因为FB在将它们发送到Logstash。

其他单行事件有望正常运行,我可以在Kibana上看到它们。

filebeat.yml:

filebeat.inputs:

  - type: s3
    queue_url: https://sqs.aaaaa.amazonaws.com/xxxxxxxx/zzzzzz
    visibility_timeout: 300s
    multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
    multiline.negate: true
    multiline.match: after

Logstash配置:

input {
  beats {
    port => 5044
    host => "0.0.0.0"
  }
}

filter {
  grok {
    match => {"message" => "%{TIMESTAMP_ISO8601:timestamp} %{GREEDYDATA:logType} %{LOGLEVEL:logLevel}%{SPACE}\[%{GREEDYDATA:key1}\] \[%{GREEDYDATA:key2}\] \[%{GREEDYDATA:key3}\] \[%{GREEDYDATA:sourceIP}\] %{GREEDYDATA:message}"}
    overwrite => [ "message" ]
  } 

 date {
    match => ["timestamp","yyyy-MM-dd HH:mm:ss,SSS"]
  }
}

以下是两个示例日志语句,我正在尝试将其中的第二个语句合并为一个事件:

2020-08-18 00:30:52,481 detailed_logs ERROR    [abc] [xyz] [def] [127.0.0.1] Exception raised. Trace:
2020-08-18 00:30:52,483 detailed_logs ERROR    Traceback (most recent call last):
  File "/Users/vvv/Documents/ttt.py",line 93,in get
    x = y.perform(abc)
  File "/Users/vvv/Documents/ttt.py",line 283,in operate
    raise exception
  File "/Users/vvv/Documents/ttt.py",line 169,in operate
    d["abb"] = n["xy"]
AttributeError: 'model' object has no attribute 'create1d_on'

我的直觉告诉我,由于我无法在official doc中找到相同的内容,因此Filebeat S3输入可能不支持多行,而Log input counterpart显然提到了相同。但是话又说回来,我可能是错的。

感谢您朝着正确的方向前进。

解决方法

我正在回答我自己的问题,并将首先说这绝不是对我提出的问题的有效答案,但是,一种解决方法可以解决我的迫切要求,并且现已部署。

由于S3输入的Filebeat多行不能按预期方式工作,并且Logstash多行编解码器不是Elastic强烈推荐的(标记为IMPORTANT here的段落),所以我最终弄平了整个应用程序的堆栈跟踪为此创建具有以下近似结构的实用程序:

dictionary = {}
counter = 0

for line in lines:
    if line and line.strip():
        dictionary[counter] = line.strip()
        counter += 1

return json.dumps(dictionary)

使用traceback.format_exc()将异常转储到日志中时,该跟踪将作为参数传递给上述实用程序,然后记录为ERROR。

可以肯定的是,在整个应用程序范围内进行更改都是手工操作,但是现在,根据要求,当在Kibana中查看时,以下构造将作为单个事件出现:

{"0": "Traceback (most recent call last):","1": "File "/Users/vvv/Documents/ttt.py",line 93,in get","2": "x = y.perform(abc)","3": "File "/Users/vvv/Documents/ttt.py",line 283,in operate","4": "raise exception","5": "File "/Users/vvv/Documents/ttt.py",line 169,"6": "d["abb"] = n["xy"]","7": "AttributeError: 'model' object has no attribute 'create1d_on'"}

欢迎任何反馈,建议和建议。