Skip to content

Commit

Permalink
Move all plugins stuff to lib/rollbar/plugins
Browse files Browse the repository at this point in the history
The middlewares are still in lib/rollbar/middlewares, but everything is
now loaded from the plugins directory
  • Loading branch information
Jon de Andres committed Apr 25, 2016
1 parent bb66428 commit 3412c9a
Show file tree
Hide file tree
Showing 44 changed files with 432 additions and 354 deletions.
56 changes: 3 additions & 53 deletions lib/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
require 'rollbar/logger_proxy'
require 'rollbar/exception_reporter'
require 'rollbar/util'
require 'rollbar/railtie' if defined?(Rails::VERSION) && Rails::VERSION::MAJOR >= 3
require 'rollbar/delay/girl_friday' if defined?(GirlFriday)
require 'rollbar/delay/thread'
require 'rollbar/truncation'
Expand Down Expand Up @@ -829,7 +828,7 @@ def configure

yield(configuration)

prepare
plugins.load!
reset_notifier!
end

Expand Down Expand Up @@ -861,57 +860,6 @@ def plugins
@plugins ||= Rollbar::Plugins.new
end

def prepare
plugins.require_all
plugins.load!

prepare_js
require_hooks
require_core_extensions
end

def prepare_js
::Rollbar::Js.prepare if configuration.js_enabled
end

def require_hooks
return if configuration.disable_monkey_patch
wrap_delayed_worker

if defined?(ActiveRecord)
require 'active_record/version'
require 'rollbar/active_record_extension' if ActiveRecord::VERSION::MAJOR >= 3
end

require 'rollbar/sidekiq' if defined?(Sidekiq)
require 'rollbar/active_job' if defined?(ActiveJob)
require 'rollbar/goalie' if defined?(Goalie)
require 'rollbar/rack' if defined?(Rack) unless configuration.disable_rack_monkey_patch
require 'rollbar/rake' if defined?(Rake)
end

def require_core_extensions
# This monkey patch is always needed in order
# to use Rollbar.scoped
require 'rollbar/core_ext/thread'

return if configuration.disable_core_monkey_patch

# Needed to avoid active_support (< 4.1.0) bug serializing JSONs
require 'rollbar/core_ext/basic_socket' if monkey_patch_socket?
end

def monkey_patch_socket?
defined?(ActiveSupport::VERSION::STRING)
end

def wrap_delayed_worker
return unless defined?(Delayed) && defined?(Delayed::Worker) && configuration.delayed_job_enabled

require 'rollbar/delayed_job'
Rollbar::Delayed.wrap_worker
end

def notifier
Thread.current[:_rollbar_notifier] ||= Notifier.new(self)
end
Expand Down Expand Up @@ -994,3 +942,5 @@ def report_message_with_request(message, level = 'info', request_data = nil, per
end
end
end

Rollbar.plugins.require_all
14 changes: 0 additions & 14 deletions lib/rollbar/active_record_extension.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/rollbar/core_ext/basic_socket.rb

This file was deleted.

9 changes: 0 additions & 9 deletions lib/rollbar/core_ext/thread.rb

This file was deleted.

33 changes: 0 additions & 33 deletions lib/rollbar/goalie.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/rollbar/js.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,4 @@
require "rollbar/js/version"

module Rollbar
module Js
extend self

attr_reader :framework
attr_reader :framework_loader

def prepare
@framework ||= detect_framework
@framework_loader ||= load_framework_class.new

@framework_loader.prepare
end

private

def detect_framework
case
when defined?(::Rails::VERSION)
:rails
end
end

def load_framework_class
require "rollbar/js/frameworks/#{framework}"

Rollbar::Js::Frameworks.const_get(framework.to_s.capitalize)
end
end
end
6 changes: 0 additions & 6 deletions lib/rollbar/js/frameworks.rb

This file was deleted.

49 changes: 0 additions & 49 deletions lib/rollbar/js/frameworks/rails.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/rollbar/js/version.rb

This file was deleted.

5 changes: 2 additions & 3 deletions lib/rollbar/js/middleware.rb → lib/rollbar/middleware/js.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require 'rack'
require 'rack/response'


module Rollbar
module Js
class Middleware
module Middleware
class Js
attr_reader :app
attr_reader :config

Expand Down
8 changes: 8 additions & 0 deletions lib/rollbar/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def initialize(name)
@loaded = false
end

def configuration
Rollbar.configuration
end

def load!
return unless load?

Expand All @@ -40,6 +44,10 @@ def execute(&block)
callables << block
end

def execute!(&block)
block.call if load?
end

def load?
!loaded && dependencies.all?(&:call)
rescue => e
Expand Down
14 changes: 11 additions & 3 deletions lib/rollbar/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ def require_all
end

def plugin_files
File.expand_path('../plugins/**/*.rb', __FILE__)
File.expand_path('../plugins/*.rb', __FILE__)
end

def define(name, &block)
plugin = Rollbar::Plugin.new(name)
plugin.instance_eval(&block)
return if loaded?(name)

plugin = Rollbar::Plugin.new(name)
collection << plugin

plugin.instance_eval(&block)
end

def load!
collection.each(&:load!)
end

private

def loaded?(name)
collection.any? { |plugin| plugin.name == name }
end
end
end
File renamed without changes.
30 changes: 30 additions & 0 deletions lib/rollbar/plugins/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Rollbar.plugins.define('active_record') do
dependency { !configuration.disable_monkey_patch }
dependency { defined?(ActiveRecord) }
dependency do
require 'active_record/version'

ActiveRecord::VERSION::MAJOR >= 3
end

execute do
module Rollbar
module ActiveRecordExtension
extend ActiveSupport::Concern

def report_validation_errors_to_rollbar
errors.full_messages.each do |error|
Rollbar.log_info "[Rollbar] Reporting form validation error: #{error} for #{self}"
Rollbar.warning("Form Validation Error: #{error} for #{self}")
end
end
end
end
end

execute do
ActiveRecord::Base.class_eval do
include Rollbar::ActiveRecordExtension
end
end
end
16 changes: 16 additions & 0 deletions lib/rollbar/plugins/basic_socket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Rollbar.plugins.define('basic_socket') do
dependency { !configuration.disable_core_monkey_patch }

# Needed to avoid active_support (< 4.1.0) bug serializing JSONs
dependency { defined?(ActiveSupport::VERSION::STRING) }

execute do
require 'socket'

class BasicSocket
def as_json
to_s
end
end
end
end
12 changes: 12 additions & 0 deletions lib/rollbar/plugins/delayed_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Rollbar.plugins.define('delayed_job') do
dependency { !configuration.disable_monkey_patch }
dependency do
defined?(Delayed) && defined?(Delayed::Worker) && configuration.delayed_job_enabled
end

execute do
require 'rollbar/plugins/delayed_job/plugin'

Rollbar::Delayed.wrap_worker
end
end
16 changes: 16 additions & 0 deletions lib/rollbar/plugins/delayed_job/job_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class JobData
attr_reader :job

def initialize(job)
@job = job
end

def to_hash
job_data = job.as_json
# Here job_data['handler'] is a YAML object comming
# from the storage backend
job_data['handler'] = job.payload_object.as_json

job_data
end
end
Loading

0 comments on commit 3412c9a

Please sign in to comment.