newrelic_rpm 2.9.9 → 2.10.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of newrelic_rpm might be problematic. Click here for more details.

Files changed (120) hide show
  1. data/CHANGELOG +117 -49
  2. data/bin/mongrel_rpm +2 -2
  3. data/install.rb +42 -33
  4. data/lib/new_relic/agent.rb +149 -39
  5. data/lib/new_relic/agent/agent.rb +139 -122
  6. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  7. data/lib/new_relic/agent/collection_helper.rb +11 -2
  8. data/lib/new_relic/agent/error_collector.rb +33 -27
  9. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +30 -26
  10. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  11. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +316 -105
  12. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +22 -0
  13. data/lib/new_relic/agent/instrumentation/memcache.rb +18 -12
  14. data/lib/new_relic/agent/instrumentation/merb/errors.rb +2 -1
  15. data/lib/new_relic/agent/instrumentation/metric_frame.rb +258 -0
  16. data/lib/new_relic/agent/instrumentation/net.rb +7 -11
  17. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +1 -1
  18. data/lib/new_relic/agent/instrumentation/rack.rb +109 -0
  19. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +6 -5
  20. data/lib/new_relic/agent/instrumentation/rails/errors.rb +7 -7
  21. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -0
  22. data/lib/new_relic/agent/method_tracer.rb +305 -150
  23. data/lib/new_relic/agent/sampler.rb +34 -0
  24. data/lib/new_relic/agent/samplers/cpu_sampler.rb +11 -1
  25. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +37 -0
  26. data/lib/new_relic/agent/samplers/memory_sampler.rb +22 -10
  27. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  28. data/lib/new_relic/agent/shim_agent.rb +10 -0
  29. data/lib/new_relic/agent/stats_engine.rb +16 -278
  30. data/lib/new_relic/agent/stats_engine/metric_stats.rb +118 -0
  31. data/lib/new_relic/agent/stats_engine/samplers.rb +81 -0
  32. data/lib/new_relic/agent/stats_engine/transactions.rb +149 -0
  33. data/lib/new_relic/agent/transaction_sampler.rb +73 -67
  34. data/lib/new_relic/agent/worker_loop.rb +69 -68
  35. data/lib/new_relic/commands/deployments.rb +4 -6
  36. data/lib/new_relic/control.rb +121 -60
  37. data/lib/new_relic/control/external.rb +13 -0
  38. data/lib/new_relic/control/merb.rb +2 -0
  39. data/lib/new_relic/control/rails.rb +16 -6
  40. data/lib/new_relic/control/ruby.rb +8 -5
  41. data/lib/new_relic/control/sinatra.rb +18 -0
  42. data/lib/new_relic/delayed_job_injection.rb +25 -0
  43. data/lib/new_relic/histogram.rb +89 -0
  44. data/lib/new_relic/local_environment.rb +64 -30
  45. data/lib/new_relic/metric_data.rb +15 -6
  46. data/lib/new_relic/metric_parser.rb +14 -1
  47. data/lib/new_relic/metric_parser/active_record.rb +14 -0
  48. data/lib/new_relic/metric_parser/controller.rb +5 -2
  49. data/lib/new_relic/metric_parser/external.rb +50 -0
  50. data/lib/new_relic/metric_parser/other_transaction.rb +15 -0
  51. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  52. data/lib/new_relic/metric_spec.rb +39 -20
  53. data/lib/new_relic/metrics.rb +9 -7
  54. data/lib/new_relic/noticed_error.rb +6 -8
  55. data/lib/new_relic/rack/metric_app.rb +5 -4
  56. data/lib/new_relic/rack/{newrelic.ru → mongrel_rpm.ru} +4 -4
  57. data/lib/new_relic/rack/newrelic.yml +1 -0
  58. data/lib/new_relic/{rack.rb → rack_app.rb} +0 -0
  59. data/lib/new_relic/recipes.rb +1 -1
  60. data/lib/new_relic/stats.rb +40 -26
  61. data/lib/new_relic/transaction_analysis.rb +5 -2
  62. data/lib/new_relic/transaction_sample.rb +134 -55
  63. data/lib/new_relic/version.rb +27 -20
  64. data/lib/new_relic_api.rb +67 -47
  65. data/lib/newrelic_rpm.rb +5 -5
  66. data/lib/tasks/tests.rake +2 -0
  67. data/newrelic.yml +69 -29
  68. data/test/active_record_fixtures.rb +2 -2
  69. data/test/config/newrelic.yml +4 -7
  70. data/test/config/test_control.rb +1 -2
  71. data/test/new_relic/agent/active_record_instrumentation_test.rb +115 -31
  72. data/test/new_relic/agent/agent_controller_test.rb +274 -0
  73. data/test/new_relic/agent/agent_test_controller.rb +42 -6
  74. data/test/new_relic/agent/busy_calculator_test.rb +79 -0
  75. data/test/new_relic/agent/collection_helper_test.rb +10 -3
  76. data/test/new_relic/agent/error_collector_test.rb +35 -17
  77. data/test/new_relic/agent/method_tracer_test.rb +60 -20
  78. data/test/new_relic/agent/metric_data_test.rb +2 -2
  79. data/test/new_relic/agent/metric_frame_test.rb +51 -0
  80. data/test/new_relic/agent/net_instrumentation_test.rb +77 -0
  81. data/test/new_relic/agent/{agent_test.rb → rpm_agent_test.rb} +26 -5
  82. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  83. data/test/new_relic/{samplers_test.rb → agent/stats_engine/samplers_test.rb} +23 -22
  84. data/test/new_relic/agent/{stats_engine_test.rb → stats_engine/stats_engine_test.rb} +19 -101
  85. data/test/new_relic/agent/task_instrumentation_test.rb +176 -0
  86. data/test/new_relic/agent/transaction_sample_builder_test.rb +2 -2
  87. data/test/new_relic/agent/transaction_sample_test.rb +53 -38
  88. data/test/new_relic/agent/transaction_sampler_test.rb +101 -33
  89. data/test/new_relic/agent/worker_loop_test.rb +16 -14
  90. data/test/new_relic/control_test.rb +26 -13
  91. data/test/new_relic/metric_parser_test.rb +31 -1
  92. data/test/new_relic/metric_spec_test.rb +2 -2
  93. data/test/new_relic/stats_test.rb +0 -8
  94. data/test/new_relic/version_number_test.rb +31 -1
  95. data/test/test_helper.rb +37 -1
  96. data/ui/controllers/newrelic_controller.rb +19 -11
  97. data/ui/helpers/google_pie_chart.rb +5 -11
  98. data/ui/helpers/newrelic_helper.rb +40 -35
  99. data/ui/views/layouts/newrelic_default.rhtml +7 -7
  100. data/ui/views/newrelic/_sample.rhtml +5 -1
  101. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  102. data/ui/views/newrelic/images/textmate.png +0 -0
  103. data/ui/views/newrelic/index.rhtml +13 -1
  104. data/ui/views/newrelic/show_sample.rhtml +5 -2
  105. data/ui/views/newrelic/stylesheets/style.css +54 -3
  106. metadata +65 -145
  107. data/Manifest +0 -143
  108. data/Rakefile +0 -22
  109. data/init.rb +0 -38
  110. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +0 -127
  111. data/lib/new_relic/agent/instrumentation/error_instrumentation.rb +0 -14
  112. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +0 -13
  113. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +0 -38
  114. data/lib/new_relic/agent/patch_const_missing.rb +0 -125
  115. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +0 -22
  116. data/lib/new_relic/metric_parser/database.rb +0 -23
  117. data/newrelic_rpm.gemspec +0 -35
  118. data/test/new_relic/agent/classloader_patch_test.rb +0 -56
  119. data/test/new_relic/agent/controller_test.rb +0 -107
  120. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +0 -70
@@ -0,0 +1,22 @@
1
+ require 'new_relic/agent/instrumentation/controller_instrumentation'
2
+
3
+ if defined?(Delayed::Job) and not NewRelic::Control.instance['disable_dj']
4
+ module NewRelic
5
+ module Agent
6
+ module Instrumentation
7
+ module DelayedJobInstrumentation
8
+
9
+ Delayed::Job.class_eval do
10
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
11
+ if self.instance_methods.include?('name')
12
+ add_transaction_tracer "invoke_job", :category => 'OtherTransaction/DelayedJob', :path => '#{self.name}'
13
+ else
14
+ add_transaction_tracer "invoke_job", :category => 'OtherTransaction/DelayedJob'
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,18 +1,24 @@
1
1
  # NOTE there are multiple implementations of the MemCache client in Ruby,
2
2
  # each with slightly different API's and semantics.
3
- # Currently we only cover memcache-client. Need to cover Ruby-MemCache.
4
3
  # See:
5
4
  # http://www.deveiate.org/code/Ruby-MemCache/ (Gem: Ruby-MemCache)
6
5
  # http://dev.robotcoop.com/Libraries/memcache-client/ (Gem: memcache-client)
7
- MemCache.class_eval do
8
- add_method_tracer :get, 'MemCache/read' if self.method_defined? :get
9
- add_method_tracer :set, 'MemCache/write' if self.method_defined? :set
10
- add_method_tracer :get_multi, 'MemCache/read' if self.method_defined? :get_multi
11
- end if defined? MemCache
6
+ unless NewRelic::Control.instance['disable_memcache_instrumentation']
7
+ MemCache.class_eval do
8
+ add_method_tracer :get, 'MemCache/read' if self.method_defined? :get
9
+ add_method_tracer :get_multi, 'MemCache/read' if self.method_defined? :get_multi
10
+ %w[set add incr decr delete].each do | method |
11
+ add_method_tracer method, 'MemCache/write' if self.method_defined? method
12
+ end
13
+ end if defined? MemCache
14
+
15
+ # Support for libmemcached through Evan Weaver's memcached wrapper
16
+ # http://blog.evanweaver.com/files/doc/fauna/memcached/classes/Memcached.html
17
+ Memcached.class_eval do
18
+ add_method_tracer :get, 'MemCache/read' if self.method_defined? :get
19
+ %w[set add increment decrement delete replace append prepend cas].each do | method |
20
+ add_method_tracer method, "MemCache/write" if self.method_defined? method
21
+ end
22
+ end if defined? Memcached
12
23
 
13
- # Support for libmemcached through Evan Weaver's memcached wrapper
14
- # http://blog.evanweaver.com/files/doc/fauna/memcached/classes/Memcached.html
15
- Memcached.class_eval do
16
- add_method_tracer :get, 'Memcached/read' if self.method_defined? :get
17
- add_method_tracer :set, 'Memcached/write' if self.method_defined? :set
18
- end if defined? Memcached
24
+ end
@@ -1,7 +1,8 @@
1
1
  # Hook in the notification to merb
2
2
  error_notifier = Proc.new {
3
3
  if request.exceptions #check that there's actually an exception
4
- NewRelic::Agent.agent.error_collector.notice_error(request.exceptions.first, request, "#{params[:controller]}/#{params[:action]}", params)
4
+ # Note, this assumes we have already captured the other information such as uri and params in the MetricFrame.
5
+ NewRelic::Agent::Instrumentation::MetricFrame.notice_error(request.exceptions.first)
5
6
  end
6
7
  }
7
8
  Merb::Dispatcher::DefaultException.before error_notifier
@@ -0,0 +1,258 @@
1
+ # A struct holding the information required to measure a controller
2
+ # action. This is put on the thread local. Handles the issue of
3
+ # re-entrancy, or nested action calls.
4
+ #
5
+ # This class is not part of the public API. Avoid making calls on it directly.
6
+ #
7
+ module NewRelic::Agent::Instrumentation
8
+ class MetricFrame
9
+ attr_accessor :start, :apdex_start, :exception,
10
+ :filtered_params, :force_flag,
11
+ :jruby_cpu_start, :process_cpu_start, :database_metric_name
12
+
13
+ # Return the currently active metric frame, or nil. Call with +true+
14
+ # to create a new metric frame if one is not already on the thread.
15
+ def self.current(create_if_empty=nil)
16
+ Thread.current[:newrelic_metric_frame] ||= create_if_empty && new
17
+ end
18
+
19
+ # This is the name of the model currently assigned to database
20
+ # measurements, overriding the default.
21
+ def self.database_metric_name
22
+ current && current.database_metric_name
23
+ end
24
+
25
+ @@java_classes_loaded = false
26
+
27
+ if defined? JRuby
28
+ begin
29
+ require 'java'
30
+ include_class 'java.lang.management.ManagementFactory'
31
+ include_class 'com.sun.management.OperatingSystemMXBean'
32
+ @@java_classes_loaded = true
33
+ rescue Exception => e
34
+ end
35
+ end
36
+
37
+ attr_reader :depth
38
+
39
+ def initialize
40
+ @start = Time.now.to_f
41
+ @path_stack = [] # stack of [controller, path] elements
42
+ @jruby_cpu_start = jruby_cpu_time
43
+ @process_cpu_start = process_cpu
44
+ end
45
+
46
+ # Indicate that we are entering a measured controller action or task.
47
+ # Make sure you unwind every push with a pop call.
48
+ def push(category, path)
49
+ NewRelic::Agent.instance.transaction_sampler.notice_first_scope_push(start)
50
+ @path_stack.push [category, path]
51
+ end
52
+
53
+ # Indicate that you don't want to keep the currently saved transaction
54
+ # information
55
+ def self.abort_transaction!
56
+ current.abort_transaction! if current
57
+ end
58
+
59
+ # Give the current metric frame a request context. Use this to
60
+ # get the URI and referer. The request is interpreted loosely
61
+ # as a Rack::Request or an ActionController::AbstractRequest.
62
+ def request=(request)
63
+ @request = request
64
+ end
65
+
66
+ # For the current web transaction, return the path of the URI minus the host part and query string, or nil.
67
+ def uri
68
+ return @uri if @uri || @request.nil?
69
+ approximate_uri = case
70
+ when @request.respond_to?(:url) then @request.url
71
+ when @request.respond_to?(:uri) then @request.uri
72
+ when @request.respond_to?(:fullpath) then @request.fullpath
73
+ when @request.respond_to?(:path) then @request.path
74
+ end
75
+ @uri = approximate_uri.split('?').first || '/' if approximate_uri
76
+ end
77
+
78
+ # For the current web transaction, return the full referer, minus the host string, or nil.
79
+ def referer
80
+ return @referer if @referer || @request.nil? || !@request.respond_to?(:referer) || !@request.referer
81
+ @referer = @request.referer.split('?').first
82
+ end
83
+
84
+ # Call this to ensure that the current transaction is not saved
85
+ def abort_transaction!
86
+ NewRelic::Agent.instance.transaction_sampler.ignore_transaction
87
+ end
88
+ # This needs to be called after entering the call to trace the controller action, otherwise
89
+ # the controller action blames itself. It gets reset in the normal #pop call.
90
+ def start_transaction
91
+ NewRelic::Agent.instance.stats_engine.start_transaction metric_name
92
+ # Only push the transaction context info once, on entry:
93
+ if @path_stack.size == 1
94
+ NewRelic::Agent.instance.transaction_sampler.notice_transaction(metric_name, uri, filtered_params)
95
+ end
96
+ end
97
+
98
+ def category
99
+ @path_stack.last.first
100
+ end
101
+
102
+ def path
103
+ @path_stack.last.last
104
+ end
105
+
106
+ # Unwind one stack level. It knows if it's back at the outermost caller and
107
+ # does the appropriate wrapup of the context.
108
+ def pop
109
+ category, path = @path_stack.pop
110
+ if category.nil?
111
+ NewRelic::Control.instance.log.error "Underflow in metric frames: #{caller.join("\n ")}"
112
+ end
113
+ if @path_stack.empty?
114
+ if NewRelic::Agent.is_execution_traced?
115
+ cpu_burn = nil
116
+ if @process_cpu_start
117
+ cpu_burn = process_cpu - @process_cpu_start
118
+ elsif @jruby_cpu_start
119
+ cpu_burn = jruby_cpu_time - @jruby_cpu_start
120
+ NewRelic::Agent.get_stats_no_scope(NewRelic::Metrics::USER_TIME).record_data_point(cpu_burn)
121
+ end
122
+ NewRelic::Agent.instance.transaction_sampler.notice_transaction_cpu_time(cpu_burn) if cpu_burn
123
+ NewRelic::Agent.instance.histogram.process(Time.now.to_f - start) if recording_web_transaction?(category)
124
+ NewRelic::Agent.instance.transaction_sampler.notice_scope_empty
125
+ end
126
+ NewRelic::Agent.instance.stats_engine.end_transaction
127
+ Thread.current[:newrelic_metric_frame] = nil
128
+ else # path stack not empty
129
+ # change the transaction name back to whatever was on the stack.
130
+ NewRelic::Agent.instance.stats_engine.scope_name = metric_name
131
+ end
132
+ end
133
+
134
+ # If we have an active metric frame, notice the error and increment the error metric.
135
+ def self.notice_error(e, custom_params=nil)
136
+ if current
137
+ current.notice_error(e, custom_params)
138
+ else
139
+ NewRelic::Agent.instance.error_collector.notice_error(e, :custom_params => custom_params)
140
+ end
141
+ end
142
+
143
+ def notice_error(e, custom_params=nil)
144
+ if exception != e
145
+ NewRelic::Agent.instance.error_collector.notice_error(e, :referer => referer, :uri => uri, :metric => metric_name, :request_params => filtered_params, :custom_params => custom_params)
146
+ self.exception = e
147
+ end
148
+ end
149
+
150
+ # Add context parameters to the metric frame. This information will be passed in to errors
151
+ # and transaction traces. Keys and Values should be strings, numbers or date/times.
152
+ def self.add_custom_parameters(p)
153
+ current.add_custom_parameters(p) if current
154
+ end
155
+
156
+ def self.custom_parameters
157
+ (current && current.custom_parameters) ? current.custom_parameters : {}
158
+ end
159
+
160
+ def record_apdex
161
+ return unless recording_web_transaction? && NewRelic::Agent.is_execution_traced?
162
+ ending = Time.now.to_f
163
+ summary_stat = NewRelic::Agent.instance.stats_engine.get_custom_stats("Apdex", NewRelic::ApdexStats)
164
+ controller_stat = NewRelic::Agent.instance.stats_engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
165
+ update_apdex(summary_stat, ending - apdex_start, exception)
166
+ update_apdex(controller_stat, ending - start, exception)
167
+ end
168
+
169
+ def metric_name
170
+ return nil if @path_stack.empty?
171
+ category + '/' + path
172
+ end
173
+
174
+ # Return the array of metrics to record for the current metric frame.
175
+ def recorded_metrics
176
+ metrics = [ metric_name ]
177
+ if @path_stack.size == 1
178
+ if recording_web_transaction?
179
+ metrics += ["HttpDispatcher"]
180
+ else
181
+ metrics += ["#{category}/all", "OtherTransaction/all"]
182
+ end
183
+ end
184
+ metrics
185
+ end
186
+
187
+ # Yield to a block that is run with a database metric name context. This means
188
+ # the Database instrumentation will use this for the metric name if it does not
189
+ # otherwise know about a model. This is re-entrant.
190
+ #
191
+ # * <tt>model</tt> is the DB model class
192
+ # * <tt>method</tt> is the name of the finder method or other method to identify the operation with.
193
+ #
194
+ def with_database_metric_name(model, method)
195
+ previous = @database_metric_name
196
+ model_name = case model
197
+ when Class
198
+ model.name
199
+ when String
200
+ model
201
+ else
202
+ model.to_s
203
+ end
204
+ @database_metric_name = "ActiveRecord/#{model_name}/#{method}"
205
+ yield
206
+ ensure
207
+ @database_metric_name=previous
208
+ end
209
+
210
+ def custom_parameters
211
+ @custom_parameters ||= {}
212
+ end
213
+
214
+ def add_custom_parameters(p)
215
+ custom_parameters.merge!(p)
216
+ end
217
+
218
+ def self.recording_web_transaction?
219
+ if c = Thread.current[:newrelic_metric_frame]
220
+ c.recording_web_transaction?
221
+ end
222
+ end
223
+
224
+ def recording_web_transaction?(cat = category)
225
+ 0 == cat.index("Controller")
226
+ end
227
+
228
+ private
229
+
230
+ def update_apdex(stat, duration, failed)
231
+ apdex_t = NewRelic::Control.instance.apdex_t
232
+ case
233
+ when failed
234
+ stat.record_apdex_f
235
+ when duration <= apdex_t
236
+ stat.record_apdex_s
237
+ when duration <= 4 * apdex_t
238
+ stat.record_apdex_t
239
+ else
240
+ stat.record_apdex_f
241
+ end
242
+ end
243
+
244
+ def process_cpu
245
+ return nil if defined? JRuby
246
+ p = Process.times
247
+ p.stime + p.utime
248
+ end
249
+
250
+ def jruby_cpu_time # :nodoc:
251
+ return nil unless @@java_classes_loaded
252
+ threadMBean = ManagementFactory.getThreadMXBean()
253
+ java_utime = threadMBean.getCurrentThreadUserTime() # ns
254
+ -1 == java_utime ? 0.0 : java_utime/1e9
255
+ end
256
+
257
+ end
258
+ end
@@ -1,18 +1,14 @@
1
1
  if defined? Net::HTTP
2
2
  Net::HTTP.class_eval do
3
3
  def request_with_newrelic_trace(*args, &block)
4
- if Thread::current[:newrelic_scope_stack].nil?
5
- request_without_newrelic_trace(*args, &block)
4
+ metrics = ["External/#{@address}/Net::HTTP/#{args[0].method}","External/#{@address}/all"]
5
+ if NewRelic::Agent::Instrumentation::MetricFrame.recording_web_transaction?
6
+ metrics << "External/allWeb"
6
7
  else
7
- self.class.trace_method_execution_with_scope("External/#{@address}/Net::HTTP/#{args[0].method}",
8
- true,
9
- true) do
10
- self.class.trace_method_execution_no_scope("External/#{@address}/all") do
11
- self.class.trace_method_execution_no_scope("External/allWeb") do
12
- request_without_newrelic_trace(*args, &block)
13
- end
14
- end
15
- end
8
+ metrics << "External/allOther"
9
+ end
10
+ self.class.trace_execution_scoped metrics do
11
+ request_without_newrelic_trace(*args, &block)
16
12
  end
17
13
  end
18
14
  alias request_without_newrelic_trace request
@@ -2,7 +2,7 @@ if defined?(PhusionPassenger)
2
2
  NewRelic::Control.instance.log.debug "Installing Passenger event hooks."
3
3
 
4
4
  PhusionPassenger.on_event(:stopping_worker_process) do
5
- NewRelic::Control.instance.log.info "Passenger stopping this process, shutdown the agent."
5
+ NewRelic::Control.instance.log.debug "Passenger stopping this process, shutdown the agent."
6
6
  NewRelic::Agent.instance.shutdown
7
7
  end
8
8
 
@@ -0,0 +1,109 @@
1
+ require 'new_relic/agent/instrumentation/controller_instrumentation'
2
+
3
+ module NewRelic
4
+ module Agent
5
+ module Instrumentation
6
+ # == Instrumentation for Rack
7
+ #
8
+ # New Relic will instrument a #call method as if it were a controller
9
+ # action, collecting transaction traces and errors. The middleware will
10
+ # be identified only by it's class, so if you want to instrument multiple
11
+ # actions in a middleware, you need to use
12
+ # NewRelic::Agent::Instrumentation::ControllerInstrumentation::ClassMethods#add_transaction_tracer
13
+ #
14
+ # Example:
15
+ # require 'new_relic/agent/instrumentation/rack'
16
+ # class Middleware
17
+ # def call(env)
18
+ # ...
19
+ # end
20
+ # # Do the include after the call method is defined:
21
+ # include NewRelic::Agent::Instrumentation::Rack
22
+ # end
23
+ #
24
+ # == Instrumenting Metal
25
+ #
26
+ # If you are using Metal, be sure and extend the your Metal class with the
27
+ # Rack instrumentation:
28
+ #
29
+ # require 'new_relic/agent/instrumentation/rack'
30
+ # class MetalApp
31
+ # def self.call(env)
32
+ # ...
33
+ # end
34
+ # # Do the include after the call method is defined:
35
+ # extend NewRelic::Agent::Instrumentation::Rack
36
+ # end
37
+ #
38
+ # == Overriding the metric name
39
+ #
40
+ # By default the middleware is identified only by its class, but if you want to
41
+ # be more specific and pass in name, then omit including the Rack instrumentation
42
+ # and instead follow this example:
43
+ #
44
+ # require 'new_relic/agent/instrumentation/controller_instrumentation'
45
+ # class Middleware
46
+ # include NewRelic::Agent::Instrumentation::ControllerInstrumentation
47
+ # def call(env)
48
+ # ...
49
+ # end
50
+ # add_transaction_tracer :call, :category => :rack, :name => 'my app'
51
+ # end
52
+ #
53
+ # == Cascading or chained calls
54
+ #
55
+ # Calls which return a 404 will not have transactions recorded, but
56
+ # any calls to instrumented frameworks like ActiveRecord will still be
57
+ # captured even if the result is a 404. To avoid this you need to
58
+ # instrument only when you are the endpoint.
59
+ #
60
+ # In these cases, you should not include or extend the Rack module but instead
61
+ # include NewRelic::Agent::Instrumentation::ControllerInstrumentation.
62
+ # Here's how that might look:
63
+ #
64
+ # require 'new_relic/agent/instrumentation/controller_instrumentation'
65
+ # class MetalApp
66
+ # extend NewRelic::Agent::Instrumentation::ControllerInstrumentation
67
+ # def self.call(env)
68
+ # if should_do_my_thing?
69
+ # perform_action_with_newrelic_trace(:category => :rack) do
70
+ # return my_response(env)
71
+ # end
72
+ # else
73
+ # return [404, {"Content-Type" => "text/html"}, ["Not Found"]]
74
+ # end
75
+ # end
76
+ # end
77
+ #
78
+ module Rack
79
+ def newrelic_request_headers
80
+ @newrelic_request.env
81
+ end
82
+ def call_with_newrelic(*args)
83
+ @newrelic_request = ::Rack::Request.new(args.first)
84
+ perform_action_with_newrelic_trace(:category => :rack, :request => @newrelic_request) do
85
+ result = call_without_newrelic(*args)
86
+ # Ignore cascaded calls
87
+ MetricFrame.abort_transaction! if result.first == 404
88
+ result
89
+ end
90
+ end
91
+ def self.included middleware #:nodoc:
92
+ middleware.class_eval do
93
+ alias call_without_newrelic call
94
+ alias call call_with_newrelic
95
+ end
96
+ end
97
+ include ControllerInstrumentation
98
+ def self.extended middleware #:nodoc:
99
+ middleware.class_eval do
100
+ class << self
101
+ alias call_without_newrelic call
102
+ alias call call_with_newrelic
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end