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

Add support for Active Job #670

Merged
merged 7 commits into from
Aug 5, 2021
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
12 changes: 12 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ AllCops:
- 'spec/fixtures/**/*'
- 'features/fixtures/**/*'

Lint/RescueException:
Exclude:
- 'lib/bugsnag/integrations/**/*'

Metrics/AbcSize:
Exclude:
- 'lib/bugsnag/configuration.rb'

Metrics/ClassLength:
Exclude:
- 'lib/bugsnag/configuration.rb'

# We can't use ".freeze" on our constants in case users are monkey patching
# them — this would be a BC break
Style/MutableConstant:
Expand Down
10 changes: 0 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,6 @@ Lint/RedundantRequireStatement:
- 'lib/bugsnag/delivery/thread_queue.rb'
- 'lib/bugsnag/session_tracker.rb'

# Offense count: 7
Lint/RescueException:
Exclude:
- 'lib/bugsnag/integrations/delayed_job.rb'
- 'lib/bugsnag/integrations/mailman.rb'
- 'lib/bugsnag/integrations/rack.rb'
- 'lib/bugsnag/integrations/rake.rb'
- 'lib/bugsnag/integrations/shoryuken.rb'
- 'lib/bugsnag/integrations/sidekiq.rb'

# Offense count: 1
# Cop supports --auto-correct.
Lint/SendWithMixinArgument:
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

## TBD

### Enhancements

* Add support for tracking exceptions and capturing metadata in Active Job, when not using an existing integration
| [#670](https://github.com/bugsnag/bugsnag-ruby/pull/670)

## v6.21.0 (23 June 2021)

### Enhancements
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ActiveJobController < ActionController::Base
protect_from_forgery

def handled
NotifyJob.perform_later(1, "hello", { a: "a", b: "b" }, keyword: true)

render json: {}
end

def unhandled
UnhandledJob.perform_later(123, { abc: "xyz" }, "abcxyz")

render json: {}
end
end
4 changes: 2 additions & 2 deletions features/fixtures/rails4/app/app/jobs/notify_job.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class NotifyJob < ApplicationJob
def perform
def perform(*args, **kwargs)
Bugsnag.notify("Failed")
end
end
end
17 changes: 17 additions & 0 deletions features/fixtures/rails4/app/app/jobs/unhandled_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Rails 4 doesn't automatically retry jobs, so this is a quick hack to allow us
# to run the same Rails 5/6 tests against Rails 4 by allowing one retry
$attempts = 0

class UnhandledJob < ApplicationJob
rescue_from(RuntimeError) do |exception|
raise exception if $attempts >= 2

retry_job
end

def perform(*args, **kwargs)
$attempts += 1

raise 'Oh no!'
end
end
1 change: 1 addition & 0 deletions features/fixtures/rails4/app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
get "/devise/(:action)", controller: 'devise'
get "/breadcrumbs/(:action)", controller: 'breadcrumbs'
get "/mongo/(:action)", controller: 'mongo'
get "/active_job/(:action)", controller: 'active_job'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ActiveJobController < ActionController::Base
protect_from_forgery

def handled
NotifyJob.perform_later(1, "hello", { a: "a", b: "b" }, keyword: true)

render json: {}
end

def unhandled
UnhandledJob.perform_later(123, { abc: "xyz" }, "abcxyz")

render json: {}
end
end
4 changes: 2 additions & 2 deletions features/fixtures/rails5/app/app/jobs/notify_job.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class NotifyJob < ApplicationJob
def perform
def perform(*args, **kwargs)
Bugsnag.notify("Failed")
end
end
end
7 changes: 7 additions & 0 deletions features/fixtures/rails5/app/app/jobs/unhandled_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class UnhandledJob < ApplicationJob
retry_on RuntimeError, wait: 1.second, attempts: 2

def perform(*args, **kwargs)
raise 'Oh no!'
end
end
3 changes: 3 additions & 0 deletions features/fixtures/rails5/app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@
get 'mongo/success_crash', to: 'mongo#success_crash'
get 'mongo/get_crash', to: 'mongo#get_crash'
get 'mongo/failure_crash', to: 'mongo#failure_crash'

get 'active_job/handled', to: 'active_job#handled'
get 'active_job/unhandled', to: 'active_job#unhandled'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ActiveJobController < ActionController::Base
protect_from_forgery

def handled
NotifyJob.perform_later(1, "hello", { a: "a", b: "b" }, keyword: true)

render json: {}
end

def unhandled
UnhandledJob.perform_later(123, { abc: "xyz" }, "abcxyz")

render json: {}
end
end
4 changes: 2 additions & 2 deletions features/fixtures/rails6/app/app/jobs/notify_job.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class NotifyJob < ApplicationJob
def perform
def perform(*args, **kwargs)
Bugsnag.notify("Failed")
end
end
end
7 changes: 7 additions & 0 deletions features/fixtures/rails6/app/app/jobs/unhandled_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class UnhandledJob < ApplicationJob
retry_on RuntimeError, wait: 1.second, attempts: 2

def perform(*args, **kwargs)
raise 'Oh no!'
end
end
3 changes: 3 additions & 0 deletions features/fixtures/rails6/app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@
get 'mongo/success_crash', to: 'mongo#success_crash'
get 'mongo/get_crash', to: 'mongo#get_crash'
get 'mongo/failure_crash', to: 'mongo#failure_crash'

get 'active_job/handled', to: 'active_job#handled'
get 'active_job/unhandled', to: 'active_job#unhandled'
end
74 changes: 74 additions & 0 deletions features/rails_features/active_job.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
Feature: Active Job

@rails4 @rails5 @rails6
Scenario: A handled error will be delivered
Given I start the rails service
When I navigate to the route "/active_job/handled" on the rails app
And I wait to receive a request
Then the request is valid for the error reporting API version "4.0" for the "Ruby Bugsnag Notifier"
And the event "unhandled" is false
And the event "severity" equals "warning"
And the event "context" equals "NotifyJob@default"
And the event "app.type" equals "active job"
And the exception "errorClass" equals "RuntimeError"
And the exception "message" equals "Failed"
And the event "metaData.active_job.job_id" matches "^[0-9a-f-]{36}$"
And the event "metaData.active_job.job_name" equals "NotifyJob"
And the event "metaData.active_job.queue" equals "default"
And the event "metaData.active_job.locale" equals "en"
And the event "metaData.active_job.arguments.0" equals 1
And the event "metaData.active_job.arguments.1" equals "hello"
And the event "metaData.active_job.arguments.2.a" equals "a"
And the event "metaData.active_job.arguments.2.b" equals "b"
And the event "metaData.active_job.arguments.3.keyword" is true
And in Rails versions ">=" 5 the event "metaData.active_job.provider_job_id" matches "^[0-9a-f-]{36}$"
And in Rails versions ">=" 5 the event "metaData.active_job.executions" equals 1
And in Rails versions ">=" 6 the event "metaData.active_job.timezone" equals "UTC"
And in Rails versions ">=" 6 the event "metaData.active_job.enqueued_at" is a timestamp

@rails4 @rails5 @rails6
Scenario: An unhandled error will be delivered
Given I start the rails service
When I navigate to the route "/active_job/unhandled" on the rails app
And I wait to receive 2 requests
Then the request is valid for the error reporting API version "4.0" for the "Ruby Bugsnag Notifier"
And the event "unhandled" is true
And the event "severity" equals "error"
And the event "context" equals "UnhandledJob@default"
And the event "app.type" equals "active job"
And the event "severityReason.type" equals "unhandledExceptionMiddleware"
And the event "severityReason.attributes.framework" equals "Active Job"
And the exception "errorClass" equals "RuntimeError"
And the exception "message" equals "Oh no!"
And the event "metaData.active_job.job_id" matches "^[0-9a-f-]{36}$"
And the event "metaData.active_job.job_name" equals "UnhandledJob"
And the event "metaData.active_job.queue" equals "default"
And the event "metaData.active_job.locale" equals "en"
And the event "metaData.active_job.arguments.0" equals 123
And the event "metaData.active_job.arguments.1.abc" equals "xyz"
And the event "metaData.active_job.arguments.2" equals "abcxyz"
And in Rails versions ">=" 5 the event "metaData.active_job.provider_job_id" matches "^[0-9a-f-]{36}$"
And in Rails versions ">=" 5 the event "metaData.active_job.executions" equals 1
And in Rails versions ">=" 6 the event "metaData.active_job.timezone" equals "UTC"
And in Rails versions ">=" 6 the event "metaData.active_job.enqueued_at" is a timestamp
When I discard the oldest request
Then the request is valid for the error reporting API version "4.0" for the "Ruby Bugsnag Notifier"
And the event "unhandled" is true
And the event "severity" equals "error"
And the event "context" equals "UnhandledJob@default"
And the event "app.type" equals "active job"
And the event "severityReason.type" equals "unhandledExceptionMiddleware"
And the event "severityReason.attributes.framework" equals "Active Job"
And the exception "errorClass" equals "RuntimeError"
And the exception "message" equals "Oh no!"
And the event "metaData.active_job.job_id" matches "^[0-9a-f-]{36}$"
And the event "metaData.active_job.job_name" equals "UnhandledJob"
And the event "metaData.active_job.queue" equals "default"
And the event "metaData.active_job.locale" equals "en"
And the event "metaData.active_job.arguments.0" equals 123
And the event "metaData.active_job.arguments.1.abc" equals "xyz"
And the event "metaData.active_job.arguments.2" equals "abcxyz"
And in Rails versions ">=" 5 the event "metaData.active_job.provider_job_id" matches "^[0-9a-f-]{36}$"
And in Rails versions ">=" 5 the event "metaData.active_job.executions" equals 2
And in Rails versions ">=" 6 the event "metaData.active_job.timezone" equals "UTC"
And in Rails versions ">=" 6 the event "metaData.active_job.enqueued_at" is a timestamp
54 changes: 54 additions & 0 deletions features/steps/ruby_notifier_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,57 @@
And the payload field "#{field}" matches the JSON fixture in "features/fixtures/sidekiq/payloads/unhandled_metadata_ca_#{created_at_present}.json"
}
end

def rails_version_matches?(operator, version_to_compare)
# send the given operator as a method to the current rails version
# this will evaluate to e.g. '6.send(">=", 5)', which is the same as '6 >= 5'
ENV["RAILS_VERSION"].to_i.send(operator, version_to_compare)
end

Then("in Rails versions {string} {int} the event {string} equals {string}") do |operator, version, path, expected|
if rails_version_matches?(operator, version)
steps %Q{
And the event "#{path}" equals "#{expected}"
}
else
steps %Q{
And the event "#{path}" is null
}
end
end

Then("in Rails versions {string} {int} the event {string} equals {int}") do |operator, version, path, expected|
if rails_version_matches?(operator, version)
steps %Q{
And the event "#{path}" equals #{expected}
}
else
steps %Q{
And the event "#{path}" is null
}
end
end

Then("in Rails versions {string} {int} the event {string} matches {string}") do |operator, version, path, expected|
if rails_version_matches?(operator, version)
steps %Q{
And the event "#{path}" matches "#{expected}"
}
else
steps %Q{
And the event "#{path}" is null
}
end
end

Then("in Rails versions {string} {int} the event {string} is a timestamp") do |operator, version, path|
if rails_version_matches?(operator, version)
steps %Q{
And the event "#{path}" is a timestamp
}
else
steps %Q{
And the event "#{path}" is null
}
end
end
Loading