Class: Appsignal::Grape::Middleware

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

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/appsignal/integrations/grape.rb', line 4

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

#call_with_appsignal_monitoring(env) ⇒ Object



12
13
14
15
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
42
43
44
45
46
47
48
# File 'lib/appsignal/integrations/grape.rb', line 12

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
    request_method = request.request_method
    path = request.path # Path without namespaces
    endpoint = env["api.endpoint"]

    if endpoint && endpoint.options
      options = endpoint.options
      request_method = options[:method].first
      klass = options[:for]
      namespace = endpoint.namespace
      namespace = "" if namespace == "/"

      path = options[:path].first.to_s
      path = "/#{path}" if path[0] != "/"
      path = "#{namespace}#{path}"

      transaction.set_action("#{request_method}::#{klass}##{path}")
    end

    transaction.set_http_or_background_queue_start
    transaction.("path", path)
    transaction.("method", request_method)
    Appsignal::Transaction.complete_current!
  end
end