Sha256: 3f8437fddd1f26d1c11c8ce4f3179005ea441a710906762425f9e0f4ccd83e41

Contents?: true

Size: 1.66 KB

Versions: 44

Compression:

Stored size: 1.66 KB

Contents

module LogjamAgent
  class Middleware
    def initialize(app, options={})
      @app = app
      @options = options
    end

    def call(env)
      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>"]]
    ensure
      headers = result[1]
      headers["X-Logjam-Request-Id"] = request.id
      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

    private

    def strip_encoding_from_etag(env)
      # In some versions, Apache is appending the content encoding,
      # like gzip to the ETag-Response-Header, which will cause the
      # Rack::ConditionalGet middleware to never match.
      if env["HTTP_IF_NONE_MATCH"] =~ /\A(.*)-\w+(\")\z/
        env["HTTP_IF_NONE_MATCH"] = $1 + $2
      end
    end

    def start_request(env)
      app_name      = env["logjam_agent.application_name"] || LogjamAgent.application_name
      env_name      = env["logjam_agent.environment_name"] || LogjamAgent.environment_name
      caller_id     = env["HTTP_X_LOGJAM_CALLER_ID"] || ""
      caller_action = env["HTTP_X_LOGJAM_ACTION"] || ""
      LogjamAgent.start_request(app_name, env_name, :caller_id => caller_id, :caller_action => caller_action)
    end

    def finish_request(env)
      LogjamAgent.finish_request(env["time_bandits.metrics"])
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
logjam_agent-0.24.10 lib/logjam_agent/middleware.rb
logjam_agent-0.24.9 lib/logjam_agent/middleware.rb
logjam_agent-0.24.8 lib/logjam_agent/middleware.rb
logjam_agent-0.24.7 lib/logjam_agent/middleware.rb
logjam_agent-0.24.6 lib/logjam_agent/middleware.rb
logjam_agent-0.24.5 lib/logjam_agent/middleware.rb
logjam_agent-0.24.4 lib/logjam_agent/middleware.rb
logjam_agent-0.24.3 lib/logjam_agent/middleware.rb
logjam_agent-0.24.2 lib/logjam_agent/middleware.rb
logjam_agent-0.24.1 lib/logjam_agent/middleware.rb
logjam_agent-0.24.0 lib/logjam_agent/middleware.rb
logjam_agent-0.23.1 lib/logjam_agent/middleware.rb
logjam_agent-0.23.0 lib/logjam_agent/middleware.rb
logjam_agent-0.22.1 lib/logjam_agent/middleware.rb
logjam_agent-0.22.0 lib/logjam_agent/middleware.rb
logjam_agent-0.21.1 lib/logjam_agent/middleware.rb
logjam_agent-0.21.0 lib/logjam_agent/middleware.rb
logjam_agent-0.20.0 lib/logjam_agent/middleware.rb
logjam_agent-0.19.6 lib/logjam_agent/middleware.rb
logjam_agent-0.19.5 lib/logjam_agent/middleware.rb