Sha256: 4654879fbf83796653f47065b6e1e3ba070a77f75fa7af4db191f431a3488038

Contents?: true

Size: 1.53 KB

Versions: 99

Compression:

Stored size: 1.53 KB

Contents

require 'rack'

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

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

      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
          # In production newer versions of Sinatra don't raise errors, but store
          # them in the sinatra.error env var.
          transaction.set_error(env['sinatra.error']) if env['sinatra.error']
          transaction.set_action(env['sinatra.route'])
          transaction.set_metadata('path', request.path)
          transaction.set_metadata('method', request.request_method)
          transaction.set_http_or_background_queue_start
          Appsignal::Transaction.complete_current!
        end
      end
    end
  end
end

Version data entries

99 entries across 99 versions & 1 rubygems

Version Path
appsignal-1.1.5.beta.1 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.4 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.4.beta.3 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.4.beta.2 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.4.beta.1 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.3 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.3.beta.1 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.2 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.1 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.1.beta.1 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.0.beta.13 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.0.beta.12 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.0.7 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.0.6 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.0.beta.11 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.0.6.beta.4 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.0.beta.10 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.0.6.beta.3 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.1.0.beta.9 lib/appsignal/rack/sinatra_instrumentation.rb
appsignal-1.0.6.beta.2 lib/appsignal/rack/sinatra_instrumentation.rb