Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions bin/query-cloudwatch
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Dir.chdir(__dir__) { require 'bundler/setup' }

require 'active_support'
require 'active_support/core_ext/integer/time'
require 'active_support/time'
require 'aws-sdk-cloudwatchlogs'
require 'concurrent-ruby'
require 'csv'
Expand All @@ -23,6 +24,7 @@ class QueryCloudwatch
:app,
:from,
:to,
:time_slices,
:query,
:format,
:complete,
Expand Down Expand Up @@ -68,8 +70,9 @@ class QueryCloudwatch
def fetch(&block)
cloudwatch_client.fetch(
query: config.query,
from: config.from,
to: config.to,
from: config.time_slices.blank? ? config.from : nil,
to: config.time_slices.blank? ? config.to : nil,
time_slices: config.time_slices.presence,
&block
)
end
Expand All @@ -92,6 +95,7 @@ class QueryCloudwatch
slice: parse_duration('1w'),
format: :csv,
to: now,
time_slices: [],
complete: false,
progress: true,
count_distinct: nil,
Expand Down Expand Up @@ -126,6 +130,13 @@ class QueryCloudwatch
--to "2020-12-31T23:59:59-00:00" \\
--query "fields @timestamp | limit 9999"

Query disjoint time slices via --date

#{$PROGRAM_NAME} \\
--date 2023-01-01,2023-02-01
--env int --app idp --log events.log
--query "fields @timestamp | limit 9999"

Timestamps
=======================

Expand Down Expand Up @@ -188,6 +199,15 @@ class QueryCloudwatch
end
end

opts.on(
'--date DATE,DATE',
'(optional) dates to query, can be disjoint, to provide time slices to query'
) do |dates|
dates.split(',').each do |date_str|
config.time_slices << Date.parse(date_str).in_time_zone('UTC').all_day
end
end

opts.on('--query QUERY', 'Cloudwatch Insights query') do |query|
config.query = query
end
Expand Down Expand Up @@ -237,7 +257,9 @@ class QueryCloudwatch
errors << 'ERROR: missing log group --group or (--app and --env and --log)'
end

errors << 'ERROR: missing time range --from or --start' if !config.from
if !config.from && config.time_slices.empty?
errors << 'ERROR: missing time range --from or --start, or --date'
end

if !config.query
if stdin.tty?
Expand Down
14 changes: 14 additions & 0 deletions spec/bin/query-cloudwatch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@
end
end

context 'with --date' do
let(:argv) { required_parameters + ['--date', '2023-01-01,2023-08-01'] }

it 'creates disjoint time slices' do
config = parse!
expect(config.time_slices).to eq(
[
Date.new(2023, 1, 1).in_time_zone('UTC').all_day,
Date.new(2023, 8, 1).in_time_zone('UTC').all_day,
],
)
end
end

context 'with --no-progress' do
let(:argv) { required_parameters + %w[--no-progress] }

Expand Down