Module: Fiveruns::Dash::Instrument

Defined in:
lib/fiveruns/dash/instrument.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.add(*raw_targets, &handler) ⇒ Object

call-seq:

Instrument.add("ClassName#instance_method", ...) { |instance, time, *args| ... }
Instrument.add("ClassName::class_method", ...) { |klass, time, *args| ... }
Instrument.add("ClassName.class_method", ...) { |klass, time, *args| ... }

Add a handler to be called every time a method is invoked



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fiveruns/dash/instrument.rb', line 17

def self.add(*raw_targets, &handler)
  options = raw_targets.last.is_a?(Hash) ? raw_targets.pop : {}
  raw_targets.each do |raw_target|
    begin
      obj, meth = case raw_target
      when /^(.+)#(.+)$/
        [Fiveruns::Dash::Util.constantize($1), $2]
      when /^(.+)(?:\.|::)(.+)$/
        [(class << Fiveruns::Dash::Util.constantize($1); self; end), $2]
      else
        raise Error, "Bad target format: #{raw_target}"
      end
      instrument(obj, meth, options, &handler)
    rescue Fiveruns::Dash::Instrument::Error => em
      raise em
    rescue => e
      Fiveruns::Dash.logger.error "Unable to instrument '#{raw_target}': #{e.message}"
      Fiveruns::Dash.logger.error e.backtrace.join("\n\t")
    end
  end
end

.handlersObject



7
8
9
# File 'lib/fiveruns/dash/instrument.rb', line 7

def self.handlers
  @handlers ||= []
end

.reentrant_timing(token, offset, this, limit_to_within, args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fiveruns/dash/instrument.rb', line 39

def self.reentrant_timing(token, offset, this, limit_to_within, args)
  # token allows us to handle re-entrant timing, see e.g. ar_time
  Thread.current[token] = 0 if Thread.current[token].nil?
  Thread.current[token] = Thread.current[token] + 1
  begin
    start = Time.now
    result = yield
  ensure
    time = Time.now - start
    Thread.current[token] = Thread.current[token] - 1
    if Thread.current[token] == 0
      if !limit_to_within || (Thread.current[:dash_markers] || []).include?(limit_to_within)
        ::Fiveruns::Dash::Instrument.handlers[offset].call(this, time, *args)
      end
    end
  end
  result
end

.timing(offset, this, args, mark, limit_to_within) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fiveruns/dash/instrument.rb', line 58

def self.timing(offset, this, args, mark, limit_to_within)
  if mark
    Thread.current[:dash_markers] ||= []
    Thread.current[:dash_markers].push mark
  end
  start = Time.now
  begin
    result = yield
  ensure
    time = Time.now - start
    Thread.current[:dash_markers].pop if mark

    if !limit_to_within || (Thread.current[:dash_markers] || []).include?(limit_to_within)
      ::Fiveruns::Dash::Instrument.handlers[offset].call(this, time, *args)
    end
  end
  result
end