ghazel-newrelic_rpm 3.1.0.1 → 3.4.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +120 -35
- data/LICENSE +29 -2
- data/README.rdoc +2 -2
- data/bin/mongrel_rpm +0 -0
- data/bin/newrelic +0 -0
- data/bin/newrelic_cmd +0 -0
- data/lib/new_relic/agent.rb +50 -38
- data/lib/new_relic/agent/agent.rb +459 -337
- data/lib/new_relic/agent/beacon_configuration.rb +71 -11
- data/lib/new_relic/agent/browser_monitoring.rb +73 -14
- data/lib/new_relic/agent/busy_calculator.rb +11 -3
- data/lib/new_relic/agent/chained_call.rb +2 -2
- data/lib/new_relic/agent/database.rb +223 -0
- data/lib/new_relic/agent/error_collector.rb +231 -183
- data/lib/new_relic/agent/instrumentation.rb +2 -2
- data/lib/new_relic/agent/instrumentation/active_merchant.rb +10 -2
- data/lib/new_relic/agent/instrumentation/active_record.rb +138 -0
- data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +7 -1
- data/lib/new_relic/agent/instrumentation/authlogic.rb +6 -0
- data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +46 -14
- data/lib/new_relic/agent/instrumentation/data_mapper.rb +8 -2
- data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +11 -3
- data/lib/new_relic/agent/instrumentation/memcache.rb +49 -25
- data/lib/new_relic/agent/instrumentation/merb/controller.rb +7 -2
- data/lib/new_relic/agent/instrumentation/merb/errors.rb +7 -1
- data/lib/new_relic/agent/instrumentation/metric_frame.rb +31 -4
- data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +1 -5
- data/lib/new_relic/agent/instrumentation/net.rb +8 -2
- data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +5 -2
- data/lib/new_relic/agent/instrumentation/queue_time.rb +1 -1
- data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +66 -35
- data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +7 -1
- data/lib/new_relic/agent/instrumentation/rails/errors.rb +7 -1
- data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +121 -1
- data/lib/new_relic/agent/instrumentation/rails3/errors.rb +7 -1
- data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +21 -0
- data/lib/new_relic/agent/instrumentation/resque.rb +80 -0
- data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -20
- data/lib/new_relic/agent/instrumentation/sunspot.rb +6 -0
- data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +7 -2
- data/lib/new_relic/agent/method_tracer.rb +205 -99
- data/lib/new_relic/agent/new_relic_service.rb +221 -0
- data/lib/new_relic/agent/pipe_channel_manager.rb +161 -0
- data/lib/new_relic/agent/pipe_service.rb +54 -0
- data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +89 -0
- data/lib/new_relic/agent/samplers/memory_sampler.rb +6 -7
- data/lib/new_relic/agent/shim_agent.rb +5 -5
- data/lib/new_relic/agent/sql_sampler.rb +282 -0
- data/lib/new_relic/agent/stats_engine.rb +2 -0
- data/lib/new_relic/agent/stats_engine/gc_profiler.rb +123 -0
- data/lib/new_relic/agent/stats_engine/metric_stats.rb +35 -30
- data/lib/new_relic/agent/stats_engine/samplers.rb +10 -4
- data/lib/new_relic/agent/stats_engine/transactions.rb +28 -87
- data/lib/new_relic/agent/transaction_info.rb +74 -0
- data/lib/new_relic/agent/transaction_sample_builder.rb +18 -3
- data/lib/new_relic/agent/transaction_sampler.rb +108 -20
- data/lib/new_relic/agent/worker_loop.rb +14 -6
- data/lib/new_relic/collection_helper.rb +19 -11
- data/lib/new_relic/command.rb +1 -1
- data/lib/new_relic/commands/deployments.rb +2 -2
- data/lib/new_relic/commands/install.rb +2 -13
- data/lib/new_relic/control.rb +2 -3
- data/lib/new_relic/control/class_methods.rb +12 -6
- data/lib/new_relic/control/configuration.rb +57 -8
- data/lib/new_relic/control/frameworks.rb +10 -0
- data/lib/new_relic/control/frameworks/external.rb +4 -4
- data/lib/new_relic/control/frameworks/merb.rb +2 -1
- data/lib/new_relic/control/frameworks/rails.rb +35 -22
- data/lib/new_relic/control/frameworks/rails3.rb +12 -7
- data/lib/new_relic/control/frameworks/ruby.rb +5 -5
- data/lib/new_relic/control/frameworks/sinatra.rb +1 -4
- data/lib/new_relic/control/instance_methods.rb +38 -12
- data/lib/new_relic/control/instrumentation.rb +23 -4
- data/lib/new_relic/control/logging_methods.rb +70 -15
- data/lib/new_relic/control/server_methods.rb +22 -9
- data/lib/new_relic/delayed_job_injection.rb +16 -3
- data/lib/new_relic/helper.rb +21 -0
- data/lib/new_relic/language_support.rb +95 -0
- data/lib/new_relic/local_environment.rb +92 -48
- data/lib/new_relic/metric_data.rb +7 -2
- data/lib/new_relic/metric_spec.rb +12 -9
- data/lib/new_relic/noticed_error.rb +6 -1
- data/lib/new_relic/rack/browser_monitoring.rb +18 -19
- data/lib/new_relic/rack/developer_mode.rb +3 -2
- data/lib/new_relic/recipes.rb +8 -4
- data/lib/new_relic/stats.rb +17 -60
- data/lib/new_relic/transaction_analysis.rb +2 -1
- data/lib/new_relic/transaction_analysis/segment_summary.rb +4 -2
- data/lib/new_relic/transaction_sample.rb +60 -75
- data/lib/new_relic/transaction_sample/segment.rb +31 -79
- data/lib/new_relic/version.rb +2 -2
- data/lib/newrelic_rpm.rb +1 -1
- data/newrelic.yml +2 -2
- data/newrelic_rpm.gemspec +46 -54
- data/test/active_record_fixtures.rb +3 -3
- data/test/config/newrelic.yml +1 -1
- data/test/fixtures/proc_cpuinfo.txt +575 -0
- data/test/new_relic/agent/agent/connect_test.rb +128 -25
- data/test/new_relic/agent/agent/start_test.rb +9 -94
- data/test/new_relic/agent/agent/start_worker_thread_test.rb +2 -4
- data/test/new_relic/agent/agent_test.rb +51 -78
- data/test/new_relic/agent/agent_test_controller.rb +1 -1
- data/test/new_relic/agent/agent_test_controller_test.rb +49 -33
- data/test/new_relic/agent/beacon_configuration_test.rb +12 -5
- data/test/new_relic/agent/browser_monitoring_test.rb +99 -50
- data/test/new_relic/agent/database_test.rb +161 -0
- data/test/new_relic/agent/error_collector_test.rb +47 -23
- data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +96 -42
- data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +0 -2
- data/test/new_relic/agent/instrumentation/instrumentation_test.rb +1 -1
- data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +3 -11
- data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +9 -9
- data/test/new_relic/agent/instrumentation/queue_time_test.rb +6 -11
- data/test/new_relic/agent/memcache_instrumentation_test.rb +54 -18
- data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +1 -1
- data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +1 -1
- data/test/new_relic/agent/method_tracer_test.rb +3 -2
- data/test/new_relic/agent/new_relic_service_test.rb +151 -0
- data/test/new_relic/agent/pipe_channel_manager_test.rb +114 -0
- data/test/new_relic/agent/pipe_service_test.rb +113 -0
- data/test/new_relic/agent/rpm_agent_test.rb +4 -31
- data/test/new_relic/agent/sql_sampler_test.rb +192 -0
- data/test/new_relic/agent/stats_engine/metric_stats_test.rb +19 -18
- data/test/new_relic/agent/stats_engine_test.rb +41 -6
- data/test/new_relic/agent/transaction_info_test.rb +13 -0
- data/test/new_relic/agent/transaction_sample_builder_test.rb +27 -4
- data/test/new_relic/agent/transaction_sampler_test.rb +68 -46
- data/test/new_relic/agent/worker_loop_test.rb +3 -3
- data/test/new_relic/agent_test.rb +242 -0
- data/test/new_relic/collection_helper_test.rb +50 -28
- data/test/new_relic/control/configuration_test.rb +77 -0
- data/test/new_relic/control/logging_methods_test.rb +49 -21
- data/test/new_relic/control_test.rb +115 -54
- data/test/new_relic/delayed_job_injection_test.rb +21 -0
- data/test/new_relic/fake_collector.rb +210 -0
- data/test/new_relic/fake_service.rb +44 -0
- data/test/new_relic/local_environment_test.rb +14 -1
- data/test/new_relic/metric_parser/metric_parser_test.rb +11 -0
- data/test/new_relic/rack/browser_monitoring_test.rb +84 -23
- data/test/new_relic/rack/developer_mode_helper_test.rb +141 -0
- data/test/new_relic/rack/developer_mode_test.rb +31 -0
- data/test/new_relic/stats_test.rb +3 -18
- data/test/new_relic/transaction_analysis/segment_summary_test.rb +14 -0
- data/test/new_relic/transaction_analysis_test.rb +3 -3
- data/test/new_relic/transaction_sample/segment_test.rb +15 -80
- data/test/new_relic/transaction_sample_test.rb +25 -18
- data/test/script/build_test_gem.sh +51 -0
- data/test/script/ci.sh +140 -0
- data/test/script/ci_agent-tests_runner.sh +82 -0
- data/test/script/ci_bench.sh +52 -0
- data/test/script/ci_multiverse_runner.sh +63 -0
- data/test/test_contexts.rb +1 -0
- data/test/test_helper.rb +18 -5
- data/ui/helpers/developer_mode_helper.rb +14 -8
- data/ui/helpers/google_pie_chart.rb +0 -1
- data/ui/views/newrelic/index.rhtml +2 -2
- data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +4 -18
- data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +10 -0
- data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +11 -11
- data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +17 -4
- data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +4 -0
- metadata +50 -36
- data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +0 -108
- data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +0 -112
- data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +0 -40
- data/lib/new_relic/data_serialization.rb +0 -84
- data/lib/new_relic/histogram.rb +0 -91
- data/lib/new_relic/rack/metric_app.rb +0 -65
- data/lib/new_relic/rack/mongrel_rpm.ru +0 -28
- data/lib/new_relic/rack/newrelic.yml +0 -27
- data/lib/new_relic/rack_app.rb +0 -6
- data/test/new_relic/data_serialization_test.rb +0 -70
- data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
- data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
- data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
@@ -22,7 +22,7 @@ module NewRelic
|
|
22
22
|
end
|
23
23
|
elsif platform =~ /darwin9/ # 10.5
|
24
24
|
@sampler = ShellPS.new("ps -o rsz")
|
25
|
-
elsif platform =~ /
|
25
|
+
elsif platform =~ /darwin1[01]/ # 10.6 & 10.7
|
26
26
|
@sampler = ShellPS.new("ps -o rss")
|
27
27
|
elsif platform =~ /freebsd/
|
28
28
|
@sampler = ShellPS.new("ps -o rss")
|
@@ -122,18 +122,17 @@ module NewRelic
|
|
122
122
|
# Returns the amount of resident memory this process is using in MB
|
123
123
|
#
|
124
124
|
def get_memory
|
125
|
-
File.open(proc_status_file, "r")
|
126
|
-
|
127
|
-
|
128
|
-
return $1.to_f / 1024.0
|
129
|
-
end
|
130
|
-
end
|
125
|
+
proc_status = File.open(proc_status_file, "r") {|f| f.read_nonblock(4096).strip }
|
126
|
+
if proc_status =~ /RSS:\s*(\d+) kB/i
|
127
|
+
return $1.to_f / 1024.0
|
131
128
|
end
|
132
129
|
raise "Unable to find RSS in #{proc_status_file}"
|
133
130
|
end
|
131
|
+
|
134
132
|
def proc_status_file
|
135
133
|
"/proc/#{$$}/status"
|
136
134
|
end
|
135
|
+
|
137
136
|
def to_s
|
138
137
|
"proc status file sampler: #{proc_status_file}"
|
139
138
|
end
|
@@ -9,19 +9,19 @@ module NewRelic
|
|
9
9
|
end
|
10
10
|
def initialize
|
11
11
|
super
|
12
|
-
@histogram.extend NewRelic::Histogram::Shim
|
13
12
|
@stats_engine.extend NewRelic::Agent::StatsEngine::Shim
|
14
13
|
@stats_engine.extend NewRelic::Agent::StatsEngine::Transactions::Shim
|
15
14
|
@transaction_sampler.extend NewRelic::Agent::TransactionSampler::Shim
|
15
|
+
@sql_sampler.extend NewRelic::Agent::SqlSampler::Shim
|
16
16
|
@error_collector.extend NewRelic::Agent::ErrorCollector::Shim
|
17
17
|
end
|
18
18
|
def after_fork *args; end
|
19
19
|
def start *args; end
|
20
|
-
def shutdown; end
|
20
|
+
def shutdown *args; end
|
21
21
|
def serialize; end
|
22
|
-
def merge_data_from
|
23
|
-
def push_trace_execution_flag
|
24
|
-
def pop_trace_execution_flag
|
22
|
+
def merge_data_from *args; end
|
23
|
+
def push_trace_execution_flag *args; end
|
24
|
+
def pop_trace_execution_flag *args; end
|
25
25
|
def browser_timing_header; "" end
|
26
26
|
def browser_timing_footer; "" end
|
27
27
|
end
|
@@ -0,0 +1,282 @@
|
|
1
|
+
require 'new_relic/agent'
|
2
|
+
require 'new_relic/control'
|
3
|
+
module NewRelic
|
4
|
+
module Agent
|
5
|
+
|
6
|
+
class SqlSampler
|
7
|
+
|
8
|
+
# Module defining methods stubbed out when the agent is disabled
|
9
|
+
module Shim #:nodoc:
|
10
|
+
def notice_scope_empty(*args); end
|
11
|
+
def notice_first_scope_push(*args); end
|
12
|
+
def notice_transaction(*args); end
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :disabled
|
16
|
+
|
17
|
+
# this is for unit tests only
|
18
|
+
attr_reader :sql_traces
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
configure!
|
22
|
+
@sql_traces = {}
|
23
|
+
clear_transaction_data
|
24
|
+
|
25
|
+
# This lock is used to synchronize access to the @last_sample
|
26
|
+
# and related variables. It can become necessary on JRuby or
|
27
|
+
# any 'honest-to-god'-multithreaded system
|
28
|
+
@samples_lock = Mutex.new
|
29
|
+
end
|
30
|
+
|
31
|
+
def configure!
|
32
|
+
@explain_threshold = config.fetch('explain_threshold', 0.5).to_f
|
33
|
+
@explain_enabled = config.fetch('explain_enabled', true)
|
34
|
+
@stack_trace_threshold = config.fetch('stack_trace_threshold',
|
35
|
+
0.5).to_f
|
36
|
+
if config.fetch('enabled', true) &&
|
37
|
+
NewRelic::Control.instance['transaction_tracer'] &&
|
38
|
+
NewRelic::Control.instance['transaction_tracer'].fetch('enabled',
|
39
|
+
true) &&
|
40
|
+
NewRelic::Control.instance.fetch('collect_traces', true)
|
41
|
+
enable
|
42
|
+
else
|
43
|
+
disable
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def config
|
48
|
+
self.class.config
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.config
|
52
|
+
control = NewRelic::Control.instance
|
53
|
+
txn_config = control.fetch('transaction_tracer', {})
|
54
|
+
|
55
|
+
if txn_config.fetch('enabled', true) && control.has_slow_sql_config?
|
56
|
+
txn_config['enabled'] = control['slow_sql']['enabled']
|
57
|
+
end
|
58
|
+
|
59
|
+
txn_config
|
60
|
+
end
|
61
|
+
|
62
|
+
# Enable the sql sampler - this also registers it with
|
63
|
+
# the statistics engine.
|
64
|
+
def enable
|
65
|
+
@disabled = false
|
66
|
+
end
|
67
|
+
|
68
|
+
# Disable the sql sampler - this also deregisters it
|
69
|
+
# with the statistics engine.
|
70
|
+
def disable
|
71
|
+
@disabled = true
|
72
|
+
end
|
73
|
+
|
74
|
+
def enabled?
|
75
|
+
!@disabled
|
76
|
+
end
|
77
|
+
|
78
|
+
def notice_transaction(path, uri=nil, params={})
|
79
|
+
if NewRelic::Agent.instance.transaction_sampler.builder
|
80
|
+
guid = NewRelic::Agent.instance.transaction_sampler.builder.sample.guid
|
81
|
+
end
|
82
|
+
transaction_data.set_transaction_info(path, uri, params, guid) if !disabled && transaction_data
|
83
|
+
end
|
84
|
+
|
85
|
+
def notice_first_scope_push(time)
|
86
|
+
create_transaction_data
|
87
|
+
end
|
88
|
+
|
89
|
+
def create_transaction_data
|
90
|
+
Thread.current[:new_relic_sql_data] = TransactionSqlData.new
|
91
|
+
end
|
92
|
+
|
93
|
+
def transaction_data
|
94
|
+
Thread.current[:new_relic_sql_data]
|
95
|
+
end
|
96
|
+
|
97
|
+
def clear_transaction_data
|
98
|
+
Thread.current[:new_relic_sql_data] = nil
|
99
|
+
end
|
100
|
+
|
101
|
+
# This is called when we are done with the transaction.
|
102
|
+
def notice_scope_empty(time=Time.now)
|
103
|
+
data = transaction_data
|
104
|
+
clear_transaction_data
|
105
|
+
|
106
|
+
if data.sql_data.size > 0
|
107
|
+
@samples_lock.synchronize do
|
108
|
+
NewRelic::Agent.instance.log.debug "Harvesting #{data.sql_data.size} slow transaction sql statement(s)"
|
109
|
+
#FIXME get tx name and uri
|
110
|
+
harvest_slow_sql data
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# this should always be called under the @samples_lock
|
116
|
+
def harvest_slow_sql(transaction_sql_data)
|
117
|
+
transaction_sql_data.sql_data.each do |sql_item|
|
118
|
+
normalized_sql = sql_item.normalize
|
119
|
+
sql_trace = @sql_traces[normalized_sql]
|
120
|
+
if sql_trace
|
121
|
+
sql_trace.aggregate(sql_item, transaction_sql_data.path,
|
122
|
+
transaction_sql_data.uri)
|
123
|
+
else
|
124
|
+
@sql_traces[normalized_sql] = SqlTrace.new(normalized_sql,
|
125
|
+
sql_item, transaction_sql_data.path, transaction_sql_data.uri)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
def notice_sql(sql, metric_name, config, duration)
|
132
|
+
return unless transaction_data
|
133
|
+
if NewRelic::Agent.is_sql_recorded?
|
134
|
+
if duration > @explain_threshold
|
135
|
+
backtrace = caller.join("\n")
|
136
|
+
transaction_data.sql_data << SlowSql.new(sql, metric_name, config,
|
137
|
+
duration, backtrace)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def merge(sql_traces)
|
143
|
+
@samples_lock.synchronize do
|
144
|
+
#FIXME we need to merge the sql_traces array back into the @sql_traces hash
|
145
|
+
# @sql_traces.merge! sql_traces
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def harvest
|
150
|
+
return [] if disabled
|
151
|
+
result = []
|
152
|
+
@samples_lock.synchronize do
|
153
|
+
result = @sql_traces.values
|
154
|
+
@sql_traces = {}
|
155
|
+
end
|
156
|
+
slowest = result.sort{|a,b| b.max_call_time <=> a.max_call_time}[0,10]
|
157
|
+
slowest.each {|trace| trace.prepare_to_send }
|
158
|
+
slowest
|
159
|
+
end
|
160
|
+
|
161
|
+
# reset samples without rebooting the web server
|
162
|
+
def reset!
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
class TransactionSqlData
|
167
|
+
attr_reader :path
|
168
|
+
attr_reader :uri
|
169
|
+
attr_reader :params
|
170
|
+
attr_reader :sql_data
|
171
|
+
attr_reader :guid
|
172
|
+
|
173
|
+
def initialize
|
174
|
+
@sql_data = []
|
175
|
+
end
|
176
|
+
|
177
|
+
def set_transaction_info(path, uri, params, guid)
|
178
|
+
@path = path
|
179
|
+
@uri = uri
|
180
|
+
@params = params
|
181
|
+
@guid = guid
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
class SlowSql
|
186
|
+
attr_reader :sql
|
187
|
+
attr_reader :metric_name
|
188
|
+
attr_reader :duration
|
189
|
+
attr_reader :backtrace
|
190
|
+
|
191
|
+
def initialize(sql, metric_name, config, duration, backtrace = nil)
|
192
|
+
@sql = sql
|
193
|
+
@metric_name = metric_name
|
194
|
+
@config = config
|
195
|
+
@duration = duration
|
196
|
+
@backtrace = backtrace
|
197
|
+
end
|
198
|
+
|
199
|
+
def obfuscate
|
200
|
+
NewRelic::Agent::Database.obfuscate_sql(@sql)
|
201
|
+
end
|
202
|
+
|
203
|
+
def normalize
|
204
|
+
NewRelic::Agent::Database::Obfuscator.instance \
|
205
|
+
.default_sql_obfuscator(@sql).gsub(/\?\s*\,\s*/, '').gsub(/\s/, '')
|
206
|
+
end
|
207
|
+
|
208
|
+
def explain
|
209
|
+
NewRelic::Agent::Database.explain_sql(@sql, @config)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
class SqlTrace < MethodTraceStats
|
214
|
+
attr_reader :path
|
215
|
+
attr_reader :url
|
216
|
+
attr_reader :sql_id
|
217
|
+
attr_reader :sql
|
218
|
+
attr_reader :database_metric_name
|
219
|
+
attr_reader :params
|
220
|
+
|
221
|
+
def initialize(normalized_query, slow_sql, path, uri)
|
222
|
+
super()
|
223
|
+
@params = {} #FIXME
|
224
|
+
@sql_id = consistent_hash(normalized_query)
|
225
|
+
set_primary slow_sql, path, uri
|
226
|
+
record_data_point slow_sql.duration
|
227
|
+
end
|
228
|
+
|
229
|
+
def set_primary(slow_sql, path, uri)
|
230
|
+
@slow_sql = slow_sql
|
231
|
+
@sql = slow_sql.sql
|
232
|
+
@database_metric_name = slow_sql.metric_name
|
233
|
+
@path = path
|
234
|
+
@url = uri
|
235
|
+
# FIXME
|
236
|
+
@params[:backtrace] = slow_sql.backtrace if slow_sql.backtrace
|
237
|
+
end
|
238
|
+
|
239
|
+
def aggregate(slow_sql, path, uri)
|
240
|
+
if slow_sql.duration > max_call_time
|
241
|
+
set_primary slow_sql, path, uri
|
242
|
+
end
|
243
|
+
|
244
|
+
record_data_point slow_sql.duration
|
245
|
+
end
|
246
|
+
|
247
|
+
def prepare_to_send
|
248
|
+
params[:explain_plan] = @slow_sql.explain if need_to_explain?
|
249
|
+
@sql = @slow_sql.obfuscate if need_to_obfuscate?
|
250
|
+
end
|
251
|
+
|
252
|
+
def agent_config
|
253
|
+
NewRelic::Agent::SqlSampler.config
|
254
|
+
end
|
255
|
+
|
256
|
+
def need_to_obfuscate?
|
257
|
+
agent_config['record_sql'] == 'obfuscated'
|
258
|
+
end
|
259
|
+
|
260
|
+
def need_to_explain?
|
261
|
+
agent_config['explain_enabled']
|
262
|
+
end
|
263
|
+
|
264
|
+
def to_json(*a)
|
265
|
+
[@path, @url, @sql_id, @sql, @database_metric_name, @call_count, @total_call_time, @min_call_time, @max_call_time, @params].to_json(*a)
|
266
|
+
end
|
267
|
+
|
268
|
+
private
|
269
|
+
|
270
|
+
def consistent_hash(string)
|
271
|
+
if NewRelic::LanguageSupport.using_version?('1.9.2')
|
272
|
+
# String#hash is salted differently on every VM start in 1.9
|
273
|
+
require 'digest/md5'
|
274
|
+
Digest::MD5.hexdigest(string).hex
|
275
|
+
else
|
276
|
+
string.hash
|
277
|
+
end.modulo(2**31-1)
|
278
|
+
# modulo ensures sql_id fits in an INT(11)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'new_relic/agent/stats_engine/metric_stats'
|
2
2
|
require 'new_relic/agent/stats_engine/samplers'
|
3
3
|
require 'new_relic/agent/stats_engine/transactions'
|
4
|
+
require 'new_relic/agent/stats_engine/gc_profiler'
|
4
5
|
|
5
6
|
module NewRelic
|
6
7
|
module Agent
|
8
|
+
# This class handles all the statistics gathering for the agent
|
7
9
|
class StatsEngine
|
8
10
|
include MetricStats
|
9
11
|
include Samplers
|
@@ -0,0 +1,123 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module NewRelic
|
3
|
+
module Agent
|
4
|
+
class StatsEngine
|
5
|
+
module GCProfiler
|
6
|
+
def self.init
|
7
|
+
@profiler = RailsBench.new if RailsBench.enabled?
|
8
|
+
@profiler = Ruby19.new if Ruby19.enabled?
|
9
|
+
@profiler = Rubinius.new if Rubinius.enabled?
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.capture
|
13
|
+
@profiler.capture if @profiler
|
14
|
+
end
|
15
|
+
|
16
|
+
class Profiler
|
17
|
+
def initialize
|
18
|
+
if self.class.enabled?
|
19
|
+
@last_timestamp = call_time
|
20
|
+
@last_count = call_count
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def capture
|
25
|
+
return unless self.class.enabled?
|
26
|
+
return if !scope_stack.empty? && scope_stack.last.name == "GC/cumulative"
|
27
|
+
|
28
|
+
num_calls = call_count - @last_count
|
29
|
+
elapsed = (call_time - @last_timestamp).to_f
|
30
|
+
@last_timestamp = call_time
|
31
|
+
@last_count = call_count
|
32
|
+
reset
|
33
|
+
|
34
|
+
record_gc_metric(num_calls, elapsed)
|
35
|
+
end
|
36
|
+
|
37
|
+
def reset; end
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
def record_gc_metric(num_calls, elapsed)
|
42
|
+
if num_calls > 0
|
43
|
+
# microseconds to seconds
|
44
|
+
elapsed = elapsed / 1_000_000.0
|
45
|
+
# Allocate the GC time to a scope as if the GC just ended
|
46
|
+
# right now.
|
47
|
+
time = Time.now.to_f
|
48
|
+
gc_scope = NewRelic::Agent.instance.stats_engine.push_scope("GC/cumulative", time - elapsed)
|
49
|
+
# GC stats are collected into a blamed metric which allows
|
50
|
+
# us to show the stats controller by controller
|
51
|
+
gc_stats = NewRelic::Agent.get_stats(gc_scope.name, true)
|
52
|
+
gc_stats.record_multiple_data_points(elapsed, num_calls)
|
53
|
+
NewRelic::Agent.instance.stats_engine.pop_scope(gc_scope, elapsed, time)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def scope_stack
|
58
|
+
Thread::current[:newrelic_scope_stack] ||= []
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class RailsBench < Profiler
|
63
|
+
def self.enabled?
|
64
|
+
::GC.respond_to?(:time) && ::GC.respond_to?(:collections)
|
65
|
+
end
|
66
|
+
|
67
|
+
# microseconds spent in GC
|
68
|
+
def call_time
|
69
|
+
::GC.time # this should already be microseconds
|
70
|
+
end
|
71
|
+
|
72
|
+
def call_count
|
73
|
+
::GC.collections
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Ruby19 < Profiler
|
78
|
+
def self.enabled?
|
79
|
+
defined?(::GC::Profiler) && ::GC::Profiler.enabled?
|
80
|
+
end
|
81
|
+
|
82
|
+
# microseconds spent in GC
|
83
|
+
# 1.9 total_time returns seconds. Don't trust the docs. It's seconds.
|
84
|
+
def call_time
|
85
|
+
::GC::Profiler.total_time * 1_000_000.0 # convert seconds to microseconds
|
86
|
+
end
|
87
|
+
|
88
|
+
def call_count
|
89
|
+
::GC.count
|
90
|
+
end
|
91
|
+
|
92
|
+
def reset
|
93
|
+
::GC::Profiler.clear
|
94
|
+
@last_timestamp = 0
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class Rubinius < Profiler
|
99
|
+
def self.enabled?
|
100
|
+
if NewRelic::LanguageSupport.using_engine?('rbx')
|
101
|
+
require 'rubinius/agent'
|
102
|
+
true
|
103
|
+
else
|
104
|
+
false
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def call_time
|
109
|
+
agent = ::Rubinius::Agent.loopback
|
110
|
+
(agent.get('system.gc.young.total_wallclock')[1] +
|
111
|
+
agent.get('system.gc.full.total_wallclock')[1]) * 1000
|
112
|
+
end
|
113
|
+
|
114
|
+
def call_count
|
115
|
+
agent = ::Rubinius::Agent.loopback
|
116
|
+
agent.get('system.gc.young.count')[1] +
|
117
|
+
agent.get('system.gc.full.count')[1]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|