From 3ec25ef0b213fdb121492ef9b4e6452ffd94542e Mon Sep 17 00:00:00 2001 From: David Bailey Date: Fri, 17 May 2019 16:20:08 -0700 Subject: [PATCH] log firehose event via server after failing to log via client --- apps/src/lib/util/firehose.js | 18 ++++++++++++++++++ dashboard/app/controllers/api_controller.rb | 14 ++++++++++++++ dashboard/config/routes.rb | 1 + 3 files changed, 33 insertions(+) diff --git a/apps/src/lib/util/firehose.js b/apps/src/lib/util/firehose.js index aa8c1810c7741..d8fe1541ade5d 100644 --- a/apps/src/lib/util/firehose.js +++ b/apps/src/lib/util/firehose.js @@ -180,6 +180,21 @@ class FirehoseClient { return data; } + handleError(requestData, error) { + // Report the error via our own servers, in case reporting it directly + // to firehose was blocked by a network firewall. + $.ajax({ + url: '/api/firehose_unreachable', + data: JSON.stringify({ + original_data: requestData, + error_text: String(error) + }), + contentType: 'application/json; charset=utf-8', + method: 'PUT', + dataType: 'json' + }); + } + /** * Pushes one data record into the delivery stream. * @param {hash} data The data to push. @@ -197,6 +212,7 @@ class FirehoseClient { options = {alwaysPut: false, includeUserId: false, callback: null} ) { data = this.addCommonValues(data, options.includeUserId); + const handleError = this.handleError.bind(this, data); if (!this.shouldPutRecord(options['alwaysPut'])) { console.groupCollapsed('Skipped sending record to ' + deliveryStreamName); console.log(data); @@ -217,6 +233,8 @@ class FirehoseClient { function(err, data) { if (options.callback) { options.callback(err, data); + } else if (err) { + handleError(err); } } ); diff --git a/dashboard/app/controllers/api_controller.rb b/dashboard/app/controllers/api_controller.rb index d857b2eec58cb..6e5a109865a95 100644 --- a/dashboard/app/controllers/api_controller.rb +++ b/dashboard/app/controllers/api_controller.rb @@ -441,6 +441,20 @@ def sign_cookies head :ok end + # PUT /api/firehose_unreachable + def firehose_unreachable + original_data = params.require(:original_data) + event = original_data['event'] + project_id = original_data['project_id'] || nil + FirehoseClient.instance.put_record( + study: 'firehose-error-unreachable', + event: event, + project_id: project_id, + data_string: params.require(:error_text), + data_json: original_data.to_json + ) + end + private def load_student(student_id) diff --git a/dashboard/config/routes.rb b/dashboard/config/routes.rb index 8af83ace9b426..2d9ae625dd3db 100644 --- a/dashboard/config/routes.rb +++ b/dashboard/config/routes.rb @@ -575,6 +575,7 @@ get '/api/user_progress/:script', to: 'api#user_progress', as: 'user_progress' get '/api/user_progress/:script/:stage_position/:level_position', to: 'api#user_progress_for_stage', as: 'user_progress_for_stage' get '/api/user_progress/:script/:stage_position/:level_position/:level', to: 'api#user_progress_for_stage', as: 'user_progress_for_stage_and_level' + put '/api/firehose_unreachable', to: 'api#firehose_unreachable' namespace :api do api_methods.each do |action| get action, action: action