class ElasticAPM::Middleware

@api private

def build_transaction(env)

def build_transaction(env)
  ElasticAPM.transaction 'Rack', 'request',
    context: ElasticAPM.build_context(env)
end

def call(env)

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
def call(env)
  begin
    if running? && !path_ignored?(env)
      transaction = build_transaction(env)
    end
    resp = @app.call env
    status, headers, body = resp
    submit_transaction(transaction, status, headers, body) if transaction
  rescue InternalError
    raise # Don't report ElasticAPM errors
  rescue ::Exception => e
    ElasticAPM.report(e, handled: false)
    transaction.submit('HTTP 5xx', status: 500) if transaction
    raise
  ensure
    transaction.release if transaction
  end
  resp
end

def config

def config
  ElasticAPM.agent.config
end

def initialize(app)

def initialize(app)
  @app = app
end

def path_ignored?(env)

def path_ignored?(env)
  config.ignore_url_patterns.any? do |r|
    env['PATH_INFO'].match r
  end
end

def running?

def running?
  ElasticAPM.running?
end

def submit_transaction(transaction, status, headers, _body)

def submit_transaction(transaction, status, headers, _body)
  result = "HTTP #{status.to_s[0]}xx"
  transaction.submit(result, status: status, headers: headers)
end