diff --git a/bin/query-cloudwatch b/bin/query-cloudwatch index d8848e1193b..cd326622f91 100755 --- a/bin/query-cloudwatch +++ b/bin/query-cloudwatch @@ -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' @@ -23,6 +24,7 @@ class QueryCloudwatch :app, :from, :to, + :time_slices, :query, :format, :complete, @@ -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 @@ -92,6 +95,7 @@ class QueryCloudwatch slice: parse_duration('1w'), format: :csv, to: now, + time_slices: [], complete: false, progress: true, count_distinct: nil, @@ -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 ======================= @@ -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 @@ -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? diff --git a/spec/bin/query-cloudwatch_spec.rb b/spec/bin/query-cloudwatch_spec.rb index 8437b6d1e38..0b2b1d8f9d1 100644 --- a/spec/bin/query-cloudwatch_spec.rb +++ b/spec/bin/query-cloudwatch_spec.rb @@ -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] }