factorylabs-newrelic_rpm 2.10.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +354 -0
- data/LICENSE +37 -0
- data/README-2.10.2.2 +10 -0
- data/README.md +138 -0
- data/bin/mongrel_rpm +33 -0
- data/bin/newrelic_cmd +4 -0
- data/cert/cacert.pem +34 -0
- data/install.rb +45 -0
- data/lib/new_relic/agent.rb +315 -0
- data/lib/new_relic/agent/agent.rb +647 -0
- data/lib/new_relic/agent/busy_calculator.rb +86 -0
- data/lib/new_relic/agent/chained_call.rb +13 -0
- data/lib/new_relic/agent/collection_helper.rb +66 -0
- data/lib/new_relic/agent/error_collector.rb +117 -0
- data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
- data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +91 -0
- data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
- data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +389 -0
- data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
- data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +12 -0
- data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
- data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
- data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
- data/lib/new_relic/agent/instrumentation/metric_frame.rb +199 -0
- data/lib/new_relic/agent/instrumentation/net.rb +21 -0
- data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
- data/lib/new_relic/agent/instrumentation/rack.rb +109 -0
- data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
- data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
- data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
- data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
- data/lib/new_relic/agent/method_tracer.rb +348 -0
- data/lib/new_relic/agent/patch_const_missing.rb +125 -0
- data/lib/new_relic/agent/sampler.rb +46 -0
- data/lib/new_relic/agent/samplers/cpu_sampler.rb +50 -0
- data/lib/new_relic/agent/samplers/memory_sampler.rb +141 -0
- data/lib/new_relic/agent/samplers/mongrel_sampler.rb +23 -0
- data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
- data/lib/new_relic/agent/shim_agent.rb +21 -0
- data/lib/new_relic/agent/stats_engine.rb +22 -0
- data/lib/new_relic/agent/stats_engine/metric_stats.rb +116 -0
- data/lib/new_relic/agent/stats_engine/samplers.rb +74 -0
- data/lib/new_relic/agent/stats_engine/transactions.rb +154 -0
- data/lib/new_relic/agent/transaction_sampler.rb +315 -0
- data/lib/new_relic/agent/worker_loop.rb +118 -0
- data/lib/new_relic/commands/deployments.rb +145 -0
- data/lib/new_relic/commands/new_relic_commands.rb +30 -0
- data/lib/new_relic/control.rb +484 -0
- data/lib/new_relic/control/external.rb +13 -0
- data/lib/new_relic/control/merb.rb +24 -0
- data/lib/new_relic/control/rails.rb +151 -0
- data/lib/new_relic/control/ruby.rb +36 -0
- data/lib/new_relic/control/sinatra.rb +14 -0
- data/lib/new_relic/histogram.rb +89 -0
- data/lib/new_relic/local_environment.rb +299 -0
- data/lib/new_relic/merbtasks.rb +6 -0
- data/lib/new_relic/metric_data.rb +42 -0
- data/lib/new_relic/metric_parser.rb +124 -0
- data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
- data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
- data/lib/new_relic/metric_parser/active_record.rb +25 -0
- data/lib/new_relic/metric_parser/controller.rb +54 -0
- data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
- data/lib/new_relic/metric_parser/errors.rb +6 -0
- data/lib/new_relic/metric_parser/external.rb +50 -0
- data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
- data/lib/new_relic/metric_parser/other_transaction.rb +15 -0
- data/lib/new_relic/metric_parser/view.rb +61 -0
- data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
- data/lib/new_relic/metric_parser/web_service.rb +9 -0
- data/lib/new_relic/metric_spec.rb +67 -0
- data/lib/new_relic/metrics.rb +7 -0
- data/lib/new_relic/noticed_error.rb +23 -0
- data/lib/new_relic/rack/metric_app.rb +56 -0
- data/lib/new_relic/rack/mongrel_rpm.ru +25 -0
- data/lib/new_relic/rack/newrelic.yml +26 -0
- data/lib/new_relic/rack_app.rb +5 -0
- data/lib/new_relic/recipes.rb +82 -0
- data/lib/new_relic/stats.rb +362 -0
- data/lib/new_relic/transaction_analysis.rb +121 -0
- data/lib/new_relic/transaction_sample.rb +671 -0
- data/lib/new_relic/version.rb +54 -0
- data/lib/new_relic_api.rb +276 -0
- data/lib/newrelic_rpm.rb +45 -0
- data/lib/tasks/all.rb +4 -0
- data/lib/tasks/install.rake +7 -0
- data/lib/tasks/tests.rake +15 -0
- data/newrelic.yml +227 -0
- data/newrelic_rpm.gemspec +214 -0
- data/recipes/newrelic.rb +6 -0
- data/test/active_record_fixtures.rb +55 -0
- data/test/config/newrelic.yml +46 -0
- data/test/config/test_control.rb +38 -0
- data/test/new_relic/agent/active_record_instrumentation_test.rb +268 -0
- data/test/new_relic/agent/agent_controller_test.rb +254 -0
- data/test/new_relic/agent/agent_test_controller.rb +78 -0
- data/test/new_relic/agent/busy_calculator_test.rb +79 -0
- data/test/new_relic/agent/classloader_patch_test.rb +56 -0
- data/test/new_relic/agent/collection_helper_test.rb +125 -0
- data/test/new_relic/agent/error_collector_test.rb +173 -0
- data/test/new_relic/agent/method_tracer_test.rb +340 -0
- data/test/new_relic/agent/metric_data_test.rb +56 -0
- data/test/new_relic/agent/mock_ar_connection.rb +40 -0
- data/test/new_relic/agent/mock_scope_listener.rb +23 -0
- data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
- data/test/new_relic/agent/rpm_agent_test.rb +125 -0
- data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
- data/test/new_relic/agent/stats_engine/samplers_test.rb +88 -0
- data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
- data/test/new_relic/agent/task_instrumentation_test.rb +177 -0
- data/test/new_relic/agent/testable_agent.rb +13 -0
- data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
- data/test/new_relic/agent/transaction_sample_test.rb +186 -0
- data/test/new_relic/agent/transaction_sampler_test.rb +404 -0
- data/test/new_relic/agent/worker_loop_test.rb +103 -0
- data/test/new_relic/control_test.rb +110 -0
- data/test/new_relic/delayed_job_test.rb +108 -0
- data/test/new_relic/deployments_api_test.rb +68 -0
- data/test/new_relic/environment_test.rb +75 -0
- data/test/new_relic/metric_parser_test.rb +172 -0
- data/test/new_relic/metric_spec_test.rb +177 -0
- data/test/new_relic/shim_agent_test.rb +9 -0
- data/test/new_relic/stats_test.rb +291 -0
- data/test/new_relic/version_number_test.rb +76 -0
- data/test/test_helper.rb +53 -0
- data/test/ui/newrelic_controller_test.rb +14 -0
- data/test/ui/newrelic_helper_test.rb +53 -0
- data/ui/controllers/newrelic_controller.rb +220 -0
- data/ui/helpers/google_pie_chart.rb +49 -0
- data/ui/helpers/newrelic_helper.rb +320 -0
- data/ui/views/layouts/newrelic_default.rhtml +47 -0
- data/ui/views/newrelic/_explain_plans.rhtml +27 -0
- data/ui/views/newrelic/_sample.rhtml +19 -0
- data/ui/views/newrelic/_segment.rhtml +28 -0
- data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
- data/ui/views/newrelic/_segment_row.rhtml +14 -0
- data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
- data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
- data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
- data/ui/views/newrelic/_sql_row.rhtml +11 -0
- data/ui/views/newrelic/_stack_trace.rhtml +30 -0
- data/ui/views/newrelic/_table.rhtml +12 -0
- data/ui/views/newrelic/explain_sql.rhtml +42 -0
- data/ui/views/newrelic/images/arrow-close.png +0 -0
- data/ui/views/newrelic/images/arrow-open.png +0 -0
- data/ui/views/newrelic/images/blue_bar.gif +0 -0
- data/ui/views/newrelic/images/file_icon.png +0 -0
- data/ui/views/newrelic/images/gray_bar.gif +0 -0
- data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
- data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
- data/ui/views/newrelic/images/textmate.png +0 -0
- data/ui/views/newrelic/index.rhtml +57 -0
- data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
- data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
- data/ui/views/newrelic/sample_not_found.rhtml +2 -0
- data/ui/views/newrelic/show_sample.rhtml +80 -0
- data/ui/views/newrelic/show_source.rhtml +3 -0
- data/ui/views/newrelic/stylesheets/style.css +484 -0
- data/ui/views/newrelic/threads.rhtml +52 -0
- metadata +238 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
|
2
|
+
# NewRelic instrumentation for DataMapper
|
3
|
+
# For now, we have to refer to all db metrics as "ActiveRecord"
|
4
|
+
if defined? DataMapper
|
5
|
+
|
6
|
+
DataMapper::Model.class_eval do
|
7
|
+
add_method_tracer :get, 'ActiveRecord/#{self.name}/find'
|
8
|
+
add_method_tracer :first, 'ActiveRecord/#{self.name}/find'
|
9
|
+
add_method_tracer :first_or_create, 'ActiveRecord/#{self.name}/find'
|
10
|
+
add_method_tracer :all, 'ActiveRecord/#{self.name}/find_all'
|
11
|
+
end
|
12
|
+
DataMapper::Adapters::DataObjectsAdapter.class_eval do
|
13
|
+
|
14
|
+
@@my_sql_defined = defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
|
15
|
+
@@postgres_defined = defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
|
16
|
+
|
17
|
+
for method in [:read_many, :read_one] do
|
18
|
+
add_method_tracer method, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/find' # Need to get the model somehow
|
19
|
+
add_method_tracer method, 'ActiveRecord/find', :push_scope => false
|
20
|
+
add_method_tracer method, 'ActiveRecord/all', :push_scope => false
|
21
|
+
end
|
22
|
+
for method in [:execute, :query] do
|
23
|
+
add_method_tracer method, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/execute' # Need to get the model somehow
|
24
|
+
add_method_tracer method, 'ActiveRecord/all', :push_scope => false
|
25
|
+
end
|
26
|
+
for method in [:create, :update]do
|
27
|
+
add_method_tracer method, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/save'
|
28
|
+
add_method_tracer method, 'ActiveRecord/save', :push_scope => false
|
29
|
+
end
|
30
|
+
add_method_tracer :delete, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/destroy'
|
31
|
+
add_method_tracer :delete, 'ActiveRecord/destroy', :push_scope => false
|
32
|
+
|
33
|
+
def log_with_newrelic_instrumentation(sql, name, &block)
|
34
|
+
# if we aren't in a blamed context, then add one so that we can see that
|
35
|
+
# controllers are calling SQL directly
|
36
|
+
# we check scope_depth vs 2 since the controller is 1, and the
|
37
|
+
#
|
38
|
+
if NewRelic::Agent.instance.transaction_sampler.scope_depth < 2
|
39
|
+
self.class.trace_method_execution "Database/DirectSQL", true, true do
|
40
|
+
log_with_capture_sql(sql, name, &block)
|
41
|
+
end
|
42
|
+
else
|
43
|
+
log_with_capture_sql(sql, name, &block)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def log_with_capture_sql(sql, name, &block)
|
48
|
+
if @@my_sql_defined && self.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
|
49
|
+
config = @config
|
50
|
+
elsif @@postgres_defined && self.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
51
|
+
config = @config
|
52
|
+
else
|
53
|
+
config = nil
|
54
|
+
end
|
55
|
+
|
56
|
+
t0 = Time.now
|
57
|
+
result = log_without_newrelic_instrumentation(sql, name, &block)
|
58
|
+
|
59
|
+
NewRelic::Agent.instance.transaction_sampler.notice_sql(sql, config, Time.now - t0)
|
60
|
+
|
61
|
+
result
|
62
|
+
end
|
63
|
+
|
64
|
+
# Compare with #alias_method_chain, which is not available in
|
65
|
+
# Rails 1.1:
|
66
|
+
#alias_method :log_without_newrelic_instrumentation, :log
|
67
|
+
#alias_method :log, :log_with_newrelic_instrumentation
|
68
|
+
|
69
|
+
=begin
|
70
|
+
# instrumentation for associations
|
71
|
+
module Associations
|
72
|
+
class AssociationCollection
|
73
|
+
add_method_tracer :delete, 'ActiveRecord/#{@owner.class.name}/association delete'
|
74
|
+
end
|
75
|
+
|
76
|
+
def HasAndBelongsToManyAssociation
|
77
|
+
add_method_tracer :find, 'ActiveRecord/#{@owner.class.name}/association find'
|
78
|
+
add_method_tracer :create_record, 'ActiveRecord/#{@owner.class.name}/association create'
|
79
|
+
add_method_tracer :insert_record, 'ActiveRecord/#{@owner.class.name}/association insert'
|
80
|
+
end
|
81
|
+
|
82
|
+
class HasManyAssociation
|
83
|
+
# add_method_tracer :find, 'ActiveRecord/#{@owner.class.name}/association find'
|
84
|
+
# add_method_tracer :insert_record, 'ActiveRecord/#{@owner.class.name}/association insert'
|
85
|
+
# add_method_tracer :create_record, 'ActiveRecord/#{@owner.class.name}/association create'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
=end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
if defined?(Delayed::Job)
|
2
|
+
Delayed::Job.class_eval do
|
3
|
+
include NewRelic::Agent::Instrumentation::ControllerInstrumentation
|
4
|
+
|
5
|
+
if self.instance_methods.include?('name')
|
6
|
+
add_transaction_tracer "invoke_job", :category => :task, :name => '#{self.name}'
|
7
|
+
else
|
8
|
+
add_transaction_tracer "invoke_job", :category => :task
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# NOTE there are multiple implementations of the MemCache client in Ruby,
|
2
|
+
# each with slightly different API's and semantics.
|
3
|
+
# See:
|
4
|
+
# http://www.deveiate.org/code/Ruby-MemCache/ (Gem: Ruby-MemCache)
|
5
|
+
# http://dev.robotcoop.com/Libraries/memcache-client/ (Gem: memcache-client)
|
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
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'merb-core/controller/merb_controller'
|
3
|
+
|
4
|
+
Merb::Controller.class_eval do
|
5
|
+
include NewRelic::Agent::Instrumentation::ControllerInstrumentation
|
6
|
+
|
7
|
+
class_inheritable_accessor :do_not_trace
|
8
|
+
class_inheritable_accessor :ignore_apdex
|
9
|
+
|
10
|
+
def self.newrelic_write_attr(attr_name, value) # :nodoc:
|
11
|
+
self.send "#{attr_name}=", attr_name, value
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.newrelic_read_attr(attr_name) # :nodoc:
|
15
|
+
self.send attr_name
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
# determine the path that is used in the metric name for
|
20
|
+
# the called controller action
|
21
|
+
def newrelic_metric_path(action)
|
22
|
+
"#{controller_name}/#{action}"
|
23
|
+
end
|
24
|
+
alias_method :perform_action_without_newrelic_trace, :_dispatch
|
25
|
+
alias_method :_dispatch, :perform_action_with_newrelic_trace
|
26
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# Hook in the notification to merb
|
2
|
+
error_notifier = Proc.new {
|
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)
|
5
|
+
end
|
6
|
+
}
|
7
|
+
Merb::Dispatcher::DefaultException.before error_notifier
|
8
|
+
Exceptions.before error_notifier
|
@@ -0,0 +1,199 @@
|
|
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
|
+
class NewRelic::Agent::Instrumentation::MetricFrame
|
8
|
+
attr_accessor :start, :apdex_start, :exception,
|
9
|
+
:filtered_params, :available_request, :force_flag,
|
10
|
+
:jruby_cpu_start, :process_cpu_start, :database_metric_name
|
11
|
+
|
12
|
+
# Return the currently active metric frame, or nil. Call with +true+
|
13
|
+
# to create a new metric frame if one is not already on the thread.
|
14
|
+
def self.current(create_if_empty=nil)
|
15
|
+
Thread.current[:newrelic_metric_frame] ||= create_if_empty && new
|
16
|
+
end
|
17
|
+
|
18
|
+
# This is the name of the model currently assigned to database
|
19
|
+
# measurements, overriding the default.
|
20
|
+
def self.database_metric_name
|
21
|
+
current && current.database_metric_name
|
22
|
+
end
|
23
|
+
|
24
|
+
@@java_classes_loaded = false
|
25
|
+
|
26
|
+
if defined? JRuby
|
27
|
+
begin
|
28
|
+
require 'java'
|
29
|
+
include_class 'java.lang.management.ManagementFactory'
|
30
|
+
include_class 'com.sun.management.OperatingSystemMXBean'
|
31
|
+
@@java_classes_loaded = true
|
32
|
+
rescue Exception => e
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_reader :depth
|
37
|
+
|
38
|
+
def initialize
|
39
|
+
@start = Time.now.to_f
|
40
|
+
@path_stack = [] # stack of [controller, path] elements
|
41
|
+
@jruby_cpu_start = jruby_cpu_time
|
42
|
+
@process_cpu_start = process_cpu
|
43
|
+
end
|
44
|
+
|
45
|
+
# Indicate that we are entering a measured controller action or task.
|
46
|
+
# Make sure you unwind every push with a pop call.
|
47
|
+
def push(category, path)
|
48
|
+
@path_stack.push [category, path]
|
49
|
+
end
|
50
|
+
|
51
|
+
# Indicate that you don't want to keep the currently saved transaction
|
52
|
+
# information
|
53
|
+
def self.abort_transaction!
|
54
|
+
current.abort_transaction! if current
|
55
|
+
end
|
56
|
+
|
57
|
+
# Call this to ensure that the current transaction is not saved
|
58
|
+
def abort_transaction!
|
59
|
+
NewRelic::Agent.instance.transaction_sampler.ignore_transaction
|
60
|
+
end
|
61
|
+
# This needs to be called after entering the call to trace the controller action, otherwise
|
62
|
+
# the controller action blames itself. It gets reset in the normal #pop call.
|
63
|
+
def start_transaction
|
64
|
+
NewRelic::Agent.instance.stats_engine.start_transaction metric_name
|
65
|
+
# Only push the transaction context info once, on entry:
|
66
|
+
if @path_stack.size == 1
|
67
|
+
NewRelic::Agent.instance.transaction_sampler.notice_transaction(metric_name, available_request, filtered_params)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def category
|
72
|
+
@path_stack.last.first
|
73
|
+
end
|
74
|
+
def path
|
75
|
+
@path_stack.last.last
|
76
|
+
end
|
77
|
+
|
78
|
+
def pop
|
79
|
+
category, path = @path_stack.pop
|
80
|
+
if category.nil?
|
81
|
+
NewRelic::Control.instance.log.error "Underflow in metric frames: #{caller.join("\n ")}"
|
82
|
+
end
|
83
|
+
# change the transaction name back to whatever was on the stack.
|
84
|
+
if @path_stack.empty?
|
85
|
+
Thread.current[:newrelic_metric_frame] = nil
|
86
|
+
if NewRelic::Agent.is_execution_traced?
|
87
|
+
cpu_burn = nil
|
88
|
+
if @process_cpu_start
|
89
|
+
cpu_burn = process_cpu - @process_cpu_start
|
90
|
+
elsif @jruby_cpu_start
|
91
|
+
cpu_burn = jruby_cpu_time - @jruby_cpu_start
|
92
|
+
NewRelic::Agent.get_stats_no_scope(NewRelic::Metrics::USER_TIME).record_data_point(cpu_burn)
|
93
|
+
end
|
94
|
+
NewRelic::Agent.instance.transaction_sampler.notice_transaction_cpu_time(cpu_burn) if cpu_burn
|
95
|
+
NewRelic::Agent.instance.histogram.process(Time.now.to_f - start) if recording_web_transaction?(category)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
NewRelic::Agent.instance.stats_engine.scope_name = metric_name
|
99
|
+
end
|
100
|
+
|
101
|
+
# If we have an active metric frame, notice the error and increment the error metric.
|
102
|
+
def self.notice_error(e, custom_params={})
|
103
|
+
if current
|
104
|
+
current.notice_error(e, custom_params)
|
105
|
+
else
|
106
|
+
NewRelic::Agent.instance.error_collector.notice_error(e, nil, nil, custom_params)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def notice_error(e, custom_params={})
|
111
|
+
if exception != e
|
112
|
+
NewRelic::Agent.instance.error_collector.notice_error(e, nil, metric_name, filtered_params.merge(custom_params))
|
113
|
+
self.exception = e
|
114
|
+
end
|
115
|
+
end
|
116
|
+
def record_apdex
|
117
|
+
return unless recording_web_transaction?
|
118
|
+
ending = Time.now.to_f
|
119
|
+
summary_stat = NewRelic::Agent.instance.stats_engine.get_custom_stats("Apdex", NewRelic::ApdexStats)
|
120
|
+
controller_stat = NewRelic::Agent.instance.stats_engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
|
121
|
+
update_apdex(summary_stat, ending - apdex_start, exception)
|
122
|
+
update_apdex(controller_stat, ending - start, exception)
|
123
|
+
end
|
124
|
+
|
125
|
+
def metric_name
|
126
|
+
return nil if @path_stack.empty?
|
127
|
+
category + '/' + path
|
128
|
+
end
|
129
|
+
|
130
|
+
# Return the array of metrics to record for the current metric frame.
|
131
|
+
def recorded_metrics
|
132
|
+
metrics = [ metric_name ]
|
133
|
+
if @path_stack.size == 1
|
134
|
+
if recording_web_transaction?
|
135
|
+
metrics += ["Controller", "HttpDispatcher"]
|
136
|
+
else
|
137
|
+
metrics += ["#{category}/all", "OtherTransaction/all"]
|
138
|
+
end
|
139
|
+
end
|
140
|
+
metrics
|
141
|
+
end
|
142
|
+
|
143
|
+
# Yield to a block that is run with a database metric name context. This means
|
144
|
+
# the Database instrumentation will use this for the metric name if it does not
|
145
|
+
# otherwise know about a model. This is re-entrant.
|
146
|
+
#
|
147
|
+
# * <tt>model</tt> is the DB model class
|
148
|
+
# * <tt>method</tt> is the name of the finder method or other method to identify the operation with.
|
149
|
+
#
|
150
|
+
def with_database_metric_name(model, method)
|
151
|
+
previous = @database_metric_name
|
152
|
+
model_name = case model
|
153
|
+
when Class
|
154
|
+
model.name
|
155
|
+
when String
|
156
|
+
model
|
157
|
+
else
|
158
|
+
model.to_s
|
159
|
+
end
|
160
|
+
@database_metric_name = "ActiveRecord/#{model_name}/#{method}"
|
161
|
+
yield
|
162
|
+
ensure
|
163
|
+
@database_metric_name=previous
|
164
|
+
end
|
165
|
+
|
166
|
+
private
|
167
|
+
|
168
|
+
def recording_web_transaction?(cat = category)
|
169
|
+
0 == cat.index("Controller")
|
170
|
+
end
|
171
|
+
|
172
|
+
def update_apdex(stat, duration, failed)
|
173
|
+
apdex_t = NewRelic::Control.instance.apdex_t
|
174
|
+
case
|
175
|
+
when failed
|
176
|
+
stat.record_apdex_f
|
177
|
+
when duration <= apdex_t
|
178
|
+
stat.record_apdex_s
|
179
|
+
when duration <= 4 * apdex_t
|
180
|
+
stat.record_apdex_t
|
181
|
+
else
|
182
|
+
stat.record_apdex_f
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def process_cpu
|
187
|
+
return nil if defined? JRuby
|
188
|
+
p = Process.times
|
189
|
+
p.stime + p.utime
|
190
|
+
end
|
191
|
+
|
192
|
+
def jruby_cpu_time # :nodoc:
|
193
|
+
return nil unless @@java_classes_loaded
|
194
|
+
threadMBean = ManagementFactory.getThreadMXBean()
|
195
|
+
java_utime = threadMBean.getCurrentThreadUserTime() # ns
|
196
|
+
-1 == java_utime ? 0.0 : java_utime/1e9
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
if defined? Net::HTTP
|
2
|
+
Net::HTTP.class_eval do
|
3
|
+
def request_with_newrelic_trace(*args, &block)
|
4
|
+
if Thread::current[:newrelic_scope_name].nil?
|
5
|
+
self.class.trace_execution_unscoped(["External/#{@address}/Net::HTTP/#{args[0].method}",
|
6
|
+
"External/#{@address}/all",
|
7
|
+
"External/allOther"]) do
|
8
|
+
request_without_newrelic_trace(*args, &block)
|
9
|
+
end
|
10
|
+
else
|
11
|
+
self.class.trace_execution_scoped(["External/#{@address}/Net::HTTP/#{args[0].method}",
|
12
|
+
"External/#{@address}/all",
|
13
|
+
"External/allWeb"]) do
|
14
|
+
request_without_newrelic_trace(*args, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
alias request_without_newrelic_trace request
|
19
|
+
alias request request_with_newrelic_trace
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
if defined?(PhusionPassenger)
|
2
|
+
NewRelic::Control.instance.log.debug "Installing Passenger event hooks."
|
3
|
+
|
4
|
+
PhusionPassenger.on_event(:stopping_worker_process) do
|
5
|
+
NewRelic::Control.instance.log.info "Passenger stopping this process, shutdown the agent."
|
6
|
+
NewRelic::Agent.instance.shutdown
|
7
|
+
end
|
8
|
+
|
9
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
10
|
+
if forked
|
11
|
+
# We want to reset the stats from the stats engine in case any carried
|
12
|
+
# over into the spawned process. Don't clear them in case any were
|
13
|
+
# cached.
|
14
|
+
NewRelic::Agent.instance.stats_engine.reset_stats
|
15
|
+
else
|
16
|
+
# We're in conservative spawning mode. We don't need to do anything.
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -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
|
+
# You should avoid instrumenting nested calls. So if you are using Rack::Cascade
|
56
|
+
# to fall through the action, or you are chaining through to the next middleware
|
57
|
+
# which will have it's own controller instrumentation, then you will want 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_env
|
81
|
+
end
|
82
|
+
def call_with_newrelic(*args)
|
83
|
+
@newrelic_env = args.first
|
84
|
+
perform_action_with_newrelic_trace(:category => :rack, :params => {:uri => @newrelic_env['REQUEST_PATH']}) 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
|