mirror of
https://github.com/meineerde/rackstash.git
synced 2026-04-02 05:01:38 +00:00
Format UNIX timestamps in encoders and preserve pre-formatted Strings
This commit is contained in:
parent
a3d66deb7a
commit
a99f182715
@ -31,25 +31,30 @@ module Rackstash
|
|||||||
# @return [Hash] the given event with the `field` key set as an ISO 8601
|
# @return [Hash] the given event with the `field` key set as an ISO 8601
|
||||||
# formatted time string.
|
# formatted time string.
|
||||||
def normalize_timestamp(event, field = FIELD_TIMESTAMP, force: false) #:doc:
|
def normalize_timestamp(event, field = FIELD_TIMESTAMP, force: false) #:doc:
|
||||||
time = normalized_time(event[field])
|
time = normalized_timestamp(event[field])
|
||||||
time ||= Time.now.utc if force
|
time ||= Time.now.utc.iso8601(ISO8601_PRECISION) if force
|
||||||
|
|
||||||
event[field] = time.iso8601(ISO8601_PRECISION) if time
|
|
||||||
|
|
||||||
|
event[field] = time if time
|
||||||
event
|
event
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param time [Time, DateTime, Date, Integer, Float]
|
# @param time [Time, DateTime, Date, Integer, Float, String]
|
||||||
# @return [Time, nil] a `Time` object on UTC timezone if the given
|
# @return [String, nil] the passed time object tramsformed into an UTC
|
||||||
# `time` could be normalized as such or `nil` if the value does not
|
# timestamp (if possible) and formatted as an ISO 8601 formatted
|
||||||
# describe a timestamp
|
# String. If the object could not be interpreted as a time, we return
|
||||||
def normalized_time(time) #:doc:
|
# `nil`.
|
||||||
|
def normalized_timestamp(time) #:doc:
|
||||||
case time
|
case time
|
||||||
when ::Time, ::DateTime
|
when ::Time, ::DateTime
|
||||||
time = time.to_time
|
time = time.to_time
|
||||||
time.utc? ? time : time.getutc
|
utc_time = time.utc? ? time : time.getutc
|
||||||
|
utc_time.iso8601(ISO8601_PRECISION)
|
||||||
when ::Date
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -37,7 +37,17 @@ RSpec.describe Rackstash::Encoder::Helper::Timestamp do
|
|||||||
it 'formats a Date object' do
|
it 'formats a Date object' do
|
||||||
event['@timestamp'] = Date.new(2016, 10, 17)
|
event['@timestamp'] = Date.new(2016, 10, 17)
|
||||||
expect(helper.normalize_timestamp(event).fetch('@timestamp'))
|
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
|
end
|
||||||
|
|
||||||
it 'ignores an unset value by default' do
|
it 'ignores an unset value by default' do
|
||||||
@ -50,12 +60,6 @@ RSpec.describe Rackstash::Encoder::Helper::Timestamp do
|
|||||||
|
|
||||||
event['@timestamp'] = nil
|
event['@timestamp'] = nil
|
||||||
expect(helper.normalize_timestamp(event).fetch('@timestamp')).to eql 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
|
end
|
||||||
|
|
||||||
it 'uses the given field name' do
|
it 'uses the given field name' do
|
||||||
@ -81,7 +85,7 @@ RSpec.describe Rackstash::Encoder::Helper::Timestamp do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'uses the current time for unknown values' do
|
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'))
|
expect(helper.normalize_timestamp(event, force: true).fetch('@timestamp'))
|
||||||
.to eql '2016-10-17T10:37:00.000000Z'
|
.to eql '2016-10-17T10:37:00.000000Z'
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user