Class: Rack::SpeedTracer::Context
- Inherits:
-
Object
- Object
- Rack::SpeedTracer::Context
- Defined in:
- lib/rack/speedtracer/context.rb
Constant Summary collapse
- TRACER_PATH =
/^\/speedtracer/.freeze
- CONTENT_TYPE =
'application/json;charset=UTF-8'.freeze
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}, &blk) ⇒ Context
constructor
A new instance of Context.
Constructor Details
#initialize(app, options = {}, &blk) ⇒ Context
Returns a new instance of Context.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rack/speedtracer/context.rb', line 10 def initialize(app, = {}, &blk) @app = app @uuid = UUID.new # TODO: storage strategy... # initialize_options with options # yield self if block_given? @db = {} end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
8 9 10 |
# File 'lib/rack/speedtracer/context.rb', line 8 def db @db end |
Instance Method Details
#call(env) ⇒ Object
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rack/speedtracer/context.rb', line 21 def call(env) if env['PATH_INFO'].match(TRACER_PATH) 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'] && @db[qs['id']] resp.write @db[qs['id']] else # Invalid request or missing request trace id resp.status = 404 end else # SpeedTracer should only issue GET & HEAD requests resp.status = 400 end return resp.finish end env['st.id'] = @uuid.generate env['st.tracer'] = Tracer.new(env['st.id'], env['REQUEST_METHOD'], env['REQUEST_URI']) @status, @headers, @response = @app.call(env) @db[env['st.id']] = env['st.tracer'].finish # set the TraceUrl header to notify SpeedTracer that # serverside meta-data is available for this request @headers['X-TraceUrl'] = '/speedtracer?id=' + env['st.id'] [@status, @headers, @response] end |