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

Allow certain paths or endpoints to be excluded #154

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
ericaporter marked this conversation as resolved.
Show resolved Hide resolved
end
```

### 6. Import existing data

To load the current contents of your database into BigQuery, run
Expand Down
2 changes: 2 additions & 0 deletions lib/dfe/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def self.config
azure_scope
gcp_scope
google_cloud_credentials
excluded_paths
]

@config ||= Struct.new(*configurables).new
Expand All @@ -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

Expand Down
14 changes: 14 additions & 0 deletions lib/dfe/analytics/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
61 changes: 61 additions & 0 deletions spec/dfe/analytics/requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,6 +68,12 @@ def index
user_id: 1 }
end

before do
DfE::Analytics.configure do |config|
config.excluded_paths = ['/healthcheck', %r{^/regex_path/.*$}, /regex_path$/]
end
end

it 'sends request data to BigQuery' do
request = stub_analytics_event_submission
DfE::Analytics::Testing.webmock! do
Expand Down Expand Up @@ -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
Loading