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

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

Instance Attribute Summary

Attributes inherited from TraceRecord

#start

Instance Method Summary collapse

Methods inherited from TraceRecord

#duration, #time_in_children, #to_json

Methods included from Render

#compiled_source

Methods included from Render

#compile, #compile!, #compiled_source, #method_name, #method_name_without_locals, #render_template, #signed_params

Constructor Details

#initialize(id, method, uri) ⇒ Tracer

Returns a new instance of Tracer.



90
91
92
93
94
95
96
97
# File 'lib/rack/bug/panels/speedtracer_panel/tracer.rb', line 90

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

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

Instance Method Details

#finishObject



204
205
206
207
208
209
210
211
# File 'lib/rack/bug/panels/speedtracer_panel/tracer.rb', line 204

def finish
  super()

  until @pstack.empty?
    finish_event
  end
  self
end

#finish_eventObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rack/bug/panels/speedtracer_panel/tracer.rb', line 155

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



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

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



141
142
143
144
145
# File 'lib/rack/bug/panels/speedtracer_panel/tracer.rb', line 141

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

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

TODO: Threadsafe



100
101
102
103
104
105
106
107
108
# File 'lib/rack/bug/panels/speedtracer_panel/tracer.rb', line 100

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



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
138
139
# File 'lib/rack/bug/panels/speedtracer_panel/tracer.rb', line 110

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



147
148
149
150
151
152
153
# File 'lib/rack/bug/panels/speedtracer_panel/tracer.rb', line 147

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



197
198
199
200
201
202
# File 'lib/rack/bug/panels/speedtracer_panel/tracer.rb', line 197

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