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

Ignore errors that occur in temporary environments #168

Merged
merged 2 commits into from
Oct 27, 2020
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.6.0

* Ignore errors that occur in temporary environments (adds `active_sentry_environments` config) (https://github.com/alphagov/govuk_app_config/pull/168)

# 2.5.2

* Fix govuk_app_config in Ruby 2.7 environments by explicitly requiring the 'delegate' library (https://github.com/alphagov/govuk_app_config/pull/167)
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ If you include `govuk_app_config` in your `Gemfile`, Rails' autoloading mechanis
Your app will have to have the following environment variables set:

- `SENTRY_DSN` - the [Data Source Name (DSN)][dsn] for Sentry
- `SENTRY_CURRENT_ENV` - production, staging or integration
- `SENTRY_CURRENT_ENV` - e.g. "production". Make sure it is [configured to be active](#active-sentry-environments).
- `GOVUK_STATSD_PREFIX` - a Statsd prefix like `govuk.apps.application-name.hostname`

[dsn]: https://docs.sentry.io/quickstart/#about-the-dsn
Expand Down Expand Up @@ -79,6 +79,18 @@ GovukError.notify(
)
```

### Active Sentry environments

GovukError will only send errors to Sentry if your `SENTRY_CURRENT_ENV` matches one of the 'active environments' in the [default configuration](https://github.com/alphagov/govuk_app_config/blob/master/lib/govuk_app_config/govuk_error/configure.rb). This is to prevent temporary test environments from flooding our Sentry account with errors.

You can add your environment to the list of active Sentry environments like so:

```ruby
GovukError.configure do |config|
config.active_sentry_environments << "my-test-environment"
end
```

### Error configuration

You can exclude certain errors from being reported using this:
Expand Down
21 changes: 17 additions & 4 deletions lib/govuk_app_config/govuk_error/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,39 @@

module GovukError
class Configuration < SimpleDelegator
attr_reader :data_sync
attr_accessor :data_sync_excluded_exceptions
attr_reader :data_sync, :sentry_environment
attr_accessor :active_sentry_environments, :data_sync_excluded_exceptions

def initialize(_raven_configuration)
super
@sentry_environment = ENV["SENTRY_CURRENT_ENV"]
@data_sync = GovukDataSync.new(ENV["GOVUK_DATA_SYNC_PERIOD"])
self.active_sentry_environments = []
self.data_sync_excluded_exceptions = []
self.should_capture = ignore_excluded_exceptions_in_data_sync
self.should_capture = ignore_exceptions_based_on_env_and_data_sync
end

def should_capture=(closure)
combined = lambda do |error_or_event|
(ignore_excluded_exceptions_in_data_sync.call(error_or_event) && closure.call(error_or_event))
(ignore_exceptions_based_on_env_and_data_sync.call(error_or_event) && closure.call(error_or_event))
end

super(combined)
end

protected

def ignore_exceptions_based_on_env_and_data_sync
lambda do |error_or_event|
ignore_exceptions_if_not_in_active_sentry_env.call(error_or_event) &&
ignore_excluded_exceptions_in_data_sync.call(error_or_event)
end
end

def ignore_exceptions_if_not_in_active_sentry_env
->(_error_or_event) { active_sentry_environments.include?(sentry_environment) }
end

def ignore_excluded_exceptions_in_data_sync
lambda { |error_or_event|
data_sync_ignored_error = data_sync_excluded_exceptions.any? do |exception_to_ignore|
Expand Down
10 changes: 10 additions & 0 deletions lib/govuk_app_config/govuk_error/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

config.silence_ready = !Rails.env.production? if defined?(Rails)

# These are the environments (described by the `SENTRY_CURRENT_ENV`
# ENV variable) where we want to capture Sentry errors. If
# `SENTRY_CURRENT_ENV` isn't in this list, or isn't defined, then
# don't capture the error.
config.active_sentry_environments = %w[
integration-blue-aws
staging
production
]

config.excluded_exceptions = [
# Default ActionDispatch rescue responses
"ActionController::RoutingError",
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_app_config/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GovukAppConfig
VERSION = "2.5.2".freeze
VERSION = "2.6.0".freeze
end
35 changes: 27 additions & 8 deletions spec/govuk_error/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@
describe ".should_capture" do
let(:configuration) { GovukError::Configuration.new(Raven.configuration) }

it "ignores errors if they happen in an environment we don't care about" do
ClimateControl.modify SENTRY_CURRENT_ENV: "some-temporary-environment" do
configuration.active_sentry_environments << "production"
expect(configuration.should_capture.call(StandardError.new)).to eq(false)
end
end

it "captures errors if they happen in an environment we care about" do
ClimateControl.modify SENTRY_CURRENT_ENV: "production" do
configuration.active_sentry_environments << "production"
expect(configuration.should_capture.call(StandardError.new)).to eq(true)
end
end

context "during the data sync" do
around do |example|
ClimateControl.modify GOVUK_DATA_SYNC_PERIOD: "22:00-08:00" do
ClimateControl.modify SENTRY_CURRENT_ENV: "production", GOVUK_DATA_SYNC_PERIOD: "22:00-08:00" do
configuration.active_sentry_environments << "production"
travel_to(Time.current.change(hour: 23)) do
example.run
end
Expand Down Expand Up @@ -70,7 +85,8 @@

context "outside of the data sync" do
around do |example|
ClimateControl.modify GOVUK_DATA_SYNC_PERIOD: "22:00-08:00" do
ClimateControl.modify SENTRY_CURRENT_ENV: "production", GOVUK_DATA_SYNC_PERIOD: "22:00-08:00" do
configuration.active_sentry_environments << "production"
travel_to(Time.current.change(hour: 21)) do
example.run
end
Expand All @@ -87,13 +103,16 @@

describe ".should_capture=" do
it "Allows apps to add their own `should_capture` callback, that is evaluated alongside the default. If both return `true`, then we should capture, but if either returns `false`, then we shouldn't." do
raven_configurator = GovukError::Configuration.new(Raven.configuration)
raven_configurator.should_capture = lambda do |error_or_event|
error_or_event == "do capture"
end
ClimateControl.modify SENTRY_CURRENT_ENV: "production" do
raven_configurator = GovukError::Configuration.new(Raven.configuration)
raven_configurator.active_sentry_environments << "production"
raven_configurator.should_capture = lambda do |error_or_event|
error_or_event == "do capture"
end

expect(raven_configurator.should_capture.call("do capture")).to eq(true)
expect(raven_configurator.should_capture.call("don't capture")).to eq(false)
expect(raven_configurator.should_capture.call("do capture")).to eq(true)
expect(raven_configurator.should_capture.call("don't capture")).to eq(false)
end
end
end
end