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

Filter Sentry gtag errors #4968

Merged
merged 2 commits into from
Jun 25, 2024
Merged
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
21 changes: 20 additions & 1 deletion config/initializers/sentry.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
# frozen_string_literal: true

FILTERABLE_JS_ERRORS = [
"Failed to fetch",
"NetworkError when attempting to fetch resource.",
].freeze

def cookie_banner_js_event?(event, hint)
hint[:exception].is_a?(Error) &&
event.tags["mechanism"] == "onunhandledrejection" &&
event.exception.values.any? { |ex| ex.value.include?("[object Response]") }
end

def filterable_js_event?(event, hint)
hint[:exception].is_a?(TypeError) && event.exception.values.any? do |exception|
FILTERABLE_JS_ERRORS.include?(exception.value)
end
end

Sentry.init do |config|
config.enabled_environments = %w[production sandbox staging review]
config.dsn = config.enabled_environments.include?(Rails.env) ? Rails.application.credentials.SENTRY_DSN : "disabled"
config.breadcrumbs_logger = %i[active_support_logger http_logger]
config.release = "#{ENV['RELEASE_VERSION']}-#{ENV['SHA']}"

filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)
config.before_send = lambda do |event, _hint|
config.before_send = lambda do |event, hint|
return nil if filterable_js_event?(event, hint) || cookie_banner_js_event?(event, hint)

# use Rails' parameter filter to sanitize the event
filter.filter(event.to_hash)
end
Expand Down
Loading