在Logstash中转换时间戳时区以获取输出索引名称
在我的场景中,Logstash接收的syslog行的“timestamp”是UTC,我们在Elasticsearch输出中使用事件“timestamp”:
output { elasticsearch { embedded => false host => localhost port => 9200 protocol => http cluster => 'elasticsearch' index => "syslog-%{+YYYY.MM.dd}" } }
我的问题是,在UTC午夜,Logstash在一天结束之前在时区(GMT-4 => America / Montreal)发送日志到不同的索引,并且由于“时间戳”,索引在20h(晚上8点)之后没有日志“是UTC。
我们已经完成了转换时区的工作,但我们遇到了显着的性能下降:
filter { mutate { add_field => { # Create a new field with string value of the UTC event date "timestamp_zoned" => "%{@timestamp}" } } date { # Parse UTC string value and convert it to my timezone into a new field match => [ "timestamp_zoned", "yyyy-MM-dd HH:mm:ss Z" ] timezone => "America/Montreal" locale => "en" remove_field => [ "timestamp_zoned" ] target => "timestamp_zoned_obj" } ruby { # Output the zoned date to a new field code => "event['index_day'] = event['timestamp_zoned_obj'].strftime('%Y.%m.%d')" remove_field => [ "timestamp_zoned_obj" ] } } output { elasticsearch { embedded => false host => localhost port => 9200 protocol => http cluster => 'elasticsearch' # Use of the string value index => "syslog-%{index_day}" } }
有没有办法优化此配置?
这是优化配置,请尝试并测试性能。
您不需要使用mutate
和date
插件。 直接使用ruby
插件。
input { stdin { } } filter { ruby { code => " event['index_day'] = event['@timestamp'].localtime.strftime('%Y.%m.%d') " } } output { stdout { codec => rubydebug } }
输出示例:
{ "message" => "test", "@version" => "1", "@timestamp" => "2015-03-30T05:27:06.310Z", "host" => "BEN_LIM", "index_day" => "2015.03.29" }
在1.5.0版中,我们可以按索引名称的本地时区转换时间戳。 这是我的配置:
filter { ruby { code => "event['index_day'] = event.timestamp.time.localtime.strftime('%Y.%m.%d')" } } output { elasticsearch { host => localhost index => "thrall-%{index_day}" } }
在Logstash V5.0.2中,API已被修改。 我们可以通过本地时区为时间戳转换索引名称。 这是我的配置:
filter { ruby { code => "event['index_day'] = event.timestamp.time.localtime.strftime('%Y.%m.%d')" } }
在logstash 5.0及更高版本中 ,您可以使用以下命令:
filter{ ruby { code => "event.set('index_day', event.get('[@timestamp]').time.localtime.strftime('%Y%m%d'))" } }
- ElasticSearch对整个字段进行聚合
- 如何使用“Elasticsearch”gem专门将elasticsearch与rails应用程序集成
- Rails 5:如何在弹性搜索中导入数据并执行条件搜索?
- 使用rest-client ruby gem在elasticsearch get请求中传递json数据
- Rails – 如何在searchkick中为filter添加更多字段?
- 如何使用elasticsearch / searchkick在我的导航栏上运行搜索栏并在特定页面中显示结果?
- Elasticsearch – 具有多匹配的范围filter?
- Rails elasticsearch – 命名范围搜索