Class: Module

Inherits:
Object show all
Includes:
ClassLoadingWatcher::SanityCheck
Defined in:
lib/new_relic/shim_agent.rb,
lib/new_relic/agent/method_tracer.rb,
lib/new_relic/agent/patch_const_missing.rb

Overview

from method_tracer.rb

Instance Method Summary collapse

Methods included from ClassLoadingWatcher::SanityCheck

#new_relic_check_for_badness

Instance Method Details

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

Add a method tracer to the specified method.

metric_name_code is ruby code that determines the name of the metric to be collected during tracing. As such, the code should be provided in ‘single quote’ strings rather than “double quote” strings, so that #{} evaluation happens at traced method execution time. Example: tracing a method :foo, where the metric name is the first argument converted to a string

add_method_tracer :foo, '#{args.first.to_s}'

statically defined metric names can be specified as regular strings push_scope specifies whether this method tracer should push the metric name onto the scope stack.



72
73
# File 'lib/new_relic/agent/method_tracer.rb', line 72

def add_method_tracer (*args)
end

#new_relic_const_missing(*args) ⇒ Object



101
102
103
104
# File 'lib/new_relic/agent/patch_const_missing.rb', line 101

def new_relic_const_missing(*args)
  new_relic_check_for_badness("Module #{self.name} const_missing", *args)
  non_new_relic_const_missing(*args)
end

#remove_method_tracer(method_name, metric_name_code) ⇒ Object

Not recommended for production use, because tracers must be removed in reverse-order from when they were added, or else other tracers that were added to the same method may get removed as well.



147
148
# File 'lib/new_relic/agent/method_tracer.rb', line 147

def remove_method_tracer(*args)
end

#trace_method_execution(metric_name, push_scope, produce_metric, deduct_call_time_from_parent, &block) ⇒ Object

Original method preserved for API backward compatibility



5
6
7
# File 'lib/new_relic/agent/method_tracer.rb', line 5

def trace_method_execution (*args)
  yield
end

#trace_method_execution_no_scope(metric_name) ⇒ Object

This is duplicated inline in add_method_tracer



14
15
16
17
18
19
20
21
22
# File 'lib/new_relic/agent/method_tracer.rb', line 14

def trace_method_execution_no_scope(metric_name)
  t0 = Time.now.to_f
  stats = @@newrelic_stats_engine.get_stats_no_scope metric_name

  result = yield
  duration = Time.now.to_f - t0              # for some reason this is 3 usec faster than Time - Time
  stats.trace_call(duration, duration)    
  result 
end

#trace_method_execution_with_scope(metric_name, produce_metric, deduct_call_time_from_parent) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/new_relic/agent/method_tracer.rb', line 24

def trace_method_execution_with_scope(metric_name, produce_metric, deduct_call_time_from_parent)

  t0 = Time.now.to_f
  stats = nil
  
  begin
    # Keep a reference to the scope we are pushing so we can do a sanity check making
    # sure when we pop we get the one we 'expected'
    expected_scope = @@newrelic_stats_engine.push_scope(metric_name, t0, deduct_call_time_from_parent)
    
    stats = @@newrelic_stats_engine.get_stats metric_name, true if produce_metric
  rescue => e
    NewRelic::Config.instance.log.error("Caught exception in trace_method_execution header. Metric name = #{metric_name}, exception = #{e}")
    NewRelic::Config.instance.log.error(e.backtrace.join("\n"))
  end

  begin
    yield
  ensure
    t1 = Time.now.to_f
    duration = t1 - t0
    
    begin
      if expected_scope
        scope = @@newrelic_stats_engine.pop_scope expected_scope, duration, t1
        
        exclusive = duration - scope.children_time
        stats.trace_call(duration, exclusive) if stats
      end
    rescue => e
      NewRelic::Config.instance.log.error("Caught exception in trace_method_execution footer. Metric name = #{metric_name}, exception = #{e}")
      NewRelic::Config.instance.log.error(e.backtrace.join("\n"))
    end
  end
end