While trying to figure out how to make indices rollover at midnight local time, I found a bunch of people asking how to do it but no clear solution.
The ruby code below generates a YYYY.MM.dd formatted string based on @timestamp in the specified timezone (in my case Australia/Melbourne). The offset is calculated in a separate step to be transparent with daylight savings.
Then -%{[@metadata][index_day]} can be used in elasticsearch output instead of %{+YYYY.MM.dd}.
I had a bit of a look at the rollover API, and I suspect xpack's ILM might take care of this use case, but I wanted something simple that Just Works with Logstash OSS.
You can use date filter with logstash to grand any event date from source and map it to @timestamp, if no source field with the date logstash will populate the @timestamp with the system date
## Convert event date time into @timestamp
date {
match => ["TimeStamp", "yyyyMMddHHmmss"]
timezone => "UTC"
target => "@timestamp"
}
Then in the output, append any date format to your index name
elasticsearch {
hosts => "http://localhost:9200"
index => "index-name-%{+YYYY-MM-dd}"
}
You could parse it once telling the date filter it is UTC, then parse again as local time, but that loses the DST handling
date { match => [ "message", "YYYY-MM-dd HH:mm" ] }
mutate { add_field => { "[@metadata][indexName]" => "%{+YYYY.MM.dd}" } }
date { match => [ "message", "YYYY-MM-dd HH:mm" ] timezone => "Etc/GMT+4" }
I think you have to parse it as the local timezone to get the right DST handling, so the question you are really asking is how to get ruby (not ruby on rails) to display a timestamp in a non-local timezone. I do not know the answer to that.
My post was a solution to this problem, rather than a question. Is there a problem with it I'm not seeing? (apart from not handling sub-hour TZ offsets in its present form)
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.