Module: ScoutRails::Tracer::ClassMethods

Defined in:
lib/scout_rails/tracer.rb

Instance Method Summary collapse

Instance Method Details

#instrument(metric_name, options = {}, &block) ⇒ Object

Options:

  • :scope => If specified, sets the sub-scope for the metric. We allow additional scope level. This is used

when rendering the transaction tree in the UI.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/scout_rails/tracer.rb', line 29

def instrument(metric_name, options={}, &block)
  # don't instrument if (1) NOT inside a transaction and (2) NOT a Controller metric.
  if !Thread::current[:scout_scope_name] and metric_name !~ /\AController\//
    return yield
  end
  if options.delete(:scope)
    Thread::current[:scout_sub_scope] = metric_name 
  end
  stack_item = ScoutRails::Agent.instance.store.record(metric_name)
  begin
    yield
  ensure
    Thread::current[:scout_sub_scope] = nil if Thread::current[:scout_sub_scope] == metric_name
    ScoutRails::Agent.instance.store.stop_recording(stack_item,options)
  end
end

#instrument_method(method, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/scout_rails/tracer.rb', line 46

def instrument_method(method,options = {})
  ScoutRails::Agent.instance.logger.info "Instrumenting #{method}"
  metric_name = options[:metric_name] || default_metric_name(method)
  return if !instrumentable?(method) or instrumented?(method,metric_name)
  class_eval instrumented_method_string(method, {:metric_name => metric_name, :scope => options[:scope]}), __FILE__, __LINE__
  
  alias_method _uninstrumented_method_name(method, metric_name), method
  alias_method method, _instrumented_method_name(method, metric_name)
end

#trace(metric_name, options = {}, &block) ⇒ Object

Use to trace a method call, possibly reporting slow transaction traces to Scout.



17
18
19
20
21
22
23
24
# File 'lib/scout_rails/tracer.rb', line 17

def trace(metric_name, options = {}, &block)
  ScoutRails::Agent.instance.store.reset_transaction!      
  instrument(metric_name, options) do
    Thread::current[:scout_scope_name] = metric_name
    yield
    Thread::current[:scout_scope_name] = nil
  end
end