Skip to content

Commit

Permalink
Ensure backwards compatibility when default log formatter is not used
Browse files Browse the repository at this point in the history
In order for changes in zshannon#12 to not cause breakage, it assumes that the default
formatter is used, or that a custom formatter would have `epoch_time` and `message`
available on the `Hash` object.

This was surfaced by: zshannon#16

These changes leave the default formatter changes in place, but ensure that the
`timestamp` and `message` values on the log events revert to the original values
prior to zshannon#12 if the message on the queue is not a `Hash` and does not contain
`epoch_time` and `message` values. This will allow backwards compatibility and
also allow for custom formatters to still pass these values in if they wish to
benefit from the timestamp disparity.
  • Loading branch information
coderbydesign committed Sep 4, 2020
1 parent df53ef5 commit 9a5fb1e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/cloudwatchlogger/client/aws_sdk/threaded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ def initialize(credentials, log_group_name, log_stream_name, opts = {})
event = {
log_group_name: @log_group_name,
log_stream_name: @log_stream_name,
log_events: [{
timestamp: message_object[:epoch_time],
message: message_object[:message]
}]
log_events: [log_event(message_object)]
}
event[:sequence_token] = @sequence_token if @sequence_token
response = @client.put_log_events(event)
Expand Down Expand Up @@ -108,10 +105,22 @@ def connect!(opts = {})
log_group_name: @log_group_name
)
retry
rescue Aws::CloudWatchLogs::Errors::ResourceAlreadyExistsException,
rescue Aws::CloudWatchLogs::Errors::ResourceAlreadyExistsException,
Aws::CloudWatchLogs::Errors::AccessDeniedException
end
end

def log_event(message_object)
timestamp = (Time.now.utc.to_f.round(3) * 1000).to_i
message = message_object

if message_object.is_a?(Hash) && %i[epoch_time message].all?{ |s| message_object.key?(s) }
timestamp = message_object[:epoch_time]
message = message_object[:message]
end

{ timestamp: timestamp, message: message }
end
end
end
end
Expand Down

0 comments on commit 9a5fb1e

Please sign in to comment.