Module: Cequel::Instrumentation::ModuleMethods

Defined in:
lib/cequel/instrumentation.rb

Overview

Metaprogramming method to wrap an existing method with instrumentation

Instance Method Summary collapse

Instance Method Details

#instrument(method_name, opts) ⇒ Object

Instruments `method_name` to publish the value returned by the `data_builder` proc onto `topic`

Example:

extend Instrumentation
instrument :create, data: {topic: "create.cequel", table_name: table_name}

Parameters:

  • method_name (Symbol, String)

    The method to instrument

  • opts (String)

    :topic (“#method_name.cequel”) The name with which to publish this instrumentation

Options Hash (opts):

  • :data_method (Object) — default: nil

    the data to publish along with the notification. If it responds to `#call` it will be called with the record object and the return value used for each notification.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cequel/instrumentation.rb', line 28

def instrument(method_name, opts)
  data = opts[:data]
  topic = opts.fetch(:topic, "#{method_name}.cequel")

  data_proc = if data.respond_to? :call
                data
              else
                ->(_) { data }
              end

  define_method(:"__data_for_#{method_name}_instrumentation", &data_proc)

  mod = Module.new
  mod.module_eval <<-METH
    def #{method_name}(*args)
      instrument("#{topic}",
                 __data_for_#{method_name}_instrumentation(self)) do
        super(*args)
      end
    end
  METH

  prepend mod
end