Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better error logging when calling BigQuery insert API on error #78

Merged
merged 1 commit into from
Jun 28, 2023
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
8 changes: 4 additions & 4 deletions lib/dfe/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def self.configure
yield(config)

config.enable_analytics ||= proc { true }
config.bigquery_table_name ||= ENV['BIGQUERY_TABLE_NAME']
config.bigquery_project_id ||= ENV['BIGQUERY_PROJECT_ID']
config.bigquery_dataset ||= ENV['BIGQUERY_DATASET']
config.bigquery_api_json_key ||= ENV['BIGQUERY_API_JSON_KEY']
config.bigquery_table_name ||= ENV.fetch('BIGQUERY_TABLE_NAME', nil)
config.bigquery_project_id ||= ENV.fetch('BIGQUERY_PROJECT_ID', nil)
config.bigquery_dataset ||= ENV.fetch('BIGQUERY_DATASET', nil)
config.bigquery_api_json_key ||= ENV.fetch('BIGQUERY_API_JSON_KEY', nil)
config.bigquery_retries ||= 3
config.bigquery_timeout ||= 120
config.environment ||= ENV.fetch('RAILS_ENV', 'development')
Expand Down
18 changes: 12 additions & 6 deletions lib/dfe/analytics/send_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,28 @@ def perform(events)
response = DfE::Analytics.events_client.insert(events, ignore_unknown: true)

unless response.success?
error_message = error_message_for(response.insert_errors)
event_count = events.length
error_message = error_message_for(response, events)

Rails.logger.error(error_message)

events.each.with_index(1) do |event, index|
Rails.logger.info("DfE::Analytics possible error processing event (#{index}/#{event_count}): #{event.inspect}")
end

raise SendEventsError, error_message
end
end
end

def error_message_for(insert_errors)
message = insert_errors
.flat_map(&:errors)
.map { |error| error.try(:message) || error['message'] }
def error_message_for(resp, events)
message =
resp
.error_rows
.map { |row| "row: #{row} errors: #{resp.errors_for(row)} index: #{resp.index_for(row)} event: #{events[resp.index_for(row)].inspect}" }
.compact.join("\n")

"Could not insert all events:\n#{message}"
"Could not insert #{resp.error_count} event(s):\n#{message}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/dfe/analytics/send_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
end

it 'logs the error message' do
expect(Rails.logger).to receive(:error).with(/Could not insert all events:/)
expect(Rails.logger).to receive(:error).with(/Could not insert 1 event\(s\):/)

perform
rescue DfE::Analytics::SendEventsError
Expand Down