Class: OpticsAgent::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/optics-agent/rack-middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



6
7
8
# File 'lib/optics-agent/rack-middleware.rb', line 6

def initialize(app, options={})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/optics-agent/rack-middleware.rb', line 10

def call(env)
  start_time = Time.now

  # XXX: figure out a way to pass this in here
  agent = OpticsAgent::Agent.instance
  query = OpticsAgent::Reporting::Query.new

  # Attach so resolver middleware can access
  env[:optics_agent] = {
    agent: agent,
    query: query
  }
  env[:optics_agent].define_singleton_method(:with_document) do |document|
    self[:query].document = document
    self
  end

  result = @app.call(env)

  # XXX: this approach means if the user forgets to call with_document
  # we just never log queries. Can we detect if the request is a graphql one?
  if (query.document)
    agent.add_query(query, env, start_time, Time.now)
  end

  result
end