api_new_relic_instrumenter.rb
959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class ProposalsDiscussionPlugin::ApiNewRelicInstrumenter < Grape::Middleware::Base
include NewRelic::Agent::Instrumentation::ControllerInstrumentation
def call_with_newrelic(&block)
trace_options = {
:category => :rack,
:path => "#{route_path}\##{route_method}",
:request => Grape::Request.new(@env)
}
perform_action_with_newrelic_trace(trace_options) do
result = yield
MetricFrame.abort_transaction! if result.first == 404 # ignore cascaded calls
result
end
end
def call(env)
@env = env
if ENV['NEW_RELIC_ID']
call_with_newrelic do
super
end
else
super
end
end
def env
@env
end
def route
env['api.endpoint'].routes.first
end
def route_method
route.route_method.downcase
end
def route_path
path = route.route_path.gsub(/^.+:version\/|^\/|:|\(.+\)/, '').tr('/', '-')
"api.#{route.route_version}.#{path}"
end
end