From 62dcdd79a0a7f274cc2d53313dcddd87b2285078 Mon Sep 17 00:00:00 2001 From: Erica Porter Date: Fri, 5 Jul 2024 14:01:25 +0100 Subject: [PATCH 1/3] Allow certain paths or endpoints to be excluded --- README.md | 11 ++++++ lib/dfe/analytics.rb | 2 + lib/dfe/analytics/requests.rb | 14 +++++++ spec/dfe/analytics/requests_spec.rb | 61 +++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) diff --git a/README.md b/README.md index ac52cdf2..4b433974 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,17 @@ user identifier proc can be defined in `config/initializers/dfe_analytics.rb`: DfE::Analytics.config.user_identifier = proc { |user| user&.uid } ``` +You can specify paths that should be excluded from logging using the skip_web_requests configuration option. This is useful for endpoints like health checks that are frequently hit and do not need to be logged. + +```ruby +DfE::Analytics.configure do |config| + # other configurations... + + # Specify paths to skip + config.skip_web_requests = ['/healthcheck', %r{^/admin}, %r{/api/v1/status}] +end +``` + ### 6. Import existing data To load the current contents of your database into BigQuery, run diff --git a/lib/dfe/analytics.rb b/lib/dfe/analytics.rb index ab6eb15e..639a92fb 100644 --- a/lib/dfe/analytics.rb +++ b/lib/dfe/analytics.rb @@ -57,6 +57,7 @@ def self.config azure_scope gcp_scope google_cloud_credentials + excluded_paths ] @config ||= Struct.new(*configurables).new @@ -82,6 +83,7 @@ def self.configure config.rack_page_cached ||= proc { |_rack_env| false } config.bigquery_maintenance_window ||= ENV.fetch('BIGQUERY_MAINTENANCE_WINDOW', nil) config.azure_federated_auth ||= false + config.excluded_paths ||= [] return unless config.azure_federated_auth diff --git a/lib/dfe/analytics/requests.rb b/lib/dfe/analytics/requests.rb index 3657722d..ac2319f4 100644 --- a/lib/dfe/analytics/requests.rb +++ b/lib/dfe/analytics/requests.rb @@ -11,6 +11,7 @@ module Requests def trigger_request_event return unless DfE::Analytics.enabled? + return if path_excluded? request_event = DfE::Analytics::Event.new .with_type('web_request') @@ -23,6 +24,19 @@ def trigger_request_event DfE::Analytics::SendEvents.do([request_event.as_json]) end + + private + + def path_excluded? + excluded_path = DfE::Analytics.config.excluded_paths + excluded_path.any? do |path| + if path.is_a?(Regexp) + path.match?(request.fullpath) + else + request.fullpath.start_with?(path) + end + end + end end end end diff --git a/spec/dfe/analytics/requests_spec.rb b/spec/dfe/analytics/requests_spec.rb index 2a371b9a..f32cc78d 100644 --- a/spec/dfe/analytics/requests_spec.rb +++ b/spec/dfe/analytics/requests_spec.rb @@ -36,6 +36,11 @@ def index Rails.application.routes.draw do get '/example/path' => 'test#index' get '/unauthenticated_example' => 'test_unauthenticated#index' + get '/healthcheck' => 'test#index' + get '/regex_path/test' => 'test#index' + get '/some/path/with/regex_path' => 'test#index' + get '/another/path/to/regex_path' => 'test#index' + get '/included/path' => 'test#index' end ex.run @@ -63,6 +68,12 @@ def index user_id: 1 } end + before do + DfE::Analytics.configure do |config| + config.excluded_paths = ['/healthcheck', %r{^/regex_path/.*$}, %r{regex_path$}] + end + end + it 'sends request data to BigQuery' do request = stub_analytics_event_submission DfE::Analytics::Testing.webmock! do @@ -156,4 +167,54 @@ def index end).to have_been_made end end + + context 'when request path is in the skip list' do + it 'does not send healthcheck request data to BigQuery' do + request = stub_analytics_event_submission + + DfE::Analytics::Testing.webmock! do + perform_enqueued_jobs do + get('/healthcheck') + end + end + + expect(request).not_to have_been_made + end + + it 'does not send regex_path/test request data to BigQuery' do + request = stub_analytics_event_submission + + DfE::Analytics::Testing.webmock! do + perform_enqueued_jobs do + get('/regex_path/test') + end + end + + expect(request).not_to have_been_made + end + + it 'does not send some/path/with/regex_path request data to BigQuery' do + request = stub_analytics_event_submission + + DfE::Analytics::Testing.webmock! do + perform_enqueued_jobs do + get('/some/path/with/regex_path') + end + end + + expect(request).not_to have_been_made + end + + it 'does not send another/path/to/regex_path request data to BigQuery' do + request = stub_analytics_event_submission + + DfE::Analytics::Testing.webmock! do + perform_enqueued_jobs do + get('/another/path/to/regex_path') + end + end + + expect(request).not_to have_been_made + end + end end From ff233d544e701b9a0001580c9d4692b65c2d67ab Mon Sep 17 00:00:00 2001 From: Erica Porter Date: Fri, 5 Jul 2024 14:12:47 +0100 Subject: [PATCH 2/3] Appease rubocop --- spec/dfe/analytics/requests_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/dfe/analytics/requests_spec.rb b/spec/dfe/analytics/requests_spec.rb index f32cc78d..c08fdb8a 100644 --- a/spec/dfe/analytics/requests_spec.rb +++ b/spec/dfe/analytics/requests_spec.rb @@ -40,7 +40,7 @@ def index get '/regex_path/test' => 'test#index' get '/some/path/with/regex_path' => 'test#index' get '/another/path/to/regex_path' => 'test#index' - get '/included/path' => 'test#index' + get '/included/path' => 'test#index' end ex.run @@ -70,7 +70,7 @@ def index before do DfE::Analytics.configure do |config| - config.excluded_paths = ['/healthcheck', %r{^/regex_path/.*$}, %r{regex_path$}] + config.excluded_paths = ['/healthcheck', %r{^/regex_path/.*$}, /regex_path$/] end end From 009213d5e6c2e1b3b26ef07e5cfa94ba010be5c4 Mon Sep 17 00:00:00 2001 From: Erica Porter Date: Mon, 8 Jul 2024 14:42:12 +0100 Subject: [PATCH 3/3] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b433974..8ad56c25 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ DfE::Analytics.configure do |config| # other configurations... # Specify paths to skip - config.skip_web_requests = ['/healthcheck', %r{^/admin}, %r{/api/v1/status}] + config.excluded_paths = ['/healthcheck', %r{^/admin}, %r{/api/v1/status}] end ```