使用或不使用 Logstash 过滤 Filebeat 输入
Filtering Filebeat input with or without Logstash
在我们当前的设置中,我们使用 Filebeat 将日志发送到 Elasticsearch 实例。应用程序日志采用 JSON 格式并在 AWS 中运行。
出于某种原因,AWS 决定在新平台版本中为日志行添加前缀,但现在日志解析不起作用。
Apr 17 06:33:32 ip-172-31-35-113 web: {"@timestamp":"2020-04-17T06:33:32.691Z","@version":"1","message":"Tomcat started on port(s): 5000 (http) with context path ''","logger_name":"org.springframework.boot.web.embedded.tomcat.TomcatWebServer","thread_name":"main","level":"INFO","level_value":20000}
之前是:
{"@timestamp":"2020-04-17T06:33:32.691Z","@version":"1","message":"Tomcat started on port(s): 5000 (http) with context path ''","logger_name":"org.springframework.boot.web.embedded.tomcat.TomcatWebServer","thread_name":"main","level":"INFO","level_value":20000}
问题是我们是否可以避免使用 Logstash 将日志行转换为旧格式?如果没有,我该如何删除前缀?哪个过滤器是最好的选择?
我当前的 Filebeat 配置如下所示:
filebeat.inputs:
- type: log
paths:
- /var/log/web-1.log
json.keys_under_root: true
json.ignore_decoding_error: true
json.overwrite_keys: true
fields_under_root: true
fields:
environment: ${ENV_NAME:not_set}
app: myapp
cloud.id: "${ELASTIC_CLOUD_ID:not_set}"
cloud.auth: "${ELASTIC_CLOUD_AUTH:not_set}"
我会尝试利用 dissect
and decode_json_fields
处理器:
processors:
# first ignore the preamble and only keep the JSON data
- dissect:
tokenizer: "%{?ignore} %{+ignore} %{+ignore} %{+ignore} %{+ignore}: %{json}"
field: "message"
target_prefix: ""
# then parse the JSON data
- decode_json_fields:
fields: ["json"]
process_array: false
max_depth: 1
target: ""
overwrite_keys: false
add_error_key: true
Logstash 中有一个名为 JSON filter 的插件,它在一个名为 "message" 的字段中包含所有原始日志行(例如)。
filter {
json {
source => "message"
}
}
如果您不想包含行的开头部分,请使用 Logstash 中的 dissect filter。应该是这样的:
filter {
dissect {
mapping => {
"message" => "%{}: %{message_without_prefix}"
}
}
}
也许在Filebeat 中也可以使用这两个功能。但根据我的经验,我更喜欢在 parsing/manipulating 记录数据时使用 Logstash。
在我们当前的设置中,我们使用 Filebeat 将日志发送到 Elasticsearch 实例。应用程序日志采用 JSON 格式并在 AWS 中运行。
出于某种原因,AWS 决定在新平台版本中为日志行添加前缀,但现在日志解析不起作用。
Apr 17 06:33:32 ip-172-31-35-113 web: {"@timestamp":"2020-04-17T06:33:32.691Z","@version":"1","message":"Tomcat started on port(s): 5000 (http) with context path ''","logger_name":"org.springframework.boot.web.embedded.tomcat.TomcatWebServer","thread_name":"main","level":"INFO","level_value":20000}
之前是:
{"@timestamp":"2020-04-17T06:33:32.691Z","@version":"1","message":"Tomcat started on port(s): 5000 (http) with context path ''","logger_name":"org.springframework.boot.web.embedded.tomcat.TomcatWebServer","thread_name":"main","level":"INFO","level_value":20000}
问题是我们是否可以避免使用 Logstash 将日志行转换为旧格式?如果没有,我该如何删除前缀?哪个过滤器是最好的选择?
我当前的 Filebeat 配置如下所示:
filebeat.inputs:
- type: log
paths:
- /var/log/web-1.log
json.keys_under_root: true
json.ignore_decoding_error: true
json.overwrite_keys: true
fields_under_root: true
fields:
environment: ${ENV_NAME:not_set}
app: myapp
cloud.id: "${ELASTIC_CLOUD_ID:not_set}"
cloud.auth: "${ELASTIC_CLOUD_AUTH:not_set}"
我会尝试利用 dissect
and decode_json_fields
处理器:
processors:
# first ignore the preamble and only keep the JSON data
- dissect:
tokenizer: "%{?ignore} %{+ignore} %{+ignore} %{+ignore} %{+ignore}: %{json}"
field: "message"
target_prefix: ""
# then parse the JSON data
- decode_json_fields:
fields: ["json"]
process_array: false
max_depth: 1
target: ""
overwrite_keys: false
add_error_key: true
Logstash 中有一个名为 JSON filter 的插件,它在一个名为 "message" 的字段中包含所有原始日志行(例如)。
filter {
json {
source => "message"
}
}
如果您不想包含行的开头部分,请使用 Logstash 中的 dissect filter。应该是这样的:
filter {
dissect {
mapping => {
"message" => "%{}: %{message_without_prefix}"
}
}
}
也许在Filebeat 中也可以使用这两个功能。但根据我的经验,我更喜欢在 parsing/manipulating 记录数据时使用 Logstash。