Class: Rack::SpeedTracer::Tracer

Inherits:
TraceRecord show all
Defined in:
lib/rack/speedtracer/tracer.rb

Instance Method Summary collapse

Constructor Details

#initialize(id, method, uri) ⇒ Tracer

Returns a new instance of Tracer.



55
56
57
58
59
60
61
62
# File 'lib/rack/speedtracer/tracer.rb', line 55

def initialize(id, method, uri)
  super(id)

  @method = method
  @uri = uri
  @event_id = 0
  @pstack = []
end

Instance Method Details

#finishObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rack/speedtracer/tracer.rb', line 99

def finish
  super()

  Yajl::Encoder.encode({
    'trace' =>  {
      'date' =>  @start.to_i,
      'application' => 'Rack SpeedTracer',
      'range' =>  range(@start, @finish),
      'id' =>  @id,
      'frameStack' =>  {
        'range' =>  range(@start, @finish),
        'id' =>  '0',
        'operation' =>  {
          'type' =>  'HTTP',
          'label' =>  [@method, @uri].join(' ')
        },
        'children' =>  @children
      }
    }
  })
end

#run(name = '', &blk) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rack/speedtracer/tracer.rb', line 64

def run(name = '', &blk)
  file, line, method = caller.first.split(':')
  method = method.gsub(/^in|[^\w]+/, '') if method

  # SpeedTracer allows us to nest events via child relationships,
  # which means that we can use a stack to keep track of the currently
  # executing events to produce a proper execution tree.
  #
  # Example execution graph:
  #
  # root
  # -- event 1
  #    ---- event 2
  # -- event 3
  #    ----- event 4
  #    ----- event 5
  #         ------ event 6
  #

  @event_id += 1
  event = ServerEvent.new(@event_id, file, line, method, name)
  @pstack.push event

  blk.call      # execute the provided code block
  event.finish  # finalize current event timers
  @pstack.pop   # pop current event from parent stack

  if parent = @pstack.last
    parent.children.push event
  else
    # no parent, means this is a child of root node
    @children.push event
  end
end