Class: Trailer::Storage::CloudWatch

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/trailer/storage/cloud_watch.rb

Instance Method Summary collapse

Constructor Details

#initializeCloudWatch

Constructor.



12
13
14
15
16
17
# File 'lib/trailer/storage/cloud_watch.rb', line 12

def initialize
  self.messages = []
  self.client   = Aws::CloudWatchLogs::Client.new(region: Trailer.config.aws_region, credentials: credentials)
  ensure_log_group
  ensure_log_stream
end

Instance Method Details

#flushObject

Sends all of the queued messages to CloudWatch, and resets the messages queue.

See stackoverflow.com/a/36901509



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trailer/storage/cloud_watch.rb', line 32

def flush
  return if messages.empty?

  events = {
    log_group_name:  Trailer.config.application_name,
    log_stream_name: Trailer.config.application_name,
    log_events:      messages,
    sequence_token:  sequence_token,
  }

  response            = client.put_log_events(events)
  self.sequence_token = response&.next_sequence_token
  self.messages       = []
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.

Parameters:

  • data (Hash)

    A key-value hash of trace data to write to storage.



22
23
24
25
26
27
# File 'lib/trailer/storage/cloud_watch.rb', line 22

def write(data)
  messages << {
    timestamp: (Time.now.utc.to_f.round(3) * 1000).to_i,
    message:   data&.to_json,
  }.compact
end