1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Format UNIX timestamps in encoders and preserve pre-formatted Strings

This commit is contained in:
Holger Just 2018-05-16 21:36:24 +02:00
parent a3d66deb7a
commit a99f182715
2 changed files with 28 additions and 19 deletions

View File

@ -31,25 +31,30 @@ module Rackstash
# @return [Hash] the given event with the `field` key set as an ISO 8601
# formatted time string.
def normalize_timestamp(event, field = FIELD_TIMESTAMP, force: false) #:doc:
time = normalized_time(event[field])
time ||= Time.now.utc if force
event[field] = time.iso8601(ISO8601_PRECISION) if time
time = normalized_timestamp(event[field])
time ||= Time.now.utc.iso8601(ISO8601_PRECISION) if force
event[field] = time if time
event
end
# @param time [Time, DateTime, Date, Integer, Float]
# @return [Time, nil] a `Time` object on UTC timezone if the given
# `time` could be normalized as such or `nil` if the value does not
# describe a timestamp
def normalized_time(time) #:doc:
# @param time [Time, DateTime, Date, Integer, Float, String]
# @return [String, nil] the passed time object tramsformed into an UTC
# timestamp (if possible) and formatted as an ISO 8601 formatted
# String. If the object could not be interpreted as a time, we return
# `nil`.
def normalized_timestamp(time) #:doc:
case time
when ::Time, ::DateTime
time = time.to_time
time.utc? ? time : time.getutc
utc_time = time.utc? ? time : time.getutc
utc_time.iso8601(ISO8601_PRECISION)
when ::Date
::Time.new(time.year, time.month, time.day, 0, 0, 0, 0).utc
time.iso8601
when Integer, Float
Time.at(time.to_f).utc.iso8601(ISO8601_PRECISION)
when String
time.to_s
end
end
end

View File

@ -37,7 +37,17 @@ RSpec.describe Rackstash::Encoder::Helper::Timestamp do
it 'formats a Date object' do
event['@timestamp'] = Date.new(2016, 10, 17)
expect(helper.normalize_timestamp(event).fetch('@timestamp'))
.to eql '2016-10-17T00:00:00.000000Z'
.to eql '2016-10-17'
end
it 'formats a UNIX timestamp' do
event['@timestamp'] = 1526499065
expect(helper.normalize_timestamp(event).fetch('@timestamp'))
.to eql '2018-05-16T19:31:05.000000Z'
event['@timestamp'] = 1526499065.4177752
expect(helper.normalize_timestamp(event).fetch('@timestamp'))
.to eql '2018-05-16T19:31:05.417775Z'
end
it 'ignores an unset value by default' do
@ -50,12 +60,6 @@ RSpec.describe Rackstash::Encoder::Helper::Timestamp do
event['@timestamp'] = nil
expect(helper.normalize_timestamp(event).fetch('@timestamp')).to eql nil
event['@timestamp'] = 123
expect(helper.normalize_timestamp(event).fetch('@timestamp')).to eql 123
event['@timestamp'] = 3.14
expect(helper.normalize_timestamp(event).fetch('@timestamp')).to eql 3.14
end
it 'uses the given field name' do
@ -81,7 +85,7 @@ RSpec.describe Rackstash::Encoder::Helper::Timestamp do
end
it 'uses the current time for unknown values' do
event['@timestamp'] = 'string'
event['@timestamp'] = Object.new
expect(helper.normalize_timestamp(event, force: true).fetch('@timestamp'))
.to eql '2016-10-17T10:37:00.000000Z'