module NewRelic::Agent::Instrumentation::Grape::Instrumentation

Constants

API_ENDPOINT
API_VERSION
FORMAT_REGEX
INSTRUMENTATION_NAME
MIN_VERSION
PIPE_STRING
VERSION_REGEX

Public Instance Methods

capture_params(endpoint) click to toggle source
# File lib/new_relic/agent/instrumentation/grape/instrumentation.rb, line 94
def capture_params(endpoint)
  txn = ::NewRelic::Agent::Transaction.tl_current
  env = endpoint.request.env
  params = ::NewRelic::Agent::ParameterFiltering::apply_filters(env, endpoint.params)
  params.delete('route_info')
  txn.filtered_params = params
  txn.merge_request_parameters(params)
end
capture_transaction(env, context) click to toggle source
# File lib/new_relic/agent/instrumentation/grape/instrumentation.rb, line 20
def capture_transaction(env, context)
  begin
    endpoint = env[API_ENDPOINT]
    version = env[API_VERSION]

    api_class = (context.class.respond_to?(:base) && context.class.base) || context.class
    handle_transaction(endpoint, api_class.name, version)
  rescue => e
    ::NewRelic::Agent.logger.warn('Error in Grape instrumentation', e)
  end
end
handle_transaction(endpoint, class_name, version) click to toggle source
# File lib/new_relic/agent/instrumentation/grape/instrumentation.rb, line 48
def handle_transaction(endpoint, class_name, version)
  return unless endpoint && route = endpoint.route

  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

  name_transaction(route, class_name, version)
  capture_params(endpoint)
end
instrumented_class() click to toggle source

Since 1.2.0, the class ‘Grape::API` no longer refers to an API instance, rather, what used to be `Grape::API` is `Grape::API::Instance` github.com/ruby-grape/grape/blob/c20a73ac1e3f3ba1082005ed61bf69452373ba87/UPGRADING.md#upgrading-to–120

# File lib/new_relic/agent/instrumentation/grape/instrumentation.rb, line 16
def instrumented_class
  defined?(::Grape::API::Instance) ? ::Grape::API::Instance : ::Grape::API
end
name_for_transaction(route, class_name, version) click to toggle source
# File lib/new_relic/agent/instrumentation/grape/instrumentation.rb, line 65
def name_for_transaction(route, class_name, version)
  action_name = route.path.sub(FORMAT_REGEX, NewRelic::EMPTY_STR)
  method_name = route.request_method
  version ||= route.version

  # defaulting does not set rack.env['api.version'] and route.version may return Array
  #
  version = version.join(PIPE_STRING) if Array === version

  if version
    action_name = action_name.sub(VERSION_REGEX, NewRelic::EMPTY_STR)
    "#{class_name}-#{version}#{action_name} (#{method_name})"
  else
    "#{class_name}#{action_name} (#{method_name})"
  end
end
name_for_transaction_deprecated(route, class_name, version) click to toggle source
# File lib/new_relic/agent/instrumentation/grape/instrumentation.rb, line 82
def name_for_transaction_deprecated(route, class_name, version)
  action_name = route.route_path.sub(FORMAT_REGEX, NewRelic::EMPTY_STR)
  method_name = route.route_method
  version ||= route.route_version
  if version
    action_name = action_name.sub(VERSION_REGEX, NewRelic::EMPTY_STR)
    "#{class_name}-#{version}#{action_name} (#{method_name})"
  else
    "#{class_name}#{action_name} (#{method_name})"
  end
end
name_transaction(route, class_name, version) click to toggle source
# File lib/new_relic/agent/instrumentation/grape/instrumentation.rb, line 57
def name_transaction(route, class_name, version)
  txn_name = name_for_transaction(route, class_name, version)
  segment_name = "Middleware/Grape/#{class_name}/call"
  NewRelic::Agent::Transaction.set_default_transaction_name(txn_name, :grape)
  txn = NewRelic::Agent::Transaction.tl_current
  txn.segments.last.name = segment_name
end
prepare!() click to toggle source
# File lib/new_relic/agent/instrumentation/grape/instrumentation.rb, line 32
def prepare!
  if defined?(::Grape::VERSION) && Gem::Version.new(::Grape::VERSION) >= Gem::Version.new('0.16.0')
    send(:remove_method, :name_for_transaction_deprecated)
  else
    send(:remove_method, :name_for_transaction)
    send(:alias_method, :name_for_transaction, :name_for_transaction_deprecated)
  end
end