Module: NewRelic::Agent::Instrumentation::Grape::Instrumentation

Extended by:
Instrumentation
Included in:
Instrumentation
Defined in:
lib/new_relic/agent/instrumentation/grape/instrumentation.rb

Constant Summary collapse

API_ENDPOINT =
'api.endpoint'.freeze
API_VERSION =
'api.version'.freeze
FORMAT_REGEX =

either :format (< 0.12.0) or .ext (>= 0.12.0)

/\(\/?\.[\:\w]*\)/.freeze
VERSION_REGEX =
/:version(\/|$)/.freeze
MIN_VERSION =
Gem::Version.new('0.2.0')
PIPE_STRING =
'|'.freeze

Instance Method Summary collapse

Instance Method Details

#capture_params(endpoint) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/new_relic/agent/instrumentation/grape/instrumentation.rb', line 90

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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/new_relic/agent/instrumentation/grape/instrumentation.rb', line 18

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) ⇒ Object



46
47
48
49
50
51
# File 'lib/new_relic/agent/instrumentation/grape/instrumentation.rb', line 46

def handle_transaction(endpoint, class_name, version)
  return unless endpoint && route = endpoint.route

  name_transaction(route, class_name, version)
  capture_params(endpoint)
end

#instrumented_classObject

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



14
15
16
# File 'lib/new_relic/agent/instrumentation/grape/instrumentation.rb', line 14

def instrumented_class
  defined?(::Grape::API::Instance) ? ::Grape::API::Instance : ::Grape::API
end

#name_for_transaction(route, class_name, version) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/new_relic/agent/instrumentation/grape/instrumentation.rb', line 61

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) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/new_relic/agent/instrumentation/grape/instrumentation.rb', line 78

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) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/new_relic/agent/instrumentation/grape/instrumentation.rb', line 53

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!Object



30
31
32
33
34
35
36
37
# File 'lib/new_relic/agent/instrumentation/grape/instrumentation.rb', line 30

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