Module: Strumbar::Instrumentation::Mongoid

Defined in:
lib/strumbar/instrumentation/mongoid.rb,
lib/strumbar/instrumentation/mongoid/runtime_tracker.rb,
lib/strumbar/instrumentation/mongoid/controller_runtime.rb

Defined Under Namespace

Modules: ControllerRuntime Classes: RuntimeTracker

Constant Summary collapse

CALLS_TO_BE_INSTRUMENTED =
[ :read, :write ]

Class Method Summary collapse

Class Method Details

.load(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/strumbar/instrumentation/mongoid.rb', line 9

def self.load(options={})
  options[:rate] ||= Strumbar.default_rate

  Strumbar.subscribe 'mongo.mongoid' do |client, event|
    RuntimeTracker.runtime += event.duration
    RuntimeTracker.count   += 1
  end

  if defined?(::ActionController)
    ActiveSupport.on_load(:action_controller) do
      include ControllerRuntime
    end
  end

  unless ::Moped::Connection.instance_methods.include? :read_with_instrumentation
    ::Moped::Connection.module_eval do
      CALLS_TO_BE_INSTRUMENTED.each do |method|
        class_eval <<-CODE, __FILE__, __LINE__ + 1
          def #{method}_with_instrumentation(*args, &block)
            Strumbar.strum 'mongo.mongoid', name: "#{method}" do
              #{method}_without_instrumentation(*args, &block)
            end
          end
        CODE

        alias_method_chain method, :instrumentation
      end
    end
  end
end