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.29.6 lib/logjam_agent/middleware.rb
logjam_agent-0.29.5 lib/logjam_agent/middleware.rb
logjam_agent-0.29.4 lib/logjam_agent/middleware.rb
logjam_agent-0.29.3 lib/logjam_agent/middleware.rb
logjam_agent-0.29.2 lib/logjam_agent/middleware.rb
logjam_agent-0.29.1 lib/logjam_agent/middleware.rb
logjam_agent-0.29.0 lib/logjam_agent/middleware.rb
logjam_agent-0.28.0 lib/logjam_agent/middleware.rb
logjam_agent-0.27.0 lib/logjam_agent/middleware.rb
logjam_agent-0.26.6 lib/logjam_agent/middleware.rb
logjam_agent-0.26.5 lib/logjam_agent/middleware.rb
logjam_agent-0.26.4 lib/logjam_agent/middleware.rb
logjam_agent-0.26.3 lib/logjam_agent/middleware.rb
logjam_agent-0.26.2 lib/logjam_agent/middleware.rb
logjam_agent-0.26.1 lib/logjam_agent/middleware.rb
logjam_agent-0.26.0 lib/logjam_agent/middleware.rb
logjam_agent-0.25.3 lib/logjam_agent/middleware.rb
logjam_agent-0.25.2 lib/logjam_agent/middleware.rb
logjam_agent-0.25.1 lib/logjam_agent/middleware.rb
logjam_agent-0.25.0 lib/logjam_agent/middleware.rb