Class: Appsignal::Rack::SinatraBaseInstrumentation Private

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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SinatraBaseInstrumentation.



34
35
36
37
38
39
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 34

def initialize(app, options = {})
  Appsignal.logger.debug "Initializing Appsignal::Rack::SinatraBaseInstrumentation"
  @app = app
  @options = options
  @raise_errors_on = raise_errors?(@app)
end

Instance Attribute Details

#raise_errors_onObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 32

def raise_errors_on
  @raise_errors_on
end

Instance Method Details

#action_name(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
85
86
87
88
89
90
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 81

def action_name(env)
  return unless env["sinatra.route"]

  if env["SCRIPT_NAME"]
    method, route = env["sinatra.route"].split(" ")
    "#{method} #{env["SCRIPT_NAME"]}#{route}"
  else
    env["sinatra.route"]
  end
end

#call(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
44
45
46
47
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 41

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

#call_with_appsignal_monitoring(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/appsignal/rack/sinatra_instrumentation.rb', line 49

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,
    :force => @options.include?(:force) && @options[:force]
  )
  begin
    Appsignal.instrument("process_action.sinatra") do
      @app.call(env)
    end
  rescue Exception => error # rubocop:disable Lint/RescueException
    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_if_nil(action_name(env))
    transaction.("path", request.path)
    transaction.("method", request.request_method)
    transaction.set_http_or_background_queue_start
    Appsignal::Transaction.complete_current!
  end
end