aws-logs 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93209b1d08b94e59613794379fbc6da21b69113c7ef5df6bf008fae348d1d129
4
- data.tar.gz: a3d4f821990b9fcf41747df792595cffa1c8ba8cb41bcbceb2f915d4a08f68e4
3
+ metadata.gz: f80b63c48f228ee6420c2d97226ac8fe1fab65fe42b847b15bfe80fd3f72197a
4
+ data.tar.gz: de12e2063cb6e25ee6505fa48ffb8d6d695b0bfa33afc9c3334a4cc132d82c00
5
5
  SHA512:
6
- metadata.gz: 98436a84cb82b97d6872e65ac07a2c9cb1ff8eac228288df4df28b85e0dddb4139641f5bacdd66977355b1b789aea60bc1d21dc7dda8c0e4ce4af220a15c4874
7
- data.tar.gz: 6877115a9d916ab5ac7f84ae92b1d89ad364e807e3e3f126d2ffdcf0699d2fade8e0e5d8e661a7d55635f98bad8a7b345ed6a047dedefde4fd0de26ac80fc977
6
+ metadata.gz: ec6276ffea2146c914704fb1021a0b3b4cc9b595640ef83cf444ef5217dbdb672504084f12cc3773afdb9ddf02018579512286c8ae25eb7399bf437b60d26c5c
7
+ data.tar.gz: 8af7b076de6042316a374321278dadee5d27dea00fcf08d97f58b5c1578fa07dd151b2cbb65f074d5457903c2fcb8ea77f7aa3c01af2a0ffff02ac8e28178050
data/CHANGELOG.md CHANGED
@@ -3,6 +3,20 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [0.4.0] - 2022-01-07
7
+ - [#3](https://github.com/tongueroo/aws-logs/pull/3) streams cli command and data method
8
+ - fix activesupport require
9
+ - streams cli command and data method
10
+
11
+ ## [0.3.4]
12
+ - set next_token for large filter_log_events responses
13
+
14
+ ## [0.3.3]
15
+ - #2 add overlap to window to account for delayed logs received at the same time
16
+
17
+ ## [0.3.2]
18
+ - #1 clean up end_loop_signal logic
19
+
6
20
  ## [0.3.1]
7
21
  - fix --no-follow option
8
22
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # aws-logs
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/GEMNAME.png)](http://badge.fury.io/rb/GEMNAME)
3
+ [![Gem Version](https://badge.fury.io/rb/aws-logs.png)](http://badge.fury.io/rb/aws-logs)
4
4
 
5
5
  [![BoltOps Badge](https://img.boltops.com/boltops/badges/boltops-badge.png)](https://www.boltops.com)
6
6
 
@@ -0,0 +1,10 @@
1
+ module AwsLogs
2
+ class Base
3
+ include AwsServices
4
+
5
+ def initialize(options={})
6
+ @options = options
7
+ @log_group_name = options[:log_group_name]
8
+ end
9
+ end
10
+ end
data/lib/aws_logs/cli.rb CHANGED
@@ -13,6 +13,14 @@ module AwsLogs
13
13
  Tail.new(options.merge(log_group_name: log_group_name)).run
14
14
  end
15
15
 
16
+ desc "streams LOG_GROUP", "Show the log group stream names. Limits on only one page of results."
17
+ long_desc Help.text(:streams)
18
+ option :descending, desc: "True by default if order-by is LastEventTime, false if order-by is LogStreamName"
19
+ option :order_by, default: "LastEventTime", desc: "accepts LogStreamName, LastEventTime"
20
+ def streams(log_group_name)
21
+ Streams.new(options.merge(log_group_name: log_group_name)).run
22
+ end
23
+
16
24
  desc "completion *PARAMS", "Prints words for auto-completion."
17
25
  long_desc Help.text(:completion)
18
26
  def completion(*params)
@@ -77,6 +77,13 @@ module AwsLogs
77
77
  def website
78
78
  ""
79
79
  end
80
+
81
+ # https://github.com/erikhuda/thor/issues/244
82
+ # Deprecation warning: Thor exit with status 0 on errors. To keep this behavior, you must define `exit_on_failure?` in `Lono::CLI`
83
+ # You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
84
+ def exit_on_failure?
85
+ true
86
+ end
80
87
  end
81
88
  end
82
89
  end
@@ -52,6 +52,6 @@ To match terms with spaces in it, you'll need quotes around it. Otherise, the ma
52
52
 
53
53
  aws-logs tail /aws/codebuild/demo --filter-pattern '"Wed Nov 27 23"' --since 3h --no-follow
54
54
 
55
- Here's an example of matching with an exclude patter using the `-` (minus sign).
55
+ Here's an example of matching with an exclude pattern using the `-` (minus sign).
56
56
 
57
57
  aws-logs tail /aws/codebuild/demo --filter-pattern '"ERROR" - "Exiting"' --since 3h --no-follow
@@ -1,3 +1,4 @@
1
+ require "active_support"
1
2
  require "active_support/core_ext/integer"
2
3
  require "time"
3
4
 
@@ -0,0 +1,24 @@
1
+ module AwsLogs
2
+ class Streams < Base
3
+ def run
4
+ resp = cloudwatchlogs.describe_log_streams(
5
+ log_group_name: @log_group_name,
6
+ descending: descending,
7
+ order_by: order_by,
8
+ )
9
+ names = resp.log_streams.map { |s| s.log_stream_name }
10
+ puts names
11
+ end
12
+
13
+ private
14
+ # True by default if order-by is LastEventTime, false if order-by is LogStreamName
15
+ def descending
16
+ return @options[:descending] unless @options[:descending].nil?
17
+ order_by == "LastEventTime"
18
+ end
19
+
20
+ def order_by
21
+ @options[:order_by] || "LastEventTime" # accepts LogStreamName, LastEventTime
22
+ end
23
+ end
24
+ end
data/lib/aws_logs/tail.rb CHANGED
@@ -1,12 +1,9 @@
1
1
  require "json"
2
2
 
3
3
  module AwsLogs
4
- class Tail
5
- include AwsServices
6
-
4
+ class Tail < Base
7
5
  def initialize(options={})
8
- @options = options
9
- @log_group_name = options[:log_group_name]
6
+ super
10
7
  # Setting to ensure matches default CLI option
11
8
  @follow = @options[:follow].nil? ? true : @options[:follow]
12
9
 
@@ -16,6 +13,15 @@ module AwsLogs
16
13
  set_trap
17
14
  end
18
15
 
16
+ def data(since="1d")
17
+ since, now = Since.new(since).to_i, current_now
18
+ resp = filter_log_events(since, now)
19
+ resp.events
20
+ rescue Aws::CloudWatchLogs::Errors::ResourceNotFoundException => e
21
+ puts "WARN: #{e.class}: #{e.message}"
22
+ []
23
+ end
24
+
19
25
  def reset
20
26
  @events = [] # constantly replaced with recent events
21
27
  @last_shown_event_id = nil
@@ -35,13 +41,17 @@ module AwsLogs
35
41
  return
36
42
  end
37
43
 
44
+ # We overlap the sliding window because CloudWatch logs can receive or send the logs out of order.
45
+ # For example, a bunch of logs can all come in at the same second, but they haven't registered to CloudWatch logs
46
+ # yet. If we don't overlap the sliding window then we'll miss the logs that were delayed in registering.
47
+ overlap = 60*1000 # overlap the sliding window by a minute
38
48
  since, now = initial_since, current_now
39
49
  until end_loop?
40
50
  refresh_events(since, now)
41
51
  display
42
- since, now = now, current_now
52
+ since, now = now-overlap, current_now
43
53
  loop_count!
44
- sleep 5 if @follow && !@@end_loop_signal && !ENV["AWS_LOGS_TEST"]
54
+ sleep 5 if @follow && !ENV["AWS_LOGS_TEST"]
45
55
  end
46
56
  # Refresh and display a final time in case the end_loop gets interrupted by stop_follow!
47
57
  refresh_events(since, now)
@@ -57,17 +67,7 @@ module AwsLogs
57
67
 
58
68
  # TODO: can hit throttle limit if there are lots of pages
59
69
  while next_token
60
- options = {
61
- log_group_name: @log_group_name, # required
62
- start_time: start_time,
63
- end_time: end_time,
64
- # limit: 10,
65
- }
66
- options[:log_stream_names] = @options[:log_stream_names] if @options[:log_stream_names]
67
- options[:log_stream_name_prefix] = @options[:log_stream_name_prefix] if @options[:log_stream_name_prefix]
68
- options[:filter_pattern] = @options[:filter_pattern] if @options[:filter_pattern]
69
- resp = cloudwatchlogs.filter_log_events(options)
70
-
70
+ resp = filter_log_events(start_time, end_time, next_token)
71
71
  @events += resp.events
72
72
  next_token = resp.next_token
73
73
  end
@@ -75,7 +75,24 @@ module AwsLogs
75
75
  @events
76
76
  end
77
77
 
78
- # Events canduplicated as events can be written to the exact same timestamp.
78
+ def filter_log_events(start_time, end_time, next_token=nil)
79
+ options = {
80
+ log_group_name: @log_group_name, # required
81
+ start_time: start_time,
82
+ end_time: end_time,
83
+ # limit: 1000,
84
+ # interleaved: true,
85
+ }
86
+
87
+ options[:log_stream_names] = @options[:log_stream_names] if @options[:log_stream_names]
88
+ options[:log_stream_name_prefix] = @options[:log_stream_name_prefix] if @options[:log_stream_name_prefix]
89
+ options[:filter_pattern] = @options[:filter_pattern] if @options[:filter_pattern]
90
+ options[:next_token] = next_token if next_token != :start && !next_token.nil?
91
+
92
+ cloudwatchlogs.filter_log_events(options)
93
+ end
94
+
95
+ # There can be duplicated events as events can be written to the exact same timestamp.
79
96
  # So also track the last_shown_event_id and prevent duplicate log lines from re-appearing.
80
97
  def display
81
98
  new_events = @events
@@ -89,7 +106,7 @@ module AwsLogs
89
106
  line = [time.to_s.color(:green), e.message]
90
107
  format = @options[:format] || "detailed"
91
108
  line.insert(1, e.log_stream_name.color(:purple)) if format == "detailed"
92
- say line.join(' ')
109
+ say line.join(' ') unless @options[:silence]
93
110
  end
94
111
  @last_shown_event_id = @events.last&.event_id
95
112
  check_follow_until!
@@ -100,9 +117,7 @@ module AwsLogs
100
117
  return unless follow_until
101
118
 
102
119
  messages = @events.map(&:message)
103
- if messages.detect { |m| m.include?(follow_until) }
104
- @@end_loop_signal = true
105
- end
120
+ @@end_loop_signal = messages.detect { |m| m.include?(follow_until) }
106
121
  end
107
122
 
108
123
  def say(text)
@@ -113,15 +128,17 @@ module AwsLogs
113
128
  @output.join("\n") + "\n"
114
129
  end
115
130
 
116
- @@end_loop_signal = false
117
131
  def set_trap
118
132
  Signal.trap("INT") {
119
133
  puts "\nCtrl-C detected. Exiting..."
120
- @@end_loop_signal = true # useful to control loop
121
134
  exit # immediate exit
122
135
  }
123
136
  end
124
137
 
138
+ # The stop_follow! results in a little waiting because it signals to break the polling loop.
139
+ # Since it's in the middle of the loop process, the loop will finish the sleep 5 first.
140
+ # So it can pause from 0-5 seconds.
141
+ @@end_loop_signal = false
125
142
  def self.stop_follow!
126
143
  @@end_loop_signal = true
127
144
  end
@@ -1,3 +1,3 @@
1
1
  module AwsLogs
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-logs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-28 00:00:00.000000000 Z
11
+ date: 2022-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -186,6 +186,7 @@ files:
186
186
  - lib/aws_logs.rb
187
187
  - lib/aws_logs/autoloader.rb
188
188
  - lib/aws_logs/aws_services.rb
189
+ - lib/aws_logs/base.rb
189
190
  - lib/aws_logs/cli.rb
190
191
  - lib/aws_logs/command.rb
191
192
  - lib/aws_logs/completer.rb
@@ -196,6 +197,7 @@ files:
196
197
  - lib/aws_logs/help/completion_script.md
197
198
  - lib/aws_logs/help/tail.md
198
199
  - lib/aws_logs/since.rb
200
+ - lib/aws_logs/streams.rb
199
201
  - lib/aws_logs/tail.rb
200
202
  - lib/aws_logs/version.rb
201
203
  - spec/fixtures/typical/events-1.json
@@ -225,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
227
  - !ruby/object:Gem::Version
226
228
  version: '0'
227
229
  requirements: []
228
- rubygems_version: 3.0.6
230
+ rubygems_version: 3.2.32
229
231
  signing_key:
230
232
  specification_version: 4
231
233
  summary: Tail AWS CloudWatch Logs