Sha256: b1e4f53ef50788f07c86504631b87051a2d11cce2d0da331bf8f46e6422f733a

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

# encoding: utf-8

module OneApm
  module Agent
    module Instrumentation
      module Sinatra
        module TransactionNamer
          extend self

          def transaction_name_for_route(env, request)
            name = route_for_sinatra(env)
            name = route_name_for_padrino(request) if name.nil?

            transaction_name(name, request) unless name.nil?
          end

          def initial_transaction_name(request)
            transaction_name(OneApm::Transaction::OA_UNKNOWN_METRIC, request)
          end

          def transaction_name(route_text, request)
            verb = http_verb(request)

            route_text = route_text.source if route_text.is_a?(Regexp)
            name = route_text.gsub(%r{^[/^\\A]*(.*?)[/\$\?\\z]*$}, '\1')
            name = "#{verb} #{name}" unless verb.nil?
            name
          rescue => e
            OneApm::Manager.logger.debug("#{e.class} : #{e.message} - Error encountered trying to identify Sinatra transaction name")
            OneApm::Transaction::OA_UNKNOWN_METRIC
          end

          def http_verb(request)
            request.request_method if request.respond_to?(:request_method)
          end

          # For bare Sinatra, our override on process_route captures the last
          # route into the environment for us to use later on
          def route_for_sinatra(env)
            env["oneapm.last_route"]
          end

          # For Padrino, the request object has a copy of the matched route
          # on it when we go to evaluating, so we can just retrieve that
          def route_name_for_padrino(request)
            request.route_obj.original_path
          rescue
            nil
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/inst/framework/sinatra/transaction_namer.rb
oneapm_rpm-1.4.1 lib/one_apm/inst/framework/sinatra/transaction_namer.rb
oneapm_rpm-1.4.0 lib/one_apm/inst/framework/sinatra/transaction_namer.rb
oneapm_rpm-1.3.7 lib/one_apm/inst/framework/sinatra/transaction_namer.rb
oneapm_rpm-1.3.7.rc1 lib/one_apm/inst/framework/sinatra/transaction_namer.rb