Class: Rack::Insight::Instrumentation::Instrument

Inherits:
Object
  • Object
show all
Includes:
Backstage, Logging
Defined in:
lib/rack/insight/instrumentation/instrument.rb

Defined Under Namespace

Classes: MethodCall, Timing

Constant Summary collapse

@@call_seq =
0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger, verbose, verbosity

Methods included from Backstage

#backstage

Constructor Details

#initializeInstrument

Returns a new instance of Instrument.



32
33
34
35
# File 'lib/rack/insight/instrumentation/instrument.rb', line 32

def initialize()
  @start = Time.now
  @collectors = nil
end

Class Method Details

.seq_numberObject



26
27
28
29
30
# File 'lib/rack/insight/instrumentation/instrument.rb', line 26

def self.seq_number
  Thread.exclusive do
    return @@call_seq += 1
  end
end

Instance Method Details

#all_collectorsObject



93
94
95
# File 'lib/rack/insight/instrumentation/instrument.rb', line 93

def all_collectors
  PackageDefinition.all_collectors
end

#collectors_for(method_call) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rack/insight/instrumentation/instrument.rb', line 56

def collectors_for(method_call)
  probe_chain = if method_call.kind == :instance
                  InstanceProbe.get_probe_chain(method_call.context)
                else
                  ClassProbe.get_probe_chain(method_call.context)
                end
  collectors = probe_chain.inject([]) do |list, probe|
    probe.collectors(method_call.method)
  end
  if verbose(:debug)
    logger.debug do
      "Probe chain for: #{method_call.context} #{method_call.kind} #{method_call.method}:\n  #{collectors.map{|col| col.class.name}.join(", ")}"
    end
  end
  collectors
end

#durationObject



110
111
112
# File 'lib/rack/insight/instrumentation/instrument.rb', line 110

def duration
  @timing.duration
end

#finish(env, status, headers, body) ⇒ Object



103
104
105
106
107
108
# File 'lib/rack/insight/instrumentation/instrument.rb', line 103

def finish(env, status, headers, body)
  @timing = Timing.new(@start, @start, Time.now)
  all_collectors.each do |collector|
    collector.request_finish(env, status, headers, body, @timing)
  end
end

#finish_event(method_call, arguments, start_time, result) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/rack/insight/instrumentation/instrument.rb', line 83

def finish_event(method_call, arguments, start_time, result)
  timing = Timing.new(@start, start_time, Time.now)
  if verbose(:debug)
    logger.debug{ "Finishing event: #{method_call.context} #{method_call.kind} #{method_call.method}" }
  end
  collectors_for(method_call).each do |collector|
    collector.after_detect(method_call, timing, arguments, result)
  end
end

#run(object, context = "::", kind = :instance, called_at = caller[0], method = "<unknown>", args = [], &blk) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rack/insight/instrumentation/instrument.rb', line 41

def run(object, context="::", kind=:instance, called_at = caller[0], method = "<unknown>", args=[], &blk)
  file, line, rest = called_at.split(':')
  call_number = backstage{ self.class.seq_number }
  method_call = backstage{ MethodCall.new(call_number, caller(1), file, line, object, context, kind, method, Thread::current) }
  #$stderr.puts [method_call.context, method_call.method].inspect
  start_time = Time.now
  backstage do
    start_event(method_call, args)
  end
  result = blk.call      # execute the provided code block
  backstage do
    finish_event(method_call, args, start_time, result)
  end
end

#start(env) ⇒ Object



97
98
99
100
101
# File 'lib/rack/insight/instrumentation/instrument.rb', line 97

def start(env)
  all_collectors.each do |collector|
    collector.request_start(env, @start)
  end
end

#start_event(method_call, arguments) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/rack/insight/instrumentation/instrument.rb', line 73

def start_event(method_call, arguments)
  if verbose(:debug)
    logger.debug{ "Starting event: #{method_call.context} #{method_call.kind} #{method_call.method}" }
  end

  collectors_for(method_call).each do |collector|
    collector.before_detect(method_call, arguments)
  end
end