diff --git a/lib/reporting/cloudwatch_client.rb b/lib/reporting/cloudwatch_client.rb index 4140e8949a6..4aedd855600 100644 --- a/lib/reporting/cloudwatch_client.rb +++ b/lib/reporting/cloudwatch_client.rb @@ -163,16 +163,19 @@ def fetch_one(query:, start_time:, end_time:) ).query_id wait_for_query_result(query_id) - rescue Aws::CloudWatchLogs::Errors::InvalidParameterException => err + # rubocop:disable Layout/LineLength, Rails/TimeZone + rescue Aws::CloudWatchLogs::Errors::InvalidParameterException, Aws::CloudWatchLogs::Errors::MalformedQueryException => err if err.message.match?(/End time should not be before the service was generally available/) - # rubocop:disable Layout/LineLength, Rails/TimeZone log(:warn, "query end_time=#{end_time} (#{Time.at(end_time)}) is before Cloudwatch Insights availability, skipping") - # rubocop:enable Layout/LineLength, Rails/TimeZone + Aws::CloudWatchLogs::Types::GetQueryResultsResponse.new(results: []) + elsif err.message.match?(/end date and time is either before the log groups creation time or exceeds the log groups log retention settings/) + log(:warn, "query end_time=#{end_time} (#{Time.at(end_time)}) is before the log groups creation time or exceeds the log groups log retention settings") Aws::CloudWatchLogs::Types::GetQueryResultsResponse.new(results: []) else raise err end end + # rubocop:enable Layout/LineLength, Rails/TimeZone def ensure_complete_logs? @ensure_complete_logs diff --git a/lib/reporting/monthly_proofing_report.rb b/lib/reporting/monthly_proofing_report.rb index 93540ed986c..4b9a1de8bcf 100644 --- a/lib/reporting/monthly_proofing_report.rb +++ b/lib/reporting/monthly_proofing_report.rb @@ -80,11 +80,6 @@ def proofing_report end csv - rescue Aws::CloudWatchLogs::Errors::MalformedQueryException => error - [ - ['Error', 'Message'], - [error.class.name, error.message], - ] end def as_csv diff --git a/spec/lib/reporting/cloudwatch_client_spec.rb b/spec/lib/reporting/cloudwatch_client_spec.rb index 2fcd16f3514..6a6f1c48e49 100644 --- a/spec/lib/reporting/cloudwatch_client_spec.rb +++ b/spec/lib/reporting/cloudwatch_client_spec.rb @@ -244,5 +244,28 @@ def stub_single_page expect(logger_io.string).to include('is before Cloudwatch Insights availability') end end + + context 'when the query is outside the log retention range' do + before do + # rubocop:disable Layout/LineLength + Aws.config[:cloudwatchlogs] = { + stub_responses: { + start_query: Aws::CloudWatchLogs::Errors::MalformedQueryException.new( + nil, + 'end date and time is either before the log groups creation time or exceeds the log groups log retention settings', + ), + }, + } + # rubocop:enable Layout/LineLength + + allow(Time).to receive(:zone).and_return(nil) + end + + it 'logs a warning and returns an empty array for that range' do + expect(fetch).to eq([]) + + expect(logger_io.string).to include('exceeds the log groups log retention settings') + end + end end end diff --git a/spec/lib/reporting/monthly_proofing_report_spec.rb b/spec/lib/reporting/monthly_proofing_report_spec.rb index db58bcf31b7..5e00fee39af 100644 --- a/spec/lib/reporting/monthly_proofing_report_spec.rb +++ b/spec/lib/reporting/monthly_proofing_report_spec.rb @@ -58,28 +58,6 @@ end end - describe '#proofing_report' do - context 'when the data is outside the log retention range' do - before do - allow(report.cloudwatch_client).to receive(:fetch).and_raise( - Aws::CloudWatchLogs::Errors::MalformedQueryException.new( - nil, - 'exceeds the log groups log retention settings', - ), - ) - end - - it 'handles the error and returns a table with information on the error' do - expect(report.proofing_report).to match( - [ - ['Error', 'Message'], - ['Aws::CloudWatchLogs::Errors::MalformedQueryException', kind_of(String)], - ], - ) - end - end - end - describe '#data' do it 'keeps unique users per event as a hash' do expect(report.data).to eq(