Class: Rack::Insight::SpeedTracer::TraceApp

Inherits:
Object
  • Object
show all
Includes:
Database::RequestDataClient
Defined in:
lib/rack/insight/panels/speedtracer_panel/trace-app.rb

Constant Summary collapse

CONTENT_TYPE =
'application/json;charset=UTF-8'.freeze
FourOhFour =
[404, {"Content-Type" => "text/html"}, "App tracker doesn't know that path or id"].freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Database::RequestDataClient

#count, #key_sql_template, #retrieve, #store, #table_length, #table_setup

Constructor Details

#initializeTraceApp

Returns a new instance of TraceApp.



15
16
17
18
# File 'lib/rack/insight/panels/speedtracer_panel/trace-app.rb', line 15

def initialize
  table_setup("speedtracer", "uuid")
  key_sql_template = "'%s'"
end

Class Attribute Details

.has_tableObject

Returns the value of attribute has_table.



11
12
13
# File 'lib/rack/insight/panels/speedtracer_panel/trace-app.rb', line 11

def has_table
  @has_table
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rack/insight/panels/speedtracer_panel/trace-app.rb', line 20

def call(env)
  resp = Rack::Response.new('', 200)
  resp['Content-Type'] = CONTENT_TYPE

  case env['REQUEST_METHOD']
  when 'HEAD' then
    # SpeedTracer dispatches HEAD requests to verify the
    # tracer endpoint when it detects the X-TraceUrl
    # header for the first time. After the initial load
    # the verification is cached by the extension.
    #
    # By default, we'll return 200.

  when 'GET' then
    # GET requests for specific trace are generated by
    # the extension when the user expands the network
    # resource tab. Hence, server-side tracer data is
    # request on-demand, and we need to store it for
    # some time.
    #

    qs = Rack::Utils.parse_query(env['QUERY_STRING'])
    if qs['id'] && (trace = @table.retrieve("uuid = '#{qs['id']}'"))
      resp.write trace.to_json
    else
      # Invalid request or missing request trace id
      return FourOhFour
    end
  else
    # SpeedTracer should only issue GET & HEAD requests
    resp.status = 400
  end

  return resp.finish
end