Module: ActiveSupport::OperationLogger::EventInstrumenterFactory

Defined in:
lib/active_support/operation_logger.rb

Overview

A generic factory responsible for creating modules that can be used to decorate classes with ActiveSupport::Notifications

Class Method Summary collapse

Class Method Details

.instrumenter_for(klass, methods, event_namespace, filter = nil) ⇒ Module

Returns a module for prepending.

Parameters:

  • klass (Class)
  • methods (Array<Symbol>)
  • event_namespace (Symbol)
  • filter (#call) (defaults to: nil)

Returns:

  • (Module)

    a module for prepending



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_support/operation_logger.rb', line 24

def self.instrumenter_for(klass, methods, event_namespace, filter = nil)
  Module.new do
    methods.each do |m|
      define_method m do |*args, &block|
        if !filter || filter.call(m, *args, &block)
          command_str = if args.size == 1 && args.first.is_a?(Array)
                          args.first.map(&:inspect).join(', ')
                        else
                          args.map(&:inspect).join(' ')
                        end
          event_id = "call.#{event_namespace}"
          name = "#{event_namespace.to_s.camelize} #{m}"
          ActiveSupport::Notifications.instrument(event_id, name: name, command: command_str) do
            super *args, &block
          end
        else
          super *args, &block
        end
      end
    end
  end
end