Class: Appsignal::Grape::Middleware

Inherits:
Grape::Middleware::Base
  • Object
show all
Defined in:
lib/appsignal/integrations/grape.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.


4
5
6
# File 'lib/appsignal/integrations/grape.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


8
9
10
11
12
13
14
# File 'lib/appsignal/integrations/grape.rb', line 8

def call(env)
  if Appsignal.active?
    call_with_appsignal_monitoring(env)
  else
    @app.call(env)
  end
end

#call_with_appsignal_monitoring(env) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/appsignal/integrations/grape.rb', line 16

def call_with_appsignal_monitoring(env)
  request      = ::Rack::Request.new(env)
  transaction  = Appsignal::Transaction.create(
    SecureRandom.uuid,
    Appsignal::Transaction::HTTP_REQUEST,
    request
  )
  begin
    @app.call(env)
  rescue => error
    transaction.set_error(error)
    raise error
  ensure
    api_endpoint = env['api.endpoint']
    if api_endpoint && options = api_endpoint.options
      method = options[:method].first
      klass  = options[:for]
      action = options[:path].first
      transaction.set_action("#{method}::#{klass}##{action}")
    end
    transaction.set_http_or_background_queue_start
    transaction.('path', request.path)
    transaction.('method', env['REQUEST_METHOD'])
    Appsignal::Transaction.complete_current!
  end
end