Class: Rack::MiniProfiler

Inherits:
Object
  • Object
show all
Extended by:
ProfilingMethods
Defined in:
lib/mini_profiler/config.rb,
lib/mini_profiler/profiler.rb,
lib/mini_profiler/timer_struct.rb,
lib/mini_profiler/body_add_proxy.rb,
lib/mini_profiler/sql_timer_struct.rb,
lib/mini_profiler/page_timer_struct.rb,
lib/mini_profiler/profiling_methods.rb,
lib/mini_profiler/storage/file_store.rb,
lib/mini_profiler/client_timer_struct.rb,
lib/mini_profiler/storage/redis_store.rb,
lib/mini_profiler/request_timer_struct.rb,
lib/mini_profiler/storage/memory_store.rb,
lib/mini_profiler/storage/abstract_store.rb,
lib/patches/sql_patches.rb

Defined Under Namespace

Modules: ActiveRecordInstrumentation, ProfilingMethods Classes: AbstractStore, BodyAddProxy, ClientTimerStruct, Config, Context, FileStore, MemoryStore, PageTimerStruct, RedisStore, RequestTimerStruct, SqlTimerStruct, TimerStruct

Constant Summary collapse

VERSION =
'104'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ProfilingMethods

profile_method, record_sql, step, unprofile_method

Constructor Details

#initialize(app, config = nil) ⇒ MiniProfiler

options: :auto_inject - should script be automatically injected on every html page (not xhr)



98
99
100
101
102
103
104
105
106
# File 'lib/mini_profiler/profiler.rb', line 98

def initialize(app, config = nil)
  MiniProfiler.config.merge!(config)
  @config = MiniProfiler.config
  @app = app
  @config.base_url_path << "/" unless @config.base_url_path.end_with? "/"
  unless @config.storage_instance
    @storage = @config.storage_instance = @config.storage.new(@config.storage_options)
  end
end

Class Method Details

.authorize_requestObject



82
83
84
# File 'lib/mini_profiler/profiler.rb', line 82

def authorize_request
  Thread.current[:mp_authorized] = true
end

.configObject

So we can change the configuration if we want



37
38
39
# File 'lib/mini_profiler/profiler.rb', line 37

def config
  @config ||= Config.default
end

.create_current(env = {}, options = {}) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/mini_profiler/profiler.rb', line 74

def create_current(env={}, options={})
  # profiling the request
  self.current = Context.new
  self.current.inject_js = config.auto_inject && (!env['HTTP_X_REQUESTED_WITH'].eql? 'XMLHttpRequest')
  self.current.page_struct = PageTimerStruct.new(env)
  self.current.current_timer = current.page_struct['Root']
end

.currentObject



46
47
48
# File 'lib/mini_profiler/profiler.rb', line 46

def current
  Thread.current[:mini_profiler_private]
end

.current=(c) ⇒ Object



50
51
52
53
# File 'lib/mini_profiler/profiler.rb', line 50

def current=(c)
  # we use TLS cause we need access to this from sql blocks and code blocks that have no access to env
  Thread.current[:mini_profiler_private]= c
end

.deauthorize_requestObject



86
87
88
# File 'lib/mini_profiler/profiler.rb', line 86

def deauthorize_request
  Thread.current[:mp_authorized] = nil
end

.discard_resultsObject

discard existing results, don’t track this request



56
57
58
# File 'lib/mini_profiler/profiler.rb', line 56

def discard_results
  self.current.discard = true if current
end

.generate_idObject



28
29
30
# File 'lib/mini_profiler/profiler.rb', line 28

def generate_id
  rand(36**20).to_s(36)
end

.has_profiling_cookie?(env) ⇒ Boolean

user has the mini profiler cookie, only used when config.authorization_mode == :whitelist

Returns:

  • (Boolean)


61
62
63
# File 'lib/mini_profiler/profiler.rb', line 61

def has_profiling_cookie?(env)
  env['HTTP_COOKIE'] && env['HTTP_COOKIE'].include?("__profilin=stylin")
end

remove the mini profiler cookie, only used when config.authorization_mode == :whitelist



66
67
68
# File 'lib/mini_profiler/profiler.rb', line 66

def remove_profiling_cookie(headers)
  Rack::Utils.delete_cookie_header!(headers, '__profilin')
end

.request_authorized?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/mini_profiler/profiler.rb', line 90

def request_authorized?
  Thread.current[:mp_authorized]
end

.reset_configObject



32
33
34
# File 'lib/mini_profiler/profiler.rb', line 32

def reset_config
  @config = Config.default
end


70
71
72
# File 'lib/mini_profiler/profiler.rb', line 70

def set_profiling_cookie(headers)
  Rack::Utils.set_cookie_header!(headers, '__profilin', 'stylin')
end

.share_templateObject



41
42
43
44
# File 'lib/mini_profiler/profiler.rb', line 41

def share_template
  return @share_template unless @share_template.nil?
  @share_template = ::File.read(::File.expand_path("../html/share.html", ::File.dirname(__FILE__)))
end

Instance Method Details

#analyze(traces, page_struct) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/mini_profiler/profiler.rb', line 328

def analyze(traces, page_struct)
  headers = {'Content-Type' => 'text/plain'}
  body = "Collected: #{traces.count} stack traces. Duration(ms): #{page_struct.duration_ms}"

  seen = {}
  fulldump = ""
  traces.each do |trace|
    fulldump << "\n\n"
    distinct = {}
    trace.each do |frame|
      name = "#{frame.klass} #{frame.method}"
      unless distinct[name]
        distinct[name] = true
        seen[name] ||= 0
        seen[name] += 1
      end
      fulldump << name << "\n"
    end
  end

  body << "\n\nStack Trace Analysis\n"
  seen.to_a.sort{|x,y| y[1] <=> x[1]}.each do |name, count|
    if count > traces.count / 10
      body << "#{name} x #{count}\n"
    end
  end

  body << "\n\n\nRaw traces \n"
  body << fulldump

  [200, headers, [body]]
end

#call(env) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/mini_profiler/profiler.rb', line 173

def call(env)
  status = headers = body = nil
  path = env['PATH_INFO']

  skip_it = (@config.pre_authorize_cb && !@config.pre_authorize_cb.call(env)) ||
    (@config.skip_paths && @config.skip_paths.any?{ |p| path[0,p.length] == p}) ||
    env["QUERY_STRING"] =~ /pp=skip/

  has_profiling_cookie = MiniProfiler.has_profiling_cookie?(env)

  if skip_it || (@config.authorization_mode == :whitelist && !has_profiling_cookie)
    status,headers,body = @app.call(env)
    if !skip_it && @config.authorization_mode == :whitelist && !has_profiling_cookie && MiniProfiler.request_authorized?
      MiniProfiler.set_profiling_cookie(headers)
    end
    return [status,headers,body]
  end

  # handle all /mini-profiler requests here
  return serve_html(env) if env['PATH_INFO'].start_with? @config.base_url_path

  MiniProfiler.create_current(env, @config)
  MiniProfiler.deauthorize_request if @config.authorization_mode == :whitelist
  if env["QUERY_STRING"] =~ /pp=no-backtrace/
    current.skip_backtrace = true
  elsif env["QUERY_STRING"] =~ /pp=full-backtrace/
    current.full_backtrace = true
  end

  done_sampling = false
  quit_sampler = false
  backtraces = nil
  missing_stacktrace = false
  if env["QUERY_STRING"] =~ /pp=sample/
    backtraces = []
    t = Thread.current
    Thread.new {
      begin
        require 'stacktrace' rescue nil
        if !t.respond_to? :stacktrace
          missing_stacktrace = true
          quit_sampler = true
          return
        end
        i = 10000 # for sanity never grab more than 10k samples
        while i > 0
          break if done_sampling
          i -= 1
          backtraces << t.stacktrace
          sleep 0.001
        end
    ensure
      quit_sampler = true
    end
    }
  end

  status, headers, body = nil
  start = Time.now
  begin
    status,headers,body = @app.call(env)
  ensure
    if backtraces
      done_sampling = true
      sleep 0.001 until quit_sampler
    end
  end

  skip_it = current.discard
  if (config.authorization_mode == :whitelist && !MiniProfiler.request_authorized?)
    MiniProfiler.remove_profiling_cookie(headers)
    skip_it = true
  end

  return [status,headers,body] if skip_it

  # we must do this here, otherwise current[:discard] is not being properly treated
  if env["QUERY_STRING"] =~ /pp=env/
    body.close if body.respond_to? :close
    return dump_env env
  end

  if env["QUERY_STRING"] =~ /pp=help/
    body.close if body.respond_to? :close
    return help
  end

  page_struct = current.page_struct
  page_struct['Root'].record_time((Time.now - start) * 1000)

  if backtraces
    body.close if body.respond_to? :close
    return help(:stacktrace) if missing_stacktrace
    return analyze(backtraces, page_struct)
  end


  # no matter what it is, it should be unviewed, otherwise we will miss POST
  @storage.set_unviewed(user(env), page_struct['Id'])
  @storage.save(page_struct)

  # inject headers, script
  if status == 200

    # inject header
    if headers.is_a? Hash
      headers['X-MiniProfiler-Ids'] = ids_json(env)
    end

    # inject script
    if current.inject_js \
      && headers.has_key?('Content-Type') \
      && !headers['Content-Type'].match(/text\/html/).nil? then
      body = MiniProfiler::BodyAddProxy.new(body, self.get_profile_script(env))
    end
  end

  # mini profiler is meddling with stuff, we can not cache cause we will get incorrect data
  # Rack::ETag has already inserted some nonesense in the chain
  headers.delete('ETag')
  headers.delete('Date')
  headers['Cache-Control'] = 'must-revalidate, private, max-age=0'
  [status, headers, body]
ensure
  # Make sure this always happens
  current = nil
end

#cancel_auto_inject(env) ⇒ Object

cancels automatic injection of profile script for the current page



398
399
400
# File 'lib/mini_profiler/profiler.rb', line 398

def cancel_auto_inject(env)
  current.inject_js = false
end

#configObject



168
169
170
# File 'lib/mini_profiler/profiler.rb', line 168

def config
  @config
end

#currentObject



159
160
161
# File 'lib/mini_profiler/profiler.rb', line 159

def current
  MiniProfiler.current
end

#current=(c) ⇒ Object



163
164
165
# File 'lib/mini_profiler/profiler.rb', line 163

def current=(c)
  MiniProfiler.current=c
end

#dump_env(env) ⇒ Object



301
302
303
304
305
306
307
308
# File 'lib/mini_profiler/profiler.rb', line 301

def dump_env(env)
  headers = {'Content-Type' => 'text/plain'}
  body = ""
  env.each do |k,v|
    body << "#{k}: #{v}\n"
  end
  [200, headers, [body]]
end

#get_profile_script(env) ⇒ Object

get_profile_script returns script to be injected inside current html page By default, profile_script is appended to the end of all html requests automatically. Calling get_profile_script cancels automatic append for the current page Use it when:

  • you have disabled auto append behaviour throught :auto_inject => false flag

  • you do not want script to be automatically appended for the current page. You can also call cancel_auto_inject



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/mini_profiler/profiler.rb', line 372

def get_profile_script(env)
  ids = ids_json(env)
  path = @config.base_url_path
  version = MiniProfiler::VERSION
  position = @config.position
  showTrivial = false
  showChildren = false
  maxTracesToShow = 10
  showControls = false
  currentId = current.page_struct["Id"]
  authorized = true
  useExistingjQuery = @config.use_existing_jquery
  # TODO : cache this snippet
  script = IO.read(::File.expand_path('../html/profile_handler.js', ::File.dirname(__FILE__)))
  # replace the variables
  [:ids, :path, :version, :position, :showTrivial, :showChildren, :maxTracesToShow, :showControls, :currentId, :authorized, :useExistingjQuery].each do |v|
    regex = Regexp.new("\\{#{v.to_s}\\}")
    script.gsub!(regex, eval(v.to_s).to_s)
  end
  # replace the '{{' and '}}''
  script.gsub!(/\{\{/, '{').gsub!(/\}\}/, '}')
  current.inject_js = false
  script
end

#help(category = nil) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/mini_profiler/profiler.rb', line 310

def help(category = nil)
  headers = {'Content-Type' => 'text/plain'}
  body = "Append the following to your query string:

  pp=help : display this screen
  pp=env : display the rack environment
  pp=skip : skip mini profiler for this request
  pp=no-backtrace : don't collect stack traces from all the SQL executed
  pp=full-backtrace : enable full backtrace for SQL executed
  pp=sample : sample stack traces and return a report isolating heavy usage (requires the stacktrace gem)
  "
  if (category == :stacktrace)
    body = "pp=stacktrace requires the stacktrace gem - add gem 'stacktrace' to your Gemfile"
  end

  [200, headers, [body]]
end

#ids_json(env) ⇒ Object



361
362
363
364
# File 'lib/mini_profiler/profiler.rb', line 361

def ids_json(env)
  ids = [current.page_struct["Id"]] + (@storage.get_unviewed_ids(user(env)) || [])
  ::JSON.generate(ids.uniq)
end

#serve_html(env) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/mini_profiler/profiler.rb', line 147

def serve_html(env)
  file_name = env['PATH_INFO'][(@config.base_url_path.length)..1000]
  return serve_results(env) if file_name.eql?('results')
  full_path = ::File.expand_path("../html/#{file_name}", ::File.dirname(__FILE__))
  return [404, {}, ["Not found"]] unless ::File.exists? full_path
  f = Rack::File.new nil
  f.path = full_path
  f.cache_control = "max-age:86400"
  f.serving env
end

#serve_results(env) ⇒ Object



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
140
141
142
143
144
145
# File 'lib/mini_profiler/profiler.rb', line 112

def serve_results(env)
  request = Rack::Request.new(env)
  id = request['id']
  page_struct = @storage.load(id)
  unless page_struct
    @storage.set_viewed(user(env), id)
    return [404, {}, ["Request not found: #{request['id']} - user #{user(env)}"]]
  end
  unless page_struct['HasUserViewed']
    page_struct['ClientTimings'].init_from_form_data(env, page_struct)
    page_struct['HasUserViewed'] = true
    @storage.save(page_struct)
    @storage.set_viewed(user(env), id)
  end

  result_json = page_struct.to_json
  # If we're an XMLHttpRequest, serve up the contents as JSON
  if request.xhr?
    [200, { 'Content-Type' => 'application/json'}, [result_json]]
  else

    # Otherwise give the HTML back
    html = MiniProfiler.share_template.dup
    html.gsub!(/\{path\}/, @config.base_url_path)
    html.gsub!(/\{version\}/, MiniProfiler::VERSION)
    html.gsub!(/\{json\}/, result_json)
    html.gsub!(/\{includes\}/, get_profile_script(env))
    html.gsub!(/\{name\}/, page_struct['Name'])
    html.gsub!(/\{duration\}/, page_struct.duration_ms.round(1).to_s)

    [200, {'Content-Type' => 'text/html'}, [html]]
  end

end

#user(env) ⇒ Object



108
109
110
# File 'lib/mini_profiler/profiler.rb', line 108

def user(env)
  @config.user_provider.call(env)
end