genki-newrelic_rpm 2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +316 -0
- data/LICENSE +37 -0
- data/Manifest +156 -0
- data/README.md +138 -0
- data/Rakefile +22 -0
- data/bin/mongrel_rpm +33 -0
- data/bin/newrelic_cmd +4 -0
- data/cert/cacert.pem +34 -0
- data/genki-newrelic_rpm.gemspec +32 -0
- data/init.rb +38 -0
- data/install.rb +37 -0
- data/lib/new_relic/agent.rb +280 -0
- data/lib/new_relic/agent/agent.rb +627 -0
- data/lib/new_relic/agent/chained_call.rb +13 -0
- data/lib/new_relic/agent/collection_helper.rb +61 -0
- data/lib/new_relic/agent/error_collector.rb +125 -0
- data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
- data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +83 -0
- data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
- data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +368 -0
- data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
- data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +132 -0
- data/lib/new_relic/agent/instrumentation/memcache.rb +21 -0
- data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
- data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
- data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
- data/lib/new_relic/agent/instrumentation/net.rb +12 -0
- data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
- data/lib/new_relic/agent/instrumentation/rack.rb +77 -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/dispatcher.rb +38 -0
- data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
- data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
- data/lib/new_relic/agent/method_tracer.rb +277 -0
- data/lib/new_relic/agent/patch_const_missing.rb +125 -0
- data/lib/new_relic/agent/sampler.rb +12 -0
- data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
- data/lib/new_relic/agent/samplers/memory_sampler.rb +137 -0
- data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
- data/lib/new_relic/agent/shim_agent.rb +21 -0
- data/lib/new_relic/agent/stats_engine.rb +24 -0
- data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
- data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
- data/lib/new_relic/agent/stats_engine/transactions.rb +155 -0
- data/lib/new_relic/agent/transaction_sampler.rb +319 -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 +436 -0
- data/lib/new_relic/control/external.rb +13 -0
- data/lib/new_relic/control/merb.rb +22 -0
- data/lib/new_relic/control/rails.rb +143 -0
- data/lib/new_relic/control/ruby.rb +34 -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 +285 -0
- data/lib/new_relic/merbtasks.rb +6 -0
- data/lib/new_relic/metric_data.rb +44 -0
- data/lib/new_relic/metric_parser.rb +120 -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/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 +52 -0
- data/lib/new_relic/metrics.rb +7 -0
- data/lib/new_relic/noticed_error.rb +25 -0
- data/lib/new_relic/rack/metric_app.rb +56 -0
- data/lib/new_relic/rack/newrelic.ru +25 -0
- data/lib/new_relic/rack/newrelic.yml +25 -0
- data/lib/new_relic/rack_app.rb +5 -0
- data/lib/new_relic/recipes.rb +82 -0
- data/lib/new_relic/stats.rb +360 -0
- data/lib/new_relic/transaction_analysis.rb +121 -0
- data/lib/new_relic/transaction_sample.rb +583 -0
- data/lib/new_relic/version.rb +54 -0
- data/lib/new_relic_api.rb +315 -0
- data/lib/newrelic_rpm.rb +40 -0
- data/lib/tasks/all.rb +4 -0
- data/lib/tasks/install.rake +7 -0
- data/lib/tasks/tests.rake +13 -0
- data/newrelic.yml +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 +39 -0
- data/test/new_relic/agent/active_record_instrumentation_test.rb +234 -0
- data/test/new_relic/agent/agent_controller_test.rb +107 -0
- data/test/new_relic/agent/agent_test.rb +117 -0
- data/test/new_relic/agent/agent_test_controller.rb +44 -0
- data/test/new_relic/agent/classloader_patch_test.rb +56 -0
- data/test/new_relic/agent/collection_helper_test.rb +118 -0
- data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
- data/test/new_relic/agent/error_collector_test.rb +155 -0
- data/test/new_relic/agent/method_tracer_test.rb +335 -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 +51 -0
- data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
- data/test/new_relic/agent/stats_engine/samplers_test.rb +78 -0
- data/test/new_relic/agent/stats_engine/stats_engine_test.rb +177 -0
- data/test/new_relic/agent/task_instrumentation_test.rb +67 -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 +146 -0
- data/test/new_relic/agent/transaction_sampler_test.rb +387 -0
- data/test/new_relic/agent/worker_loop_test.rb +103 -0
- data/test/new_relic/control_test.rb +94 -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 +74 -0
- data/test/test_helper.rb +38 -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 +214 -0
- data/ui/helpers/google_pie_chart.rb +55 -0
- data/ui/helpers/newrelic_helper.rb +314 -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 +15 -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/textmate.png +0 -0
- data/ui/views/newrelic/index.rhtml +45 -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 +77 -0
- data/ui/views/newrelic/show_source.rhtml +3 -0
- data/ui/views/newrelic/stylesheets/style.css +433 -0
- data/ui/views/newrelic/threads.rhtml +52 -0
- metadata +327 -0
@@ -0,0 +1,61 @@
|
|
1
|
+
module NewRelic::Agent::CollectionHelper
|
2
|
+
# Transform parameter hash into a hash whose values are strictly
|
3
|
+
# strings
|
4
|
+
def normalize_params(params)
|
5
|
+
case params
|
6
|
+
when Symbol, FalseClass, TrueClass, nil
|
7
|
+
params
|
8
|
+
when Numeric
|
9
|
+
truncate(params.to_s)
|
10
|
+
when String
|
11
|
+
truncate(params)
|
12
|
+
when Hash
|
13
|
+
new_params = {}
|
14
|
+
params.each do | key, value |
|
15
|
+
new_params[truncate(normalize_params(key),32)] = normalize_params(value)
|
16
|
+
end
|
17
|
+
new_params
|
18
|
+
when Array
|
19
|
+
params.first(20).map{|item| normalize_params(item)}
|
20
|
+
else
|
21
|
+
truncate(flatten(params))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Return an array of strings (backtrace), cleaned up for readability
|
26
|
+
# Return nil if there is no backtrace
|
27
|
+
|
28
|
+
def strip_nr_from_backtrace(backtrace)
|
29
|
+
if backtrace
|
30
|
+
# this is for 1.9.1, where strings no longer have Enumerable
|
31
|
+
backtrace = backtrace.split("\n") if String === backtrace
|
32
|
+
# strip newrelic from the trace
|
33
|
+
backtrace = backtrace.reject {|line| line =~ /new_relic\/agent\// }
|
34
|
+
# rename methods back to their original state
|
35
|
+
backtrace = backtrace.collect {|line| line.gsub(/_without_(newrelic|trace)/, "")}
|
36
|
+
end
|
37
|
+
backtrace
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Convert any kind of object to a short string.
|
43
|
+
def flatten(object)
|
44
|
+
s = case object
|
45
|
+
when nil then ''
|
46
|
+
when object.instance_of?(String) then object
|
47
|
+
when String then String.new(object) # convert string subclasses to strings
|
48
|
+
else "#<#{object.class.to_s}>"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
def truncate(string, len=256)
|
52
|
+
case string
|
53
|
+
when Symbol then string
|
54
|
+
when nil then ""
|
55
|
+
when String
|
56
|
+
string.to_s.gsub(/^(.{#{len}})(.*)/) {$2.blank? ? $1 : $1 + "..."}
|
57
|
+
else
|
58
|
+
truncate(flatten(string), len)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
|
2
|
+
module NewRelic::Agent
|
3
|
+
class ErrorCollector
|
4
|
+
include CollectionHelper
|
5
|
+
|
6
|
+
# Defined the methods that need to be stubbed out when the
|
7
|
+
# agent is disabled
|
8
|
+
module Shim #:nodoc:
|
9
|
+
def notice_error(*args); end
|
10
|
+
end
|
11
|
+
|
12
|
+
MAX_ERROR_QUEUE_LENGTH = 20 unless defined? MAX_ERROR_QUEUE_LENGTH
|
13
|
+
|
14
|
+
attr_accessor :enabled
|
15
|
+
|
16
|
+
def initialize(agent = nil)
|
17
|
+
@agent = agent
|
18
|
+
@errors = []
|
19
|
+
# lookup of exception class names to ignore. Hash for fast access
|
20
|
+
@ignore = {}
|
21
|
+
@ignore_filter = nil
|
22
|
+
|
23
|
+
config = NewRelic::Control.instance.fetch('error_collector', {})
|
24
|
+
|
25
|
+
@enabled = config.fetch('enabled', true)
|
26
|
+
@capture_source = config.fetch('capture_source', true)
|
27
|
+
|
28
|
+
ignore_errors = config.fetch('ignore_errors', "")
|
29
|
+
ignore_errors = ignore_errors.split(",")
|
30
|
+
ignore_errors.each { |error| error.strip! }
|
31
|
+
ignore(ignore_errors)
|
32
|
+
@lock = Mutex.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def ignore_error_filter(&block)
|
36
|
+
@ignore_filter = block
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# errors is an array of Exception Class Names
|
41
|
+
#
|
42
|
+
def ignore(errors)
|
43
|
+
errors.each { |error| @ignore[error] = true; log.debug("Ignoring error: '#{error}'") }
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def notice_error(exception, request=nil, action_path=nil, filtered_params={})
|
48
|
+
|
49
|
+
return unless @enabled
|
50
|
+
return if @ignore[exception.class.name]
|
51
|
+
|
52
|
+
if @ignore_filter
|
53
|
+
exception = @ignore_filter.call(exception)
|
54
|
+
|
55
|
+
return if exception.nil?
|
56
|
+
end
|
57
|
+
|
58
|
+
error_stat.increment_count
|
59
|
+
|
60
|
+
data = {}
|
61
|
+
|
62
|
+
action_path ||= ''
|
63
|
+
|
64
|
+
data[:request_params] = normalize_params(filtered_params) if NewRelic::Control.instance.capture_params
|
65
|
+
|
66
|
+
data[:custom_params] = normalize_params(@agent.custom_params) if @agent
|
67
|
+
|
68
|
+
data[:request_uri] = request.path if request
|
69
|
+
data[:request_uri] ||= ""
|
70
|
+
|
71
|
+
data[:request_referer] = request.referer if request
|
72
|
+
data[:request_referer] ||= ""
|
73
|
+
|
74
|
+
data[:rails_root] = NewRelic::Control.instance.root
|
75
|
+
|
76
|
+
data[:file_name] = exception.file_name if exception.respond_to?('file_name')
|
77
|
+
data[:line_number] = exception.line_number if exception.respond_to?('line_number')
|
78
|
+
|
79
|
+
if @capture_source && exception.respond_to?('source_extract')
|
80
|
+
data[:source] = exception.source_extract
|
81
|
+
end
|
82
|
+
|
83
|
+
if exception.respond_to? 'original_exception'
|
84
|
+
inside_exception = exception.original_exception
|
85
|
+
else
|
86
|
+
inside_exception = exception
|
87
|
+
end
|
88
|
+
|
89
|
+
data[:stack_trace] = inside_exception.backtrace
|
90
|
+
|
91
|
+
noticed_error = NewRelic::NoticedError.new(action_path, data, exception)
|
92
|
+
|
93
|
+
@lock.synchronize do
|
94
|
+
if @errors.length >= MAX_ERROR_QUEUE_LENGTH
|
95
|
+
log.info("The error reporting queue has reached #{MAX_ERROR_QUEUE_LENGTH}. This error will not be reported to RPM: #{exception.message}")
|
96
|
+
else
|
97
|
+
@errors << noticed_error
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Get the errors currently queued up. Unsent errors are left
|
103
|
+
# over from a previous unsuccessful attempt to send them to the server.
|
104
|
+
# We first clear out all unsent errors before sending the newly queued errors.
|
105
|
+
def harvest_errors(unsent_errors)
|
106
|
+
if unsent_errors && !unsent_errors.empty?
|
107
|
+
return unsent_errors
|
108
|
+
else
|
109
|
+
@lock.synchronize do
|
110
|
+
errors = @errors
|
111
|
+
@errors = []
|
112
|
+
return errors
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
def error_stat
|
119
|
+
@error_stat ||= NewRelic::Agent.get_stats("Errors/all")
|
120
|
+
end
|
121
|
+
def log
|
122
|
+
NewRelic::Control.instance.log
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# ActiveMerchant Instrumentation.
|
2
|
+
|
3
|
+
if defined? ActiveMerchant
|
4
|
+
|
5
|
+
ActiveMerchant::Billing::Gateway.implementations.each do |gateway|
|
6
|
+
gateway.class_eval do
|
7
|
+
implemented_methods = public_instance_methods(false)
|
8
|
+
gateway_name = self.name.split('::').last
|
9
|
+
[:authorize, :purchase, :credit, :void, :capture, :recurring].each do |operation|
|
10
|
+
if implemented_methods.include?(operation.to_s)
|
11
|
+
add_method_tracer operation, "ActiveMerchant/gateway/#{gateway_name}/#{operation}", :scoped_metric_only => true
|
12
|
+
add_method_tracer operation, "ActiveMerchant/gateway/#{gateway_name}", :push_scope => false
|
13
|
+
add_method_tracer operation, "ActiveMerchant/operation/#{operation}", :push_scope => false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
|
2
|
+
# NewRelic instrumentation for ActiveRecord
|
3
|
+
if defined?(ActiveRecord) && defined?(ActiveRecord::Base) && !NewRelic::Control.instance['skip_ar_instrumentation']
|
4
|
+
|
5
|
+
module NewRelic
|
6
|
+
module Agent
|
7
|
+
module Instrumentation
|
8
|
+
module ActiveRecordInstrumentation
|
9
|
+
|
10
|
+
def self.included(instrumented_class)
|
11
|
+
instrumented_class.class_eval do
|
12
|
+
alias_method :log_without_newrelic_instrumentation, :log
|
13
|
+
alias_method :log, :log_with_newrelic_instrumentation
|
14
|
+
protected :log
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def log_with_newrelic_instrumentation(sql, name, &block)
|
19
|
+
|
20
|
+
return log_without_newrelic_instrumentation(sql, name, &block) unless NewRelic::Agent.is_execution_traced?
|
21
|
+
|
22
|
+
# Capture db config if we are going to try to get the explain plans
|
23
|
+
if (defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter) && self.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)) ||
|
24
|
+
(defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && self.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter))
|
25
|
+
supported_config = @config
|
26
|
+
end
|
27
|
+
if name && (parts = name.split " ") && parts.size == 2
|
28
|
+
model = parts.first
|
29
|
+
operation = parts.last.downcase
|
30
|
+
metric_name = case operation
|
31
|
+
when 'load' then 'find'
|
32
|
+
when 'indexes', 'columns' then nil # fall back to DirectSQL
|
33
|
+
when 'destroy', 'find', 'save', 'create' then operation
|
34
|
+
when 'update' then 'save'
|
35
|
+
else
|
36
|
+
if model == 'Join'
|
37
|
+
operation
|
38
|
+
end
|
39
|
+
end
|
40
|
+
metric = "ActiveRecord/#{model}/#{metric_name}" if metric_name
|
41
|
+
end
|
42
|
+
if metric.nil? && sql =~ /^(select|update|insert|delete)/i
|
43
|
+
# Could not determine the model/operation so let's find a better
|
44
|
+
# metric. If it doesn't match the regex, it's probably a show
|
45
|
+
# command or some DDL which we'll ignore.
|
46
|
+
metric = "Database/SQL/#{$1.downcase}"
|
47
|
+
end
|
48
|
+
|
49
|
+
if !metric
|
50
|
+
log_without_newrelic_instrumentation(sql, name, &block)
|
51
|
+
else
|
52
|
+
metrics = [metric, "ActiveRecord/all"]
|
53
|
+
metrics << "ActiveRecord/#{metric_name}" if metric_name
|
54
|
+
self.class.trace_execution_scoped(metrics) do
|
55
|
+
t0 = Time.now.to_f
|
56
|
+
begin
|
57
|
+
log_without_newrelic_instrumentation(sql, name, &block)
|
58
|
+
ensure
|
59
|
+
NewRelic::Agent.instance.transaction_sampler.notice_sql(sql, supported_config, Time.now.to_f - t0)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
# instrumentation to catch logged SQL statements in sampled transactions
|
68
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
|
69
|
+
include ::NewRelic::Agent::Instrumentation::ActiveRecordInstrumentation
|
70
|
+
end
|
71
|
+
|
72
|
+
# This instrumentation will add an extra scope to the transaction traces
|
73
|
+
# which will show the code surrounding the query, inside the model find_by_sql
|
74
|
+
# method.
|
75
|
+
ActiveRecord::Base.class_eval do
|
76
|
+
class << self
|
77
|
+
add_method_tracer :find_by_sql, 'ActiveRecord/#{self.name}/find_by_sql', :metric => false
|
78
|
+
end
|
79
|
+
end unless NewRelic::Control.instance['disable_ar_instrumentation']
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,368 @@
|
|
1
|
+
|
2
|
+
module NewRelic::Agent::Instrumentation
|
3
|
+
# == NewRelic instrumentation for controllers
|
4
|
+
#
|
5
|
+
# This instrumentation is applied to the action controller by default if the agent
|
6
|
+
# is actively collecting statistics. It will collect statistics for the
|
7
|
+
# given action.
|
8
|
+
#
|
9
|
+
# In cases where you don't want to instrument the top level action, but instead
|
10
|
+
# have other methods which are dispatched to by your action, and you want to treat
|
11
|
+
# these as distinct actions, then what you need to do is use
|
12
|
+
# #perform_action_with_newrelic_trace
|
13
|
+
#
|
14
|
+
module ControllerInstrumentation
|
15
|
+
|
16
|
+
if defined? JRuby
|
17
|
+
@@newrelic_java_classes_missing = false
|
18
|
+
begin
|
19
|
+
require 'java'
|
20
|
+
include_class 'java.lang.management.ManagementFactory'
|
21
|
+
include_class 'com.sun.management.OperatingSystemMXBean'
|
22
|
+
rescue
|
23
|
+
@@newrelic_java_classes_missing = true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.included(clazz) # :nodoc:
|
28
|
+
clazz.extend(ClassMethods)
|
29
|
+
end
|
30
|
+
|
31
|
+
# This module is for importing stubs when the agent is disabled
|
32
|
+
module ClassMethodsShim # :nodoc:
|
33
|
+
def newrelic_ignore(*args); end
|
34
|
+
def newrelic_ignore_apdex(*args); end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Shim # :nodoc:
|
38
|
+
def self.included(clazz)
|
39
|
+
clazz.extend(ClassMethodsShim)
|
40
|
+
end
|
41
|
+
def newrelic_notice_error(*args); end
|
42
|
+
def new_relic_trace_controller_action(*args); yield; end
|
43
|
+
def newrelic_metric_path; end
|
44
|
+
def perform_action_with_newrelic_trace(*args); yield; end
|
45
|
+
end
|
46
|
+
|
47
|
+
module ClassMethods
|
48
|
+
# Have NewRelic ignore actions in this controller. Specify the actions as hash options
|
49
|
+
# using :except and :only. If no actions are specified, all actions are ignored.
|
50
|
+
def newrelic_ignore(specifiers={})
|
51
|
+
newrelic_ignore_aspect('do_not_trace', specifiers)
|
52
|
+
end
|
53
|
+
# Have NewRelic omit apdex measurements on the given actions. Typically used for
|
54
|
+
# actions that are not user facing or that skew your overall apdex measurement.
|
55
|
+
# Accepts :except and :only options, as with #newrelic_ignore.
|
56
|
+
def newrelic_ignore_apdex(specifiers={})
|
57
|
+
newrelic_ignore_aspect('ignore_apdex', specifiers)
|
58
|
+
end
|
59
|
+
|
60
|
+
def newrelic_ignore_aspect(property, specifiers={}) # :nodoc:
|
61
|
+
if specifiers.empty?
|
62
|
+
self.newrelic_write_attr property, true
|
63
|
+
elsif ! (Hash === specifiers)
|
64
|
+
logger.error "newrelic_#{property} takes an optional hash with :only and :except lists of actions (illegal argument type '#{specifiers.class}')"
|
65
|
+
else
|
66
|
+
self.newrelic_write_attr property, specifiers
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Should be monkey patched into the controller class implemented
|
71
|
+
# with the inheritable attribute mechanism.
|
72
|
+
def newrelic_write_attr(attr_name, value) # :nodoc:
|
73
|
+
instance_variable_set "@#{attr_name}", value
|
74
|
+
end
|
75
|
+
def newrelic_read_attr(attr_name) # :nodoc:
|
76
|
+
instance_variable_get "@#{attr_name}"
|
77
|
+
end
|
78
|
+
|
79
|
+
# Add transaction tracing to the given method. This will treat
|
80
|
+
# the given method as a main entrypoint for instrumentation, just
|
81
|
+
# like controller actions are treated by default. Useful especially
|
82
|
+
# for background tasks.
|
83
|
+
#
|
84
|
+
# Example for background job:
|
85
|
+
# class Job
|
86
|
+
# include NewRelic::Agent::Instrumentation::ControllerInstrumentation
|
87
|
+
# def run(task)
|
88
|
+
# ...
|
89
|
+
# end
|
90
|
+
# # Instrument run so tasks show up under task.name. Note single
|
91
|
+
# # quoting to defer eval to runtime.
|
92
|
+
# add_transaction_tracer :run, :name => '#{args[0].name}'
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# Here's an example of a controller that uses a dispatcher
|
96
|
+
# action to invoke operations which you want treated as top
|
97
|
+
# level actions, so they aren't all lumped into the invoker
|
98
|
+
# action.
|
99
|
+
#
|
100
|
+
# MyController < ActionController::Base
|
101
|
+
# include NewRelic::Agent::Instrumentation::ControllerInstrumentation
|
102
|
+
# # dispatch the given op to the method given by the service parameter.
|
103
|
+
# def invoke_operation
|
104
|
+
# op = params['operation']
|
105
|
+
# send op
|
106
|
+
# end
|
107
|
+
# # Ignore the invoker to avoid double counting
|
108
|
+
# newrelic_ignore :only => 'invoke_operation'
|
109
|
+
# # Instrument the operations:
|
110
|
+
# add_transaction_tracer :print
|
111
|
+
# add_transaction_tracer :show
|
112
|
+
# add_transaction_tracer :forward
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
# See NewRelic::Agent::Instrumentation::ControllerInstrumentation#perform_action_with_newrelic_trace
|
116
|
+
# for the full list of available options.
|
117
|
+
#
|
118
|
+
def add_transaction_tracer(method, options={})
|
119
|
+
# The metric path:
|
120
|
+
options[:name] ||= method.to_s
|
121
|
+
options_arg = []
|
122
|
+
options.each do |key, value|
|
123
|
+
options_arg << %Q[:#{key} => #{value.is_a?(Symbol) ? value.inspect : %Q["#{value.to_s}"]}]
|
124
|
+
end
|
125
|
+
class_eval <<-EOC
|
126
|
+
def #{method.to_s}_with_newrelic_transaction_trace(*args, &block)
|
127
|
+
NewRelic::Agent::Instrumentation::DispatcherInstrumentation.newrelic_dispatcher_start
|
128
|
+
perform_action_with_newrelic_trace(#{options_arg.join(',')}) do
|
129
|
+
#{method.to_s}_without_newrelic_transaction_trace(*args, &block)
|
130
|
+
end
|
131
|
+
ensure
|
132
|
+
NewRelic::Agent::Instrumentation::DispatcherInstrumentation.newrelic_dispatcher_finish
|
133
|
+
end
|
134
|
+
EOC
|
135
|
+
alias_method "#{method.to_s}_without_newrelic_transaction_trace", method.to_s
|
136
|
+
alias_method method.to_s, "#{method.to_s}_with_newrelic_transaction_trace"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Must be implemented in the controller class:
|
141
|
+
# Determine the path that is used in the metric name for
|
142
|
+
# the called controller action. Of the form controller_path/action_name
|
143
|
+
#
|
144
|
+
def newrelic_metric_path(action_name_override = nil) # :nodoc:
|
145
|
+
raise "Not implemented!"
|
146
|
+
end
|
147
|
+
|
148
|
+
# Yield to the given block with NewRelic tracing. Used by
|
149
|
+
# default instrumentation on controller actions in Rails and Merb.
|
150
|
+
# But it can also be used in custom instrumentation of controller
|
151
|
+
# methods and background tasks.
|
152
|
+
#
|
153
|
+
# Here's a more verbose version of the example shown in
|
154
|
+
# <tt>ClassMethods#add_transaction_tracer</tt> using this method instead of
|
155
|
+
# #add_transaction_tracer.
|
156
|
+
#
|
157
|
+
# Below is a controller with an +invoke_operation+ action which
|
158
|
+
# dispatches to more specific operation methods based on a
|
159
|
+
# parameter (very dangerous, btw!). With this instrumentation,
|
160
|
+
# the +invoke_operation+ action is ignored but the operation
|
161
|
+
# methods show up in RPM as if they were first class controller
|
162
|
+
# actions
|
163
|
+
#
|
164
|
+
# MyController < ActionController::Base
|
165
|
+
# include NewRelic::Agent::Instrumentation::ControllerInstrumentation
|
166
|
+
# # dispatch the given op to the method given by the service parameter.
|
167
|
+
# def invoke_operation
|
168
|
+
# op = params['operation']
|
169
|
+
# perform_action_with_newrelic_trace(:name => op) do
|
170
|
+
# send op, params['message']
|
171
|
+
# end
|
172
|
+
# end
|
173
|
+
# # Ignore the invoker to avoid double counting
|
174
|
+
# newrelic_ignore :only => 'invoke_operation'
|
175
|
+
# end
|
176
|
+
#
|
177
|
+
# By passing a block in combination with specific arguments, you can
|
178
|
+
# invoke this directly to capture high level information in
|
179
|
+
# several contexts:
|
180
|
+
#
|
181
|
+
# * Pass <tt>:category => :controller</tt> and <tt>:name => actionname</tt>
|
182
|
+
# to treat the block as if it were a controller action, invoked
|
183
|
+
# inside a real action. <tt>:name</tt> is the name of the method, and is
|
184
|
+
# used in the metric name.
|
185
|
+
#
|
186
|
+
# When invoked directly, pass in a block to measure with some
|
187
|
+
# combination of options:
|
188
|
+
#
|
189
|
+
# * <tt>:category => :controller</tt> indicates that this is a
|
190
|
+
# controller action and will appear with all the other actions. This
|
191
|
+
# is the default.
|
192
|
+
# * <tt>:category => :task</tt> indicates that this is a
|
193
|
+
# background task and will show up in RPM with other background
|
194
|
+
# tasks instead of in the controllers list
|
195
|
+
# * <tt>:category => :rack</tt> if you are instrumenting a rack
|
196
|
+
# middleware call. The <tt>:name</tt> is optional, useful if you
|
197
|
+
# have more than one potential transaction in the #call.
|
198
|
+
# * <tt>:category => :uri</tt> indicates that this is a
|
199
|
+
# web transaction whose name is a normalized URI, where 'normalized'
|
200
|
+
# means the URI does not have any elements with data in them such
|
201
|
+
# as in many REST URIs.
|
202
|
+
# * <tt>:name => action_name</tt> is used to specify the action
|
203
|
+
# name used as part of the metric name
|
204
|
+
# * <tt>:force => true</tt> indicates you should capture all
|
205
|
+
# metrics even if the #newrelic_ignore directive was specified
|
206
|
+
# * <tt>:class_name => aClass.name</tt> is used to override the name
|
207
|
+
# of the class when used inside the metric name. Default is the
|
208
|
+
# current class.
|
209
|
+
# * <tt>:path => metric_path</tt> is *deprecated* in the public API. It
|
210
|
+
# allows you to set the entire metric after the category part. Overrides
|
211
|
+
# all the other options.
|
212
|
+
#
|
213
|
+
# If a single argument is passed in, it is treated as a metric
|
214
|
+
# path. This form is deprecated.
|
215
|
+
def perform_action_with_newrelic_trace(*args)
|
216
|
+
agent = NewRelic::Agent.instance
|
217
|
+
stats_engine = agent.stats_engine
|
218
|
+
|
219
|
+
# Skip instrumentation based on the value of 'do_not_trace' and if
|
220
|
+
# we aren't calling directly with a block.
|
221
|
+
if !block_given? && is_filtered?('do_not_trace')
|
222
|
+
# Tell the dispatcher instrumentation that we ignored this action and it shouldn't
|
223
|
+
# be counted for the overall HTTP operations measurement.
|
224
|
+
Thread.current[:newrelic_ignore_controller] = true
|
225
|
+
# Also ignore all instrumentation in the call sequence
|
226
|
+
NewRelic::Agent.disable_all_tracing do
|
227
|
+
return perform_action_without_newrelic_trace(*args)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
# reset this in case we came through a code path where the top level controller is ignored
|
232
|
+
Thread.current[:newrelic_ignore_controller] = nil
|
233
|
+
apdex_start = (Thread.current[:started_on] || Thread.current[:newrelic_dispatcher_start] || Time.now).to_f
|
234
|
+
force = false
|
235
|
+
category = 'Controller'
|
236
|
+
if block_given? && args.any?
|
237
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
238
|
+
unless path = options[:path]
|
239
|
+
category =
|
240
|
+
case options[:category]
|
241
|
+
when :controller, nil then 'Controller'
|
242
|
+
when :task then 'Controller/Task' # 'Task'
|
243
|
+
when :rack then 'Controller/Rack' #'WebTransaction/Rack'
|
244
|
+
when :uri then 'Controller' #'WebTransaction/Uri'
|
245
|
+
# for internal use only
|
246
|
+
when :sinatra then 'Controller/Sinatra' #'WebTransaction/Uri'
|
247
|
+
else options[:category].to_s.capitalize
|
248
|
+
end
|
249
|
+
# To be consistent with the ActionController::Base#controller_path used in rails to determine the
|
250
|
+
# metric path, we drop the controller off the end of the path if there is one.
|
251
|
+
action = options[:name] || args.first
|
252
|
+
force = options[:force]
|
253
|
+
metric_class = options[:class_name] || (self.is_a?(Class) ? self.name : self.class.name)
|
254
|
+
|
255
|
+
path = metric_class
|
256
|
+
path += ('/' + action) if action
|
257
|
+
end
|
258
|
+
else
|
259
|
+
path = newrelic_metric_path
|
260
|
+
end
|
261
|
+
metric_name = category + '/' + path
|
262
|
+
start = Time.now.to_f
|
263
|
+
agent.ensure_worker_thread_started
|
264
|
+
|
265
|
+
NewRelic::Agent.trace_execution_scoped [metric_name, "Controller"], :force => force do
|
266
|
+
stats_engine.transaction_name = metric_name
|
267
|
+
available_params = self.respond_to?(:params) ? params : {}
|
268
|
+
local_params = (respond_to? :filter_parameters) ? filter_parameters(available_params) : available_params
|
269
|
+
available_request = (respond_to? :request) ? request : nil
|
270
|
+
agent.transaction_sampler.notice_transaction(path, available_request, local_params)
|
271
|
+
|
272
|
+
if newrelic_record_cpu_burn?
|
273
|
+
t = newrelic_cpu_time
|
274
|
+
end
|
275
|
+
|
276
|
+
failed = false
|
277
|
+
|
278
|
+
begin
|
279
|
+
# run the action
|
280
|
+
if block_given?
|
281
|
+
yield
|
282
|
+
else
|
283
|
+
perform_action_without_newrelic_trace(*args)
|
284
|
+
end
|
285
|
+
rescue Exception => e
|
286
|
+
failed = true
|
287
|
+
raise e
|
288
|
+
ensure
|
289
|
+
if NewRelic::Agent.is_execution_traced?
|
290
|
+
if newrelic_record_cpu_burn?
|
291
|
+
cpu_burn = newrelic_cpu_time - t
|
292
|
+
stats_engine.get_stats_no_scope(NewRelic::Metrics::USER_TIME).record_data_point(cpu_burn)
|
293
|
+
agent.transaction_sampler.notice_transaction_cpu_time(cpu_burn)
|
294
|
+
end
|
295
|
+
# do the apdex bucketing
|
296
|
+
#
|
297
|
+
unless is_filtered?('ignore_apdex')
|
298
|
+
ending = Time.now.to_f
|
299
|
+
# this uses the start of the dispatcher or the mongrel
|
300
|
+
# thread: causes apdex to show too little capacity
|
301
|
+
apdex_overall(apdex_start, ending, failed)
|
302
|
+
# this uses the start time of the controller action:
|
303
|
+
# does not include capacity problems since those aren't
|
304
|
+
# per controller
|
305
|
+
apdex_controller(start, ending, failed, path)
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
310
|
+
ensure
|
311
|
+
# clear out the name of the traced transaction under all circumstances
|
312
|
+
stats_engine.transaction_name = nil
|
313
|
+
end
|
314
|
+
|
315
|
+
private
|
316
|
+
def apdex_overall_stat
|
317
|
+
NewRelic::Agent.instance.stats_engine.get_custom_stats("Apdex", NewRelic::ApdexStats)
|
318
|
+
end
|
319
|
+
|
320
|
+
def apdex_overall(start, ending, failed)
|
321
|
+
record_apdex(apdex_overall_stat, (ending - start), failed)
|
322
|
+
end
|
323
|
+
|
324
|
+
def apdex_controller(start, ending, failed, path)
|
325
|
+
controller_stat = NewRelic::Agent.instance.stats_engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
|
326
|
+
record_apdex(controller_stat, (ending - start), failed)
|
327
|
+
end
|
328
|
+
|
329
|
+
def record_apdex(stat, duration, failed)
|
330
|
+
apdex_t = NewRelic::Control.instance.apdex_t
|
331
|
+
case
|
332
|
+
when failed
|
333
|
+
stat.record_apdex_f
|
334
|
+
when duration <= apdex_t
|
335
|
+
stat.record_apdex_s
|
336
|
+
when duration <= 4 * apdex_t
|
337
|
+
stat.record_apdex_t
|
338
|
+
else
|
339
|
+
stat.record_apdex_f
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
protected
|
344
|
+
|
345
|
+
# Filter out
|
346
|
+
def is_filtered?(key)
|
347
|
+
ignore_actions = self.class.newrelic_read_attr(key) if self.class.respond_to? :newrelic_read_attr
|
348
|
+
case ignore_actions
|
349
|
+
when nil; false
|
350
|
+
when Hash
|
351
|
+
only_actions = Array(ignore_actions[:only])
|
352
|
+
except_actions = Array(ignore_actions[:except])
|
353
|
+
only_actions.include?(action_name.to_sym) || (except_actions.any? && !except_actions.include?(action_name.to_sym))
|
354
|
+
else
|
355
|
+
true
|
356
|
+
end
|
357
|
+
end
|
358
|
+
def newrelic_record_cpu_burn? # :nodoc:
|
359
|
+
defined? JRuby and not @@newrelic_java_classes_missing
|
360
|
+
end
|
361
|
+
def newrelic_cpu_time # :nodoc:
|
362
|
+
threadMBean = ManagementFactory.getThreadMXBean()
|
363
|
+
java_utime = threadMBean.getCurrentThreadUserTime() # ns
|
364
|
+
-1 == java_utime ? 0.0 : java_utime/1e9
|
365
|
+
end
|
366
|
+
|
367
|
+
end
|
368
|
+
end
|