Class: LogjamAgent::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/logjam_agent/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, framework = :rails) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
6
7
8
9
10
# File 'lib/logjam_agent/middleware.rb', line 3

def initialize(app, framework = :rails)
  @app = app
  @framework = framework
  unless %i{rails sinatra}.include?(framework)
    raise ArgumentError.new("Invalid logjam_agent framework: #{framework}. Only :rails and :sinatra are valid!")
  end
  @reraise = defined?(Rails::Railtie) && Rails.env.test?
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  env["logjam_agent.framework"] = @framework
  strip_encoding_from_etag(env)
  request = start_request(env)
  result = @app.call(env)
  result[1] ||= {}
  result
rescue Exception
  result = [500, {'Content-Type' => 'text/html'}, ["<html><body><h1>500 Internal Server Error</h1></body></html>"]]
  raise if @reraise
ensure
  headers = result[1]
  headers["X-Logjam-Request-Id"] = request.id
  if env["sinatra.static_file"]
    request.fields[:action] = "Sinatra#static_file"
  end
  unless (request_action = request.fields[:action]).blank?
    headers["X-Logjam-Request-Action"] = request_action
  end
  unless (caller_id = request.fields[:caller_id]).blank?
    headers["X-Logjam-Caller-Id"] = caller_id
  end
  finish_request(env)
end