Skip to content

Commit

Permalink
Merge 9f879bc into 1446466
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlongridge committed Aug 23, 2019
2 parents 1446466 + 9f879bc commit 2f7baec
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Changelog

## TBD

### Enhancements

* Add Ruby (and other framework) version strings to report and session payloads (device.runtimeVersions).
| [560](https://github.com/bugsnag/bugsnag-ruby/pull/560)

### Fixes

* Use `Module#prepend` for Rake integration when on a new enough Ruby version
Expand Down
7 changes: 6 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ Thank you!
- Run the tests with and make sure they all pass

```
rake spec
bundle exec rake spec
```
- You will need to add the following test dependencies:

```
bundle install --with test --binstubs
```
- For adding a new integration (like support for a web framework or worker
queue), include an example in the `example/` directory showing off what
Expand Down
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ group :test, optional: true do
gem 'rdoc', '~> 5.1.0'
gem 'pry'
gem 'addressable', '~> 2.3.8'
gem 'delayed_job' if ruby_version >= Gem::Version.new('2.2.2')
gem 'i18n', ruby_version <= Gem::Version.new('2.3.0') ? '1.4.0': '>1.4.0' if ruby_version >= Gem::Version.new('2.2.2')
if ruby_version >= Gem::Version.new('2.2.2')
gem 'delayed_job', ruby_version < Gem::Version.new('2.5.0') ? '4.1.7': '>4.1.7'
gem 'i18n', ruby_version <= Gem::Version.new('2.3.0') ? '1.4.0' : '>1.4.0'
end
gem 'webmock', ruby_version <= Gem::Version.new('1.9.3') ? '2.3.2': '>2.3.2'
gem 'hashdiff', ruby_version <= Gem::Version.new('1.9.3') ? '0.3.8': '>0.3.8'
end
Expand Down
4 changes: 4 additions & 0 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Configuration
attr_accessor :proxy_password
attr_accessor :timeout
attr_accessor :hostname
attr_accessor :runtime_versions
attr_accessor :ignore_classes
attr_accessor :auto_capture_sessions
attr_accessor :track_sessions
Expand Down Expand Up @@ -93,6 +94,9 @@ def initialize
self.send_code = true
self.meta_data_filters = Set.new(DEFAULT_META_DATA_FILTERS)
self.hostname = default_hostname
self.runtime_versions = {}
self.runtime_versions["ruby"] = RUBY_VERSION
self.runtime_versions["jruby"] = JRUBY_VERSION if defined?(JRUBY_VERSION)
self.timeout = 15
self.notify_release_stages = nil
self.auto_capture_sessions = true
Expand Down
3 changes: 2 additions & 1 deletion lib/bugsnag/integrations/que.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'que'

if defined?(::Que)
handler = proc do |error, job|
begin
Expand Down Expand Up @@ -40,9 +39,11 @@

if Que.respond_to?(:error_notifier=)
Bugsnag.configuration.app_type ||= "que"
Bugsnag.configuration.runtime_versions["que"] = ::Que::Version
Que.error_notifier = handler
elsif Que.respond_to?(:error_handler=)
Bugsnag.configuration.app_type ||= "que"
Bugsnag.configuration.runtime_versions["que"] = ::Que::Version
Que.error_handler = handler
end
end
3 changes: 3 additions & 0 deletions lib/bugsnag/integrations/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def initialize(app)
config.middleware.insert_before(Bugsnag::Middleware::Callbacks, Bugsnag::Middleware::WardenUser) if defined?(Warden)
config.middleware.insert_before(Bugsnag::Middleware::Callbacks, Bugsnag::Middleware::ClearanceUser) if defined?(Clearance)

# Set environment data for payload
config.app_type ||= "rack"
config.runtime_versions["rack"] = ::Rack.release if defined?(::Rack)
config.runtime_versions["sinatra"] = ::Sinatra::VERSION if defined?(::Sinatra)
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/bugsnag/integrations/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Railtie < ::Rails::Railtie
config.release_stage = ENV["BUGSNAG_RELEASE_STAGE"] || ::Rails.env.to_s
config.project_root = ::Rails.root.to_s
config.middleware.insert_before Bugsnag::Middleware::Callbacks, Bugsnag::Middleware::Rails3Request
config.runtime_versions["rails"] = ::Rails::VERSION::STRING
end

ActiveSupport.on_load(:action_controller) do
Expand Down
2 changes: 2 additions & 0 deletions lib/bugsnag/integrations/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def execute(args = nil)
Bugsnag.configuration.app_type ||= "rake"
old_task = Bugsnag.configuration.request_data[:bugsnag_running_task]
Bugsnag.configuration.set_request_data :bugsnag_running_task, self
Bugsnag.configuration.runtime_versions["rake"] = ::Rake::VERSION

super
rescue Exception => ex
Expand Down Expand Up @@ -44,6 +45,7 @@ def execute_with_bugsnag(args=nil)
Bugsnag.configuration.app_type ||= "rake"
old_task = Bugsnag.configuration.request_data[:bugsnag_running_task]
Bugsnag.configuration.set_request_data :bugsnag_running_task, self
Bugsnag.configuration.runtime_versions["rake"] = ::Rake::VERSION

execute_without_bugsnag(args)
rescue Exception => ex
Expand Down
2 changes: 2 additions & 0 deletions lib/bugsnag/integrations/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ def save
Resque.after_fork do
Bugsnag.configuration.app_type = "resque"
Bugsnag.configuration.default_delivery_method = :synchronous
Bugsnag.configuration.runtime_versions["resque"] = ::Resque::VERSION
end
else
Resque.before_first_fork do
Bugsnag.configuration.app_type = "resque"
Bugsnag.configuration.default_delivery_method = :synchronous
Bugsnag.configuration.runtime_versions["resque"] = ::Resque::VERSION
end
end
1 change: 1 addition & 0 deletions lib/bugsnag/integrations/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize
Bugsnag.configuration.internal_middleware.use(Bugsnag::Middleware::Sidekiq)
Bugsnag.configuration.app_type = "sidekiq"
Bugsnag.configuration.default_delivery_method = :synchronous
Bugsnag.configuration.runtime_versions["sidekiq"] = ::Sidekiq::VERSION
end

def call(worker, msg, queue)
Expand Down
7 changes: 0 additions & 7 deletions lib/bugsnag/middleware/rails3_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ def call(report)
})

report.user["id"] = client_ip

# Add the rails version
if report.configuration.send_environment
report.add_tab(:environment, {
:railsVersion => Rails::VERSION::STRING
})
end
end

@bugsnag.call(report)
Expand Down
5 changes: 4 additions & 1 deletion lib/bugsnag/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Report
attr_accessor :delivery_method
attr_accessor :exceptions
attr_accessor :hostname
attr_accessor :runtime_versions
attr_accessor :grouping_hash
attr_accessor :meta_data
attr_accessor :raw_exceptions
Expand All @@ -55,6 +56,7 @@ def initialize(exception, passed_configuration, auto_notify=false)
self.breadcrumbs = []
self.delivery_method = configuration.delivery_method
self.hostname = configuration.hostname
self.runtime_versions = configuration.runtime_versions.dup
self.meta_data = {}
self.release_stage = configuration.release_stage
self.severity = auto_notify ? "error" : "warning"
Expand Down Expand Up @@ -97,7 +99,8 @@ def as_json
},
context: context,
device: {
hostname: hostname
hostname: hostname,
runtimeVersions: runtime_versions
},
exceptions: exceptions,
groupingHash: grouping_hash,
Expand Down
3 changes: 2 additions & 1 deletion lib/bugsnag/session_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def deliver(session_payload)
:version => Bugsnag::Report::NOTIFIER_VERSION
},
:device => {
:hostname => Bugsnag.configuration.hostname
:hostname => Bugsnag.configuration.hostname,
:runtimeVersions => Bugsnag.configuration.runtime_versions
},
:app => {
:version => Bugsnag.configuration.app_version,
Expand Down
33 changes: 32 additions & 1 deletion spec/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: utf-8
require 'spec_helper'
require 'json'

describe Bugsnag::Configuration do
describe "delivery_method" do
Expand Down Expand Up @@ -161,6 +161,37 @@
end
end

describe "#hostname" do
it "has a default value" do
expect(subject.hostname.length).to be > 0
end

it "has a value set by Socket" do
expect(subject.hostname).to eq(Socket.gethostname)
end

it "has a value set by DYNO environment variable" do
ENV['DYNO'] = 'localhost'
expect(subject.hostname).to eq("localhost")
end

after do
ENV['DYNO'] = nil
end
end

describe "#runtime_versions" do
it "has a default value" do
expect(subject.runtime_versions.length).to be > 0
expect(subject.runtime_versions["ruby"]).to eq(RUBY_VERSION)
end

it "has a settable value" do
subject.runtime_versions["ruby"] = '9.9.9'
expect(subject.runtime_versions["ruby"]).to eq('9.9.9')
end
end

describe "logger" do
class TestLogger
attr_accessor :logs
Expand Down
5 changes: 5 additions & 0 deletions spec/integrations/que_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
unless defined?(::Que)
@mocked_que = true
class ::Que
Version = '9.9.9'
class << self
attr_accessor :error_notifier
end
Expand Down Expand Up @@ -53,13 +54,17 @@ def require(path)
allow(Bugsnag).to receive(:configuration).and_return(config)
expect(config).to receive(:app_type)
expect(config).to receive(:app_type=).with('que')
runtime = {}
expect(config).to receive(:runtime_versions).and_return(runtime)
allow(config).to receive(:clear_request_data)
expect(Que).to receive(:error_notifier=) do |handler|
handler.call(error, job)
end

#Kick off
load './lib/bugsnag/integrations/que.rb'

expect(runtime).to eq("que" => "9.9.9")
end

context 'when the job is nil' do
Expand Down
24 changes: 23 additions & 1 deletion spec/integrations/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
app = lambda { |env| raise exception }
rack_stack = Bugsnag::Rack.new(app)

before do
unless defined?(::Rack)
@mocked_rack = true
class Rack
def self.release
'9.9.9'
end
class Request
end
end
end
end

it "re-raises the exception" do
expect { rack_stack.call(rack_env) }.to raise_error(BugsnagTestException)
end
Expand Down Expand Up @@ -47,6 +60,13 @@
}
end

it "applies the rack version" do
app = lambda { |env| raise BugsnagTestException.new("It crashed") }
rack_stack = Bugsnag::Rack.new(app)

expect(Bugsnag.configuration.runtime_versions["rack"]).to eq '9.9.9'
end

it "does not deliver an exception if auto_notify is disabled" do
Bugsnag.configure do |config|
config.auto_notify = false
Expand All @@ -63,6 +83,9 @@
unless defined?(::Rack)
@mocked_rack = true
class Rack
def self.release
'9.9.9'
end
class Request
end
end
Expand Down Expand Up @@ -196,7 +219,6 @@ class Request
end

it "don't mess with middlewares list on each req" do
stub_const('Rack', nil)
app = lambda { |env| ['200', {}, ['']] }

Bugsnag::Rack.new(app)
Expand Down
3 changes: 3 additions & 0 deletions spec/integrations/rake_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'webrick'
require 'spec_helper'
require 'json'
require 'rake'

describe "Bugsnag Rake integration" do
describe Bugsnag::Middleware::Rake do
Expand Down Expand Up @@ -64,6 +65,8 @@
result = request()
expect(result["events"][0]["metaData"]["rake_task"]).not_to be_nil
expect(result["events"][0]["metaData"]["rake_task"]["name"]).to eq("test:crash")
expect(result["events"][0]["app"]["type"]).to eq("rake")
expect(result["events"][0]["device"]["runtimeVersions"]["rake"]).to match(/\A\d+\.\d+\.\d+/)
end
end
end
5 changes: 5 additions & 0 deletions spec/integrations/resque_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
unless defined?(::Resque)
@mocked_resque = true
class ::Resque
VERSION = '9.9.9'
class Worker
end
class Failure
Expand Down Expand Up @@ -44,10 +45,14 @@ def require(path)
expect(fork_check).to receive(:fork_per_job?).and_return(true)
expect(::Resque).to receive(:after_fork).and_yield
expect(Bugsnag.configuration).to receive(:app_type=).with("resque")
runtime = {}
expect(Bugsnag.configuration).to receive(:runtime_versions).and_return(runtime)
expect(Bugsnag.configuration).to receive(:default_delivery_method=).with(:synchronous)

#Kick off
require './lib/bugsnag/integrations/resque'

expect(runtime).to eq("resque" => "9.9.9")
end

it "can configure" do
Expand Down
3 changes: 3 additions & 0 deletions spec/integrations/sidekiq_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'sidekiq'
require 'sidekiq/testing'

class Worker
Expand Down Expand Up @@ -29,6 +30,8 @@ def perform(value)
expect(event["metaData"]["sidekiq"]["msg"]["args"]).to eq([-0])
expect(event["metaData"]["sidekiq"]["msg"]["queue"]).to eq("default")
expect(event["severity"]).to eq("error")
expect(event["app"]["type"]).to eq("sidekiq")
expect(event["device"]["runtimeVersions"]["sidekiq"]).to match(/\A\d+\.\d+\.\d+/)
}
end
end
Expand Down
14 changes: 13 additions & 1 deletion spec/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ def gloops

if defined?(JRUBY_VERSION)

it "should work with java.lang.Throwables" do
it "works with java.lang.Throwables" do
begin
JRubyException.raise!
rescue
Expand All @@ -1267,4 +1267,16 @@ def gloops
}
end
end

it 'includes device data when notify is called' do
Bugsnag.configuration.hostname = 'test-host'
Bugsnag.configuration.runtime_versions["ruby"] = '9.9.9'
Bugsnag.notify(BugsnagTestException.new("It crashed"))

expect(Bugsnag).to have_sent_notification{ |payload, headers|
event = payload["events"][0]
expect(event["device"]["hostname"]).to eq('test-host')
expect(event["device"]["runtimeVersions"]["ruby"]).to eq('9.9.9')
}
end
end
1 change: 1 addition & 0 deletions spec/session_tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
device = payload["device"]
expect(device.include?("hostname")).to be true
expect(device["hostname"]).to eq(Bugsnag.configuration.hostname)
expect(device["runtimeVersions"]["ruby"]).to eq(Bugsnag.configuration.runtime_versions["ruby"])
end

it 'uses middleware to attach session to notification' do
Expand Down

0 comments on commit 2f7baec

Please sign in to comment.