Skip to content

Commit

Permalink
Merge pull request #13141 from mkllnk/dfc-error-logging
Browse files Browse the repository at this point in the history
Report DFC server errors to Bugsnag
  • Loading branch information
mkllnk authored Feb 12, 2025
2 parents 2d577e9 + d9cb1e8 commit 020cced
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
7 changes: 7 additions & 0 deletions app/services/alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ class Alert
# )
def self.raise(error, metadata = {}, &block)
Bugsnag.notify(error) do |payload|
unless metadata.respond_to?(:each)
metadata = { metadata: { data: metadata } }
end

metadata.each do |name, data|
# Bugsnag only reports metadata when given a Hash.
data = { data: } unless data.is_a?(Hash)
payload.add_metadata(name, data)
end

block.call(payload)
end
end
Expand Down
5 changes: 3 additions & 2 deletions config/initializers/bugsnag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Bugsnag.configure do |config|
config.api_key = ENV['BUGSNAG_API_KEY']
config.release_stage = Rails.env
config.app_version = Rails.application.config.x.git_version

# Avoid missing API key warning without changing the Rails log level.
if Rails.env.development?
config.logger = Logger.new(STDOUT)
Expand All @@ -10,5 +11,5 @@
# If you want to notify Bugsnag in dev or test then set the env var:
# spring stop
# BUGSNAG=true ./bin/rails console
config.notify_release_stages = %w(production staging) unless ENV["BUGSNAG"]
config.enabled_release_stages = %w(production staging) unless ENV["BUGSNAG"]
end
3 changes: 3 additions & 0 deletions engines/dfc_provider/app/services/dfc_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def call(url, data = nil, method: nil)
# If access was denied and our token is stale then refresh and retry:
refresh_access_token!
response = request(url, data, method:)
rescue Faraday::ServerError => e
Alert.raise(e, { dfc_request: { data: } })
raise
end

response.body
Expand Down
20 changes: 12 additions & 8 deletions engines/dfc_provider/spec/services/dfc_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
account.updated_at = 1.day.ago

expect {
api.call("http://example.net/api")
}
expect { api.call("http://example.net/api") }
.to raise_error(Faraday::UnauthorizedError)
.and change {
account.token
}.and change {
account.refresh_token
}
.and change { account.token }
.and change { account.refresh_token }
end

it "doesn't try to refresh the token when it's still fresh" do
Expand Down Expand Up @@ -86,4 +81,13 @@
products = graph.select { |s| s.semanticType == "dfc-b:SuppliedProduct" }
expect(products).to be_present
end

it "reports and raises server errors" do
stub_request(:get, "http://example.net/api").to_return(status: 500)

expect(Bugsnag).to receive(:notify)

expect { api.call("http://example.net/api") }
.to raise_error(Faraday::ServerError)
end
end
32 changes: 20 additions & 12 deletions spec/services/alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
original_config = nil
Bugsnag.configure do |config|
original_config = config.dup
config.api_key ||= "dummy-key"
config.notify_release_stages = ["test"]
config.api_key ||= "00000000000000000000000000000000"
config.enabled_release_stages = ["test"]
config.delivery_method = :synchronous
end

example.run

Bugsnag.configure do |config|
config.api_key ||= original_config.api_key
config.notify_release_stages = original_config.notify_release_stages
config.api_key = original_config.api_key
config.enabled_release_stages = original_config.notify_release_stages
config.delivery_method = original_config.delivery_method
end
end
Expand All @@ -28,8 +28,6 @@
end

it "adds context" do
pending "Bugsnag calls in CI" if ENV.fetch("CI", false)

expect_any_instance_of(Bugsnag::Report).to receive(:add_metadata).with(
:order, { number: "ABC123" }
)
Expand All @@ -43,9 +41,23 @@
)
end

it "is compatible with Bugsnag API" do
pending "Bugsnag calls in CI" if ENV.fetch("CI", false)
it "adds context given as keyword argument" do
expect_any_instance_of(Bugsnag::Report).to receive(:add_metadata).with(
:thing, { data: "ABC123" }
)

Alert.raise("hey", thing: "ABC123")
end

it "adds simple values as context" do
expect_any_instance_of(Bugsnag::Report).to receive(:add_metadata).with(
:metadata, { data: "ABC123" }
)

Alert.raise("hey", "ABC123")
end

it "is compatible with Bugsnag API" do
expect_any_instance_of(Bugsnag::Report).to receive(:add_metadata).with(
:order, { number: "ABC123" }
)
Expand All @@ -56,8 +68,6 @@
end

it "sends ActiveRecord objects" do
pending "Bugsnag calls in CI" if ENV.fetch("CI", false)

order = Spree::Order.new(number: "ABC123")

expect_any_instance_of(Bugsnag::Report).to receive(:add_metadata).with(
Expand All @@ -68,8 +78,6 @@
end

it "notifies Bugsnag when ActiveRecord object is missing" do
pending "Bugsnag calls in CI" if ENV.fetch("CI", false)

expect_any_instance_of(Bugsnag::Report).to receive(:add_metadata).with(
"NilClass", { record_was_nil: true }
)
Expand Down

0 comments on commit 020cced

Please sign in to comment.