Module: Sequel::Plugins::NewrelicInstrumentation::MethodTracer

Included in:
ClassMethods, InstanceMethods
Defined in:
lib/sequel/plugins/newrelic_instrumentation.rb

Overview

Meta-programming for creating method tracers for the Sequel::Model plugin.

Instance Method Summary collapse

Instance Method Details

#add_method_tracer(method_name, metric = nil, options = {}) ⇒ Object

Install a method named method_name that will trace execution with a metric name derived from metric (or method_name if metric isn’t specified). The options hash is passed as-is though to NewRelic::Agent::MethodTracer#trace_execution_scoped; see the docs for that method for valid settings.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sequel/plugins/newrelic_instrumentation.rb', line 36

def add_method_tracer( method_name, metric=nil, options={} )
  # Shift options hash if metric is omitted
  if metric.is_a?( Hash )
    options = metric
    metric = nil
  end

  metric ||= method_name.to_s

  body = make_tracer_method( metric, options )
  define_method( method_name, &body )
end

#make_tracer_method(opname, options) ⇒ Object

Make a lambda for the method body of the traced method



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sequel/plugins/newrelic_instrumentation.rb', line 19

def make_tracer_method( opname, options )
  body = Proc.new do |*args, &block|
    classname = self.is_a?( Class ) ? self.name : self.class.name
    metric = "ActiveRecord/%s/%s" % [ classname, opname ]
    trace_execution_scoped( metric, options ) do
      super( *args, &block )
    end
  end

  return body
end