Sha256: d9c65d7cf81bff748323c60dfef2224f01e2f66d2bc253fa3e5db1243ae9f980

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

module NewRelic
  module Agent
    module GrapeInstrumentation
      API_ENDPOINT = 'api.endpoint'.freeze
      FORMAT       = '(.:format)'.freeze
      EMPTY_STRING = ''.freeze
      MIN_VERSION  = ::NewRelic::VersionNumber.new("0.2.0")
    end
  end
end

DependencyDetection.defer do
  named :grape

  depends_on do
    defined?(::Grape::VERSION) &&
      ::NewRelic::VersionNumber.new(::Grape::VERSION) >= ::NewRelic::Agent::GrapeInstrumentation::MIN_VERSION
  end

  depends_on do
    false
  end

  executes do
    NewRelic::Agent.logger.info 'Installing Grape instrumentation'
    instrument_call
  end

  def instrument_call
    ::Grape::API.class_eval do
      def call_with_new_relic(env)
        begin
          response = call_without_new_relic(env)
        ensure
          # We don't want an error in our transaction naming to kill the request.
          begin
            endpoint = env[::NewRelic::Agent::GrapeInstrumentation::API_ENDPOINT]

            if endpoint
              route_obj = endpoint.route
              if route_obj
                action_name = route_obj.route_path.sub(::NewRelic::Agent::GrapeInstrumentation::FORMAT,
                                                        ::NewRelic::Agent::GrapeInstrumentation::EMPTY_STRING)
                method_name = route_obj.route_method

                txn_name = "#{self.class.name}#{action_name} (#{method_name})"
                ::NewRelic::Agent.set_transaction_name(txn_name)
              end
            end
          rescue => e
            ::NewRelic::Agent.logger.warn("Error in Grape transaction naming", e)
          end
        end

        response
      end

      alias_method :call_without_new_relic, :call
      alias_method :call, :call_with_new_relic
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
newrelic_rpm-3.9.8.273 lib/new_relic/agent/instrumentation/grape.rb