Class: AwsLogs::Tail

Inherits:
Base
  • Object
show all
Defined in:
lib/aws_logs/tail.rb

Constant Summary collapse

@@waiting_already_shown =
false
@@global_end_loop_signal =

For backwards compatibility. This is not thread-safe.

false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AwsServices

#cloudwatchlogs

Constructor Details

#initialize(options = {}) ⇒ Tail

Returns a new instance of Tail.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/aws_logs/tail.rb', line 6

def initialize(options = {})
  super
  # Setting to ensure matches default CLI option
  @follow = @options[:follow].nil? ? true : @options[:follow]
  @refresh_rate = @options[:refresh_rate] || 2
  @wait_exists = @options[:wait_exists]
  @wait_exists_retries = @options[:wait_exists_retries]
  @logger = @options[:logger] || default_logger # separate logger instance for thread-safety

  @loop_count = 0
  @output = [] # for specs
  reset
  set_trap
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/aws_logs/tail.rb', line 5

def logger
  @logger
end

Class Method Details

.stop_follow!Object



204
205
206
207
# File 'lib/aws_logs/tail.rb', line 204

def self.stop_follow!
  logger.info "WARN: AwsLogs::Tail.stop_follow! is deprecated. Use AwsLogs::Tail#stop_follow! instead which is thread-safe."
  @@global_end_loop_signal = true
end

Instance Method Details

#check_follow_until!Object



171
172
173
174
175
176
177
# File 'lib/aws_logs/tail.rb', line 171

def check_follow_until!
  follow_until = @options[:follow_until]
  return unless follow_until

  messages = @events.map(&:message)
  @end_loop_signal = messages.detect { |m| m.include?(follow_until) }
end

#codebuild_complete?(message) ⇒ Boolean

Container

2024/03/27 02:35:32.086024 Phase complete: BUILD State: SUCCEEDED

Returns:

  • (Boolean)


167
168
169
# File 'lib/aws_logs/tail.rb', line 167

def codebuild_complete?(message)
  message.starts_with?("[Container]") && message.include?("Phase complete: BUILD")
end

#data(since = "24h", quiet_not_found = false) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/aws_logs/tail.rb', line 34

def data(since = "24h", quiet_not_found = false)
  since, now = Since.new(since).to_i, current_now
  resp = filter_log_events(since, now)
  resp.events
rescue Aws::CloudWatchLogs::Errors::ResourceNotFoundException => e
  logger.info "WARN: #{e.class}: #{e.message}" unless quiet_not_found
  []
end

#default_loggerObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aws_logs/tail.rb', line 21

def default_logger
  logger = ActiveSupport::Logger.new($stdout)
  # The ActiveSupport::Logger::SimpleFormatter always adds extra lines to the output,
  # unlike puts, which only adds a newline if it's needed.
  # We want the simpler puts behavior.
  logger.formatter = proc { |severity, timestamp, progname, msg|
    msg = "#{msg}\n" unless msg.end_with?("\n")
    "#{msg}"
  }
  logger.level = ENV["AWS_LOGS_LOG_LEVEL"] || :info
  logger
end

#displayObject

There can be duplicated events as events can be written to the exact same timestamp. So also track the last_shown_event and prevent duplicate log lines from re-appearing.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/aws_logs/tail.rb', line 132

def display
  new_events = @events
  shown_index = new_events.find_index { |e| e.event_id == @last_shown_event&.event_id }
  if shown_index
    new_events = @events[shown_index + 1..-1] || []
  end

  new_events.each do |e|
    time = Time.at(e.timestamp / 1000).utc.to_s.color(:green) unless @options[:format] == "plain"
    line = [time, e.message].compact
    format = @options[:format] || "detailed"
    line.insert(1, e.log_stream_name.color(:purple)) if format == "detailed"

    filtered = show_if? ? show_if(e) : true
    say line.join(" ") if !@options[:silence] && filtered
  end
  @last_shown_event = @events.last
  check_follow_until!
end

#filter_log_events(start_time, end_time, next_token = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/aws_logs/tail.rb', line 113

def filter_log_events(start_time, end_time, next_token = nil)
  options = {
    log_group_name: @log_group_name, # required
    start_time: start_time,
    end_time: end_time
    # limit: 1000,
    # interleaved: true,
  }

  options[:log_stream_names] = @options[:log_stream_names] if @options[:log_stream_names]
  options[:log_stream_name_prefix] = @options[:log_stream_name_prefix] if @options[:log_stream_name_prefix]
  options[:filter_pattern] = @options[:filter_pattern] if @options[:filter_pattern]
  options[:next_token] = next_token if next_token != :start && !next_token.nil?

  cloudwatchlogs.filter_log_events(options)
end

#outputObject



183
184
185
# File 'lib/aws_logs/tail.rb', line 183

def output
  @output.join("\n") + "\n"
end

#refresh_events(start_time, end_time) ⇒ Object

TODO: lazy Enum or else its seems stuck for long –since



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aws_logs/tail.rb', line 99

def refresh_events(start_time, end_time)
  @events = []
  next_token = :start

  # TODO: can hit throttle limit if there are lots of pages
  while next_token
    resp = filter_log_events(start_time, end_time, next_token)
    @events += resp.events
    next_token = resp.next_token
  end

  @events
end

#resetObject



43
44
45
46
47
# File 'lib/aws_logs/tail.rb', line 43

def reset
  @events = [] # constantly replaced with recent events
  @last_shown_event_id = nil
  @completed = nil
end

#runObject

The start and end time is useful to limit results and make the API fast. We’ll leverage it like so:

1. load all events from an initial since time
2. after that load events pass that first window

It’s a sliding window of time we’re using.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/aws_logs/tail.rb', line 56

def run
  # We overlap the sliding window because CloudWatch logs can receive or send the logs out of order.
  # For example, a bunch of logs can all come in at the same second, but they haven't registered to CloudWatch logs
  # yet. If we don't overlap the sliding window then we'll miss the logs that were delayed in registering.
  overlap = 60 * 1000 # overlap the sliding window by a minute
  since, now = initial_since, current_now
  @wait_retries ||= 0
  until end_loop?
    refresh_events(since, now)
    display

    # @last_shown_event.timestamp changes and creates a "sliding window"
    # The overlap is a just in case buffer
    since = @last_shown_event ? @last_shown_event.timestamp - overlap : initial_since
    now = current_now

    loop_count!
    sleep @refresh_rate if @follow && !ENV["AWS_LOGS_TEST"]
  end
  # Refresh and display a final time in case the end_loop gets interrupted by stop_follow!
  refresh_events(since, now)
  display
rescue Aws::CloudWatchLogs::Errors::ResourceNotFoundException => e
  if @wait_exists
    seconds = Integer(@options[:wait_exists_seconds] || 5)
    unless @@waiting_already_shown
      logger.info "Waiting for log group to exist: #{@log_group_name}"
      @@waiting_already_shown = true
    end
    sleep seconds
    @wait_retries += 1
    logger.info "Waiting #{seconds} seconds. #{@wait_retries} of #{@wait_exists_retries} retries"
    if !@wait_exists_retries || @wait_retries < @wait_exists_retries
      retry
    end
    logger.info "Giving up waiting for log group to exist"
  end
  logger.info "ERROR: #{e.class}: #{e.message}".color(:red)
  logger.info "Log group #{@log_group_name} not found."
end

#say(text) ⇒ Object



179
180
181
# File 'lib/aws_logs/tail.rb', line 179

def say(text)
  ENV["AWS_LOGS_TEST"] ? @output << text : logger.info(text)
end

#set_trapObject



187
188
189
190
191
192
193
# File 'lib/aws_logs/tail.rb', line 187

def set_trap
  Signal.trap("INT") {
    # puts must be used here instead of logger.info or else get Thread-safe error
    puts "\nCtrl-C detected. Exiting..."
    exit # immediate exit
  }
end

#show_if(e) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/aws_logs/tail.rb', line 156

def show_if(e)
  filter = @options[:show_if]
  case filter
  when ->(f) { f.respond_to?(:call) }
    filter.call(e)
  else
    filter # true or false
  end
end

#show_if?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/aws_logs/tail.rb', line 152

def show_if?
  !@options[:show_if].nil?
end

#stop_follow!Object

The stop_follow! results in a little waiting because it signals to break the polling loop. Since it’s in the middle of the loop process, the loop will finish the sleep 5 first. So it can pause from 0-5 seconds.



198
199
200
# File 'lib/aws_logs/tail.rb', line 198

def stop_follow!
  @end_loop_signal = true
end