class ElasticAPM::Grape::Middleware

Public Class Methods

new(app) click to toggle source
# File lib/elastic_apm/grape/middleware.rb, line 7
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/elastic_apm/grape/middleware.rb, line 12
def call(env)
  begin
    transaction_name = env['grape.routing_args'][:route_info].pattern.origin

    transaction = ElasticAPM.transaction transaction_name, 'app',
      context: ElasticAPM.build_context(env)

    resp = @app.call env

    transaction.submit(resp[0], headers: resp[1]) if transaction
  rescue InternalError
    raise # Don't report ElasticAPM errors
  rescue ::Exception => e
    ElasticAPM.report(e, handled: false)
    transaction.submit(500) if transaction
    raise
  ensure
    transaction.release if transaction
  end

  resp
end