Class: Trailer::Storage::CloudWatch
- Inherits:
-
Object
- Object
- Trailer::Storage::CloudWatch
- Includes:
- Concurrent::Async
- Defined in:
- lib/trailer/storage/cloud_watch.rb
Instance Method Summary collapse
-
#flush ⇒ Object
Sends all of the queued messages to CloudWatch, and resets the messages queue.
-
#initialize ⇒ CloudWatch
constructor
Constructor.
-
#write(data) ⇒ Object
Queues the given hash for writing to CloudWatch.
Constructor Details
#initialize ⇒ CloudWatch
Constructor.
12 13 14 15 16 17 |
# File 'lib/trailer/storage/cloud_watch.rb', line 12 def initialize self. = [] self.client = Aws::CloudWatchLogs::Client.new(region: Trailer.config.aws_region, credentials: credentials) ensure_log_group ensure_log_stream end |
Instance Method Details
#flush ⇒ Object
Sends all of the queued messages to CloudWatch, and resets the messages queue.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/trailer/storage/cloud_watch.rb', line 22 def flush return if .empty? events = { log_group_name: Trailer.config.application_name, log_stream_name: Trailer.config.application_name, log_events: , sequence_token: sequence_token, } response = client.put_log_events(events) self.sequence_token = response&.next_sequence_token self. = [] rescue Aws::CloudWatchLogs::Errors::InvalidSequenceTokenException # Only one client at a time can write to the log. If another client has written before we get a chance, # the sequence token is invalidated, and we need to get a new one. self.sequence_token = log_stream[:upload_sequence_token] retry end |
#write(data) ⇒ Object
Queues the given hash for writing to CloudWatch.
45 46 47 48 49 50 51 52 |
# File 'lib/trailer/storage/cloud_watch.rb', line 45 def write(data) return if data.empty? << { timestamp: (Time.now.utc.to_f.round(3) * 1000).to_i, message: data&.to_json, }.compact end |