Class: Appsignal::Rack::SinatraInstrumentation

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/rack/sinatra_instrumentation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ SinatraInstrumentation

Returns a new instance of SinatraInstrumentation.


8
9
10
11
12
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 8

def initialize(app, options = {})
  Appsignal.logger.debug 'Initializing Appsignal::Rack::SinatraInstrumentation'
  @app, @options = app, options
  @raise_errors_on = @app.settings.raise_errors
end

Instance Attribute Details

#raise_errors_onObject (readonly)

Returns the value of attribute raise_errors_on


6
7
8
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 6

def raise_errors_on
  @raise_errors_on
end

Instance Method Details

#call(env) ⇒ Object


14
15
16
17
18
19
20
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 14

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

#call_with_appsignal_monitoring(env) ⇒ Object


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
49
50
51
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 22

def call_with_appsignal_monitoring(env)
  if @options[:params_method]
    env[:params_method] = @options[:params_method]
  end
  request = @options.fetch(:request_class, Sinatra::Request).new(env)
  transaction = Appsignal::Transaction.create(
    SecureRandom.uuid,
    Appsignal::Transaction::HTTP_REQUEST,
    request
  )
  begin
    ActiveSupport::Notifications.instrument('process_action.sinatra') do
      @app.call(env)
    end
  rescue => error
    transaction.set_error(error)
    raise error
  ensure
    # If raise_error is off versions of Sinatra don't raise errors, but store
    # them in the sinatra.error env var.
    if !@raise_errors_on && env['sinatra.error'] && !env['sinatra.skip_appsignal_error']
      transaction.set_error(env['sinatra.error'])
    end
    transaction.set_action(env['sinatra.route'])
    transaction.('path', request.path)
    transaction.('method', request.request_method)
    transaction.set_http_or_background_queue_start
    Appsignal::Transaction.complete_current!
  end
end