Class: Rack::Bug::SpeedTrace::Tracer

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

Instance Method Summary collapse

Methods inherited from TraceRecord

#duration, #time_in_children, #to_json

Methods included from Render

#compiled_source

Constructor Details

#initialize(id, method, uri) ⇒ Tracer

Returns a new instance of Tracer.



88
89
90
91
92
93
94
95
# File 'lib/rack/bug/speedtracer/tracer.rb', line 88

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

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

Instance Method Details

#finishObject



202
203
204
205
206
207
208
209
# File 'lib/rack/bug/speedtracer/tracer.rb', line 202

def finish
  super()

  until @pstack.empty?
    finish_event
  end
  self
end

#finish_eventObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rack/bug/speedtracer/tracer.rb', line 153

def finish_event
  event = @pstack.pop
  if event.nil?
  else
    event.finish


    unless (parent = @pstack.last).nil?
      parent.children.push event
    else
      @children.push event
    end
  end
end

#hash_representationObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rack/bug/speedtracer/tracer.rb', line 168

def hash_representation
  finish
  { 'trace' =>  {

    'url' => "/speedtracer?id=#@id",

    'frameStack' => {

      'range' => range(@start, @finish),
      'operation' =>  {
      'type' =>  'HTTP',
      'label' =>  [@method, @uri].join(' ')
    },
      'children' =>  @children

    }, #end frameStack

      'resources' => {
      'Application' => '/', #Should get the Rails app name...
      'Application.endpoint' => '/' #Should get the env path thing
    }, #From what I can tell, Speed Tracer treats this whole hash as optional

      'range' =>  range(@start, @finish)
  }
  }
end

#make_string_of(array) ⇒ Object



139
140
141
142
143
# File 'lib/rack/bug/speedtracer/tracer.rb', line 139

def make_string_of(array)
  array.map do |item|
    short_string(item)
  end.join(",")
end

#run(context = "::", called_at = , args = [], &blk) ⇒ Object

TODO: Threadsafe



98
99
100
101
102
103
104
105
106
# File 'lib/rack/bug/speedtracer/tracer.rb', line 98

def run(context="::", called_at = caller[0], args=[], &blk)
  file, line, method = called_at.split(':')

  method = method.gsub(/^in|[^\w]+/, '') if method

  start_event(file, line, method, context, args)
  blk.call      # execute the provided code block
  finish_event
end

#short_string(item, max_per_elem = 50) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rack/bug/speedtracer/tracer.rb', line 108

def short_string(item, max_per_elem = 50)
  begin
    string = item.inspect
    if string.length > max_per_elem
      case item
      when NilClass
        "nil"
      when Hash
        "{ " + item.map do |key, value|
          short_string(key, 15) + "=>" + short_string(value, 30)
        end.join(", ") + " }"
      when find_constant("ActionView::Base")
        tmpl = item.template
        if tmpl.nil?
          item.path.inspect
        else
          [tmpl.base_path, tmpl.name].join("/")
        end
      when find_constant("ActiveRecord::Base")
        string = "#{item.class.name}(#{item.id})" 
      else
        string = item.class.name
      end
    else
      string
    end
  rescue Exception => ex
    "..."
  end
end

#start_event(file, line, method, context, arguments) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/rack/bug/speedtracer/tracer.rb', line 145

def start_event(file, line, method, context, arguments)
  @event_id += 1

  arguments_string = make_string_of(arguments)
  event = ServerEvent.new(@event_id, file, line, method, context, arguments_string)
  @pstack.push event
end

#to_htmlObject



195
196
197
198
199
200
# File 'lib/rack/bug/speedtracer/tracer.rb', line 195

def to_html
  hash = hash_representation
  extra = {:self_time => duration - time_in_children}
  "<a href='#{hash['url']}'>Raw JSON</a>\n" + 
    render_template('serverevent', extra.merge(symbolize_hash(hash['trace']['frameStack'])))
end