Class: AwsLogs::Tail

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

Constant Summary collapse

@@end_loop_signal =

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.

false

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AwsServices

#cloudwatchlogs

Constructor Details

#initialize(options = {}) ⇒ Tail

Returns a new instance of Tail.



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

def initialize(options={})
  super
  # Setting to ensure matches default CLI option
  @follow = @options[:follow].nil? ? true : @options[:follow]
  @refresh_rate = @options[:refresh_rate] || 5
  @wait_exists = @options[:wait_exists]
  @wait_exists_retries = @options[:wait_exists_retries]

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

Class Method Details

.stop_follow!Object



156
157
158
# File 'lib/aws_logs/tail.rb', line 156

def self.stop_follow!
  @@end_loop_signal = true
end

Instance Method Details

#check_follow_until!Object



129
130
131
132
133
134
135
# File 'lib/aws_logs/tail.rb', line 129

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

#data(since = "1d", quiet_not_found = false) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/aws_logs/tail.rb', line 19

def data(since="1d", 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
  puts "WARN: #{e.class}: #{e.message}" unless quiet_not_found
  []
end

#displayObject

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



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

def display
  new_events = @events
  shown_index = new_events.find_index { |e| e.event_id == @last_shown_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
    line = [time.to_s.color(:green), e.message]
    format = @options[:format] || "detailed"
    line.insert(1, e.log_stream_name.color(:purple)) if format == "detailed"
    say line.join(' ') unless @options[:silence]
  end
  @last_shown_event_id = @events.last&.event_id
  check_follow_until!
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aws_logs/tail.rb', line 92

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



141
142
143
# File 'lib/aws_logs/tail.rb', line 141

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



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/aws_logs/tail.rb', line 78

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



28
29
30
31
32
# File 'lib/aws_logs/tail.rb', line 28

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/aws_logs/tail.rb', line 41

def run
  if ENV['AWS_LOGS_NOOP']
    puts "Noop test"
    return
  end

  # 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
    since, now = now-overlap, 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 = 5
    puts "Waiting for log group #{@log_group_name} to exist. Waiting #{seconds} seconds."
    sleep seconds
    @wait_retries += 1
    if !@wait_exists_retries || @wait_retries < @wait_exists_retries
      retry
    end
  end
  puts "ERROR: #{e.class}: #{e.message}".color(:red)
  puts "Log group #{@log_group_name} not found."
end

#say(text) ⇒ Object



137
138
139
# File 'lib/aws_logs/tail.rb', line 137

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

#set_trapObject



145
146
147
148
149
150
# File 'lib/aws_logs/tail.rb', line 145

def set_trap
  Signal.trap("INT") {
    puts "\nCtrl-C detected. Exiting..."
    exit # immediate exit
  }
end