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

Install Sentry #5

Merged
merged 6 commits into from
Dec 22, 2023
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "prometheus-client"
gem "rest-client"
gem "sentry-ruby"

group :test do
gem "climate_control"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ GEM
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
sentry-ruby (5.15.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
timecop (0.9.8)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand All @@ -125,6 +127,7 @@ DEPENDENCIES
rest-client
rspec
rubocop-govuk
sentry-ruby
timecop
webmock

Expand Down
3 changes: 2 additions & 1 deletion collect
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Bundler.setup(:default)

$LOAD_PATH << "./lib"

require "error"
require "govuk_sli_collector"

GovukSliCollector.call
Error.report { GovukSliCollector.call }
32 changes: 32 additions & 0 deletions lib/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "sentry-ruby"

class Error
class << self
def catch_and_report(&block)
configure

block.call
rescue StandardError => e
Sentry.capture_exception(e)
end

def report(&block)
configure

block.call
rescue StandardError => e
Sentry.capture_exception(e)

raise e
end

private

def configure
@configure ||= begin
Sentry.init
true
end
end
end
end
3 changes: 2 additions & 1 deletion lib/govuk_sli_collector.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "error"
require "govuk_sli_collector/publishing_latency_sli"

module GovukSliCollector
def self.call
GovukSliCollector::PublishingLatencySli.new.call
Error.catch_and_report { PublishingLatencySli.new.call }
end
end
37 changes: 37 additions & 0 deletions spec/error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "error"

RSpec.describe Error do
describe ".catch_and_report" do
it "sends any exceptions to Sentry and doesn't let them propagate" do
stub_const("SilencedError", Class.new(StandardError))

allow(Sentry).to receive(:capture_exception)

expect {
described_class.catch_and_report do
raise SilencedError, "This error is sent to Sentry and silenced"
end
}.not_to raise_error

expect(Sentry).to have_received(:capture_exception)
.with(an_instance_of(SilencedError))
end
end

describe ".report" do
it "sends any exceptions to Sentry but also lets them propagate" do
stub_const("NoisyError", Class.new(StandardError))

allow(Sentry).to receive(:capture_exception)

expect {
described_class.report do
raise NoisyError, "This error is sent to Sentry and re-raised"
end
}.to raise_error(NoisyError)

expect(Sentry).to have_received(:capture_exception)
.with(an_instance_of(NoisyError))
end
end
end
26 changes: 20 additions & 6 deletions spec/govuk_sli_collector_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
require "govuk_sli_collector"

RSpec.describe GovukSliCollector do
it "collects the publishing latency SLI" do
publishing_latency_sli = instance_spy(GovukSliCollector::PublishingLatencySli)
allow(GovukSliCollector::PublishingLatencySli).to receive(:new)
.and_return(publishing_latency_sli)
describe "collecting the Publishing Latency SLI" do
it "invokes PublishingLatencySli#call" do
publishing_latency_sli = instance_spy(GovukSliCollector::PublishingLatencySli)
allow(GovukSliCollector::PublishingLatencySli).to receive(:new)
.and_return(publishing_latency_sli)

described_class.call
described_class.call

expect(publishing_latency_sli).to have_received(:call)
expect(publishing_latency_sli).to have_received(:call)
end

it "sends any exceptions to Sentry and doesn't let them propagate" do
stub_const("SilencedError", Class.new(StandardError))
expect(GovukSliCollector::PublishingLatencySli).to receive(:new)
.and_raise(SilencedError, "This error is sent to Sentry and silenced")

expect(Error).to receive(:catch_and_report).and_call_original

expect {
described_class.call
}.not_to raise_error
end
end
end