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
@@ -1,16 +1,46 @@
|
|
1
1
|
module NewRelic
|
2
2
|
module Agent
|
3
|
+
# This class contains the configuration data for setting up RUM
|
4
|
+
# headers and footers - acts as a cache of this data so we don't
|
5
|
+
# need to look it up or reconfigure it every request
|
3
6
|
class BeaconConfiguration
|
7
|
+
|
8
|
+
# the statically generated header - generated when the beacon
|
9
|
+
# configuration is created - does not vary per page
|
4
10
|
attr_reader :browser_timing_header
|
11
|
+
|
12
|
+
# the static portion of the RUM footer - this part does not vary
|
13
|
+
# by which request is in progress
|
5
14
|
attr_reader :browser_timing_static_footer
|
15
|
+
|
16
|
+
# the application id we include in the javascript -
|
17
|
+
# crossreferences with the application id on the collectors
|
6
18
|
attr_reader :application_id
|
19
|
+
|
20
|
+
# the key used for browser monitoring. This is different from
|
21
|
+
# the account key
|
7
22
|
attr_reader :browser_monitoring_key
|
8
|
-
attr_reader :beacon
|
9
|
-
attr_reader :rum_enabled
|
10
|
-
attr_reader :license_bytes
|
11
23
|
|
12
|
-
|
24
|
+
# which beacon we should report to - set by startup of the agent
|
25
|
+
attr_reader :beacon
|
13
26
|
|
27
|
+
# whether RUM is enabled or not - determined based on server and
|
28
|
+
# local config
|
29
|
+
attr_reader :rum_enabled
|
30
|
+
|
31
|
+
# whether JSONP is used to communicate with the Beacon or not
|
32
|
+
attr_reader :rum_jsonp
|
33
|
+
|
34
|
+
# RUM footer command used for 'finish' - based on whether JSONP is
|
35
|
+
# being used. 'nrfj' for JSONP, otherwise 'nrf2'
|
36
|
+
attr_reader :finish_command
|
37
|
+
|
38
|
+
# A static javascript header that is identical for every account
|
39
|
+
# and application
|
40
|
+
JS_HEADER = "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"
|
41
|
+
|
42
|
+
# Creates a new browser configuration data. Argument is a hash
|
43
|
+
# of configuration values from the server
|
14
44
|
def initialize(connect_data)
|
15
45
|
@browser_monitoring_key = connect_data['browser_key']
|
16
46
|
@application_id = connect_data['application_id']
|
@@ -22,8 +52,14 @@ module NewRelic
|
|
22
52
|
NewRelic::Control.instance.log.debug("Browser timing header: #{@browser_timing_header.inspect}")
|
23
53
|
@browser_timing_static_footer = build_load_file_js(connect_data)
|
24
54
|
NewRelic::Control.instance.log.debug("Browser timing static footer: #{@browser_timing_static_footer.inspect}")
|
55
|
+
@rum_jsonp = connect_data['rum.jsonp']
|
56
|
+
@rum_jsonp = true if @rum_jsonp.nil?
|
57
|
+
NewRelic::Control.instance.log.debug("Real User Monitoring is using JSONP protocol") if @rum_jsonp
|
58
|
+
@finish_command = @rum_jsonp ? 'nrfj' : 'nrf2'
|
25
59
|
end
|
26
|
-
|
60
|
+
|
61
|
+
# returns a memoized version of the bytes in the license key for
|
62
|
+
# obscuring transaction names in the javascript
|
27
63
|
def license_bytes
|
28
64
|
if @license_bytes.nil?
|
29
65
|
@license_bytes = []
|
@@ -31,18 +67,42 @@ module NewRelic
|
|
31
67
|
end
|
32
68
|
@license_bytes
|
33
69
|
end
|
34
|
-
|
70
|
+
|
71
|
+
# returns a snippet of text that does not change
|
72
|
+
# per-transaction. Is empty when rum is disabled, or we are not
|
73
|
+
# including the episodes file dynamically (i.e. the user
|
74
|
+
# includes it themselves)
|
35
75
|
def build_load_file_js(connect_data)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
76
|
+
js = <<-EOS
|
77
|
+
if (!NREUMQ.f) { NREUMQ.f=function() {
|
78
|
+
NREUMQ.push(["load",new Date().getTime()]);
|
79
|
+
EOS
|
80
|
+
|
81
|
+
if connect_data.fetch('rum.load_episodes_file', true)
|
82
|
+
episodes_url = connect_data.fetch('episodes_url', '')
|
83
|
+
js << <<-EOS
|
84
|
+
var e=document.createElement(\"script\");
|
85
|
+
e.type=\"text/javascript\";e.async=true;e.src=\"#{episodes_url}\";
|
86
|
+
document.body.appendChild(e);
|
87
|
+
EOS
|
88
|
+
end
|
89
|
+
|
90
|
+
js << <<-EOS
|
91
|
+
if(NREUMQ.a)NREUMQ.a();
|
92
|
+
};
|
93
|
+
NREUMQ.a=window.onload;window.onload=NREUMQ.f;
|
94
|
+
};
|
95
|
+
EOS
|
96
|
+
js
|
40
97
|
end
|
41
98
|
|
99
|
+
# returns a copy of the static javascript header, in case people
|
100
|
+
# are munging strings somewhere down the line
|
42
101
|
def javascript_header
|
43
102
|
JS_HEADER.dup
|
44
103
|
end
|
45
|
-
|
104
|
+
|
105
|
+
# Returns the header string, properly html-safed if needed
|
46
106
|
def build_browser_timing_header
|
47
107
|
return "" if !@rum_enabled
|
48
108
|
return "" if @browser_monitoring_key.nil?
|
@@ -2,8 +2,37 @@ require 'base64'
|
|
2
2
|
require 'new_relic/agent/beacon_configuration'
|
3
3
|
module NewRelic
|
4
4
|
module Agent
|
5
|
+
# This module contains support for Real User Monitoring - the
|
6
|
+
# javascript generation and configuration
|
5
7
|
module BrowserMonitoring
|
6
|
-
|
8
|
+
|
9
|
+
|
10
|
+
class DummyMetricFrame
|
11
|
+
def initialize
|
12
|
+
@attributes = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def user_attributes
|
16
|
+
@attributes
|
17
|
+
end
|
18
|
+
|
19
|
+
def queue_time
|
20
|
+
0.0
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@@dummy_metric_frame = DummyMetricFrame.new
|
25
|
+
|
26
|
+
|
27
|
+
# This method returns a string suitable for inclusion in a page
|
28
|
+
# - known as 'manual instrumentation' for Real User
|
29
|
+
# Monitoring. Can return either a script tag with associated
|
30
|
+
# javascript, or in the case of disabled Real User Monitoring,
|
31
|
+
# an empty string
|
32
|
+
#
|
33
|
+
# This is the header string - it should be placed as high in the
|
34
|
+
# page as is reasonably possible - that is, before any style or
|
35
|
+
# javascript inclusions, but after any header-related meta tags
|
7
36
|
def browser_timing_header
|
8
37
|
return "" if NewRelic::Agent.instance.beacon_configuration.nil?
|
9
38
|
return "" if !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced?
|
@@ -11,36 +40,61 @@ module NewRelic
|
|
11
40
|
NewRelic::Agent.instance.beacon_configuration.browser_timing_header
|
12
41
|
end
|
13
42
|
|
43
|
+
# This method returns a string suitable for inclusion in a page
|
44
|
+
# - known as 'manual instrumentation' for Real User
|
45
|
+
# Monitoring. Can return either a script tag with associated
|
46
|
+
# javascript, or in the case of disabled Real User Monitoring,
|
47
|
+
# an empty string
|
48
|
+
#
|
49
|
+
# This is the footer string - it should be placed as low in the
|
50
|
+
# page as is reasonably possible.
|
14
51
|
def browser_timing_footer
|
15
52
|
config = NewRelic::Agent.instance.beacon_configuration
|
16
53
|
return "" if config.nil? || !config.rum_enabled || config.browser_monitoring_key.nil?
|
17
54
|
return "" if !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced?
|
18
|
-
generate_footer_js
|
55
|
+
generate_footer_js(config)
|
19
56
|
end
|
20
57
|
|
21
58
|
private
|
22
59
|
|
23
|
-
def generate_footer_js
|
60
|
+
def generate_footer_js(config)
|
24
61
|
if browser_monitoring_start_time
|
25
|
-
config = NewRelic::Agent.instance.beacon_configuration
|
26
62
|
application_id = config.application_id
|
27
63
|
beacon = config.beacon
|
28
64
|
license_key = config.browser_monitoring_key
|
29
65
|
|
30
|
-
footer_js_string(beacon, license_key, application_id)
|
66
|
+
footer_js_string(config, beacon, license_key, application_id)
|
31
67
|
else
|
32
68
|
''
|
33
69
|
end
|
34
70
|
end
|
35
71
|
|
36
72
|
def browser_monitoring_transaction_name
|
37
|
-
|
73
|
+
NewRelic::Agent::TransactionInfo.get.transaction_name
|
38
74
|
end
|
39
75
|
|
40
76
|
def browser_monitoring_start_time
|
41
|
-
|
77
|
+
NewRelic::Agent::TransactionInfo.get.start_time
|
42
78
|
end
|
43
|
-
|
79
|
+
|
80
|
+
def metric_frame_attribute(key)
|
81
|
+
current_metric_frame.user_attributes[key] || ""
|
82
|
+
end
|
83
|
+
|
84
|
+
def current_metric_frame
|
85
|
+
Thread.current[:last_metric_frame] || @@dummy_metric_frame
|
86
|
+
end
|
87
|
+
|
88
|
+
def tt_guid
|
89
|
+
txn = NewRelic::Agent::TransactionInfo.get
|
90
|
+
return txn.guid if txn.include_guid?
|
91
|
+
""
|
92
|
+
end
|
93
|
+
|
94
|
+
def tt_token
|
95
|
+
return NewRelic::Agent::TransactionInfo.get.token
|
96
|
+
end
|
97
|
+
|
44
98
|
def clamp_to_positive(value)
|
45
99
|
return 0.0 if value < 0
|
46
100
|
value
|
@@ -51,12 +105,17 @@ module NewRelic
|
|
51
105
|
end
|
52
106
|
|
53
107
|
def browser_monitoring_queue_time
|
54
|
-
clamp_to_positive((
|
108
|
+
clamp_to_positive((current_metric_frame.queue_time.to_f * 1000.0).round)
|
55
109
|
end
|
56
110
|
|
57
|
-
def footer_js_string(beacon, license_key, application_id)
|
58
|
-
obfuscated_transaction_name = obfuscate(browser_monitoring_transaction_name)
|
59
|
-
|
111
|
+
def footer_js_string(config, beacon, license_key, application_id)
|
112
|
+
obfuscated_transaction_name = obfuscate(config, browser_monitoring_transaction_name)
|
113
|
+
|
114
|
+
user = obfuscate(config, metric_frame_attribute(:user))
|
115
|
+
account = obfuscate(config, metric_frame_attribute(:account))
|
116
|
+
product = obfuscate(config, metric_frame_attribute(:product))
|
117
|
+
|
118
|
+
html_safe_if_needed("<script type=\"text/javascript\">#{config.browser_timing_static_footer}NREUMQ.push([\"#{config.finish_command}\",\"#{beacon}\",\"#{license_key}\",#{application_id},\"#{obfuscated_transaction_name}\",#{browser_monitoring_queue_time},#{browser_monitoring_app_time},new Date().getTime(),\"#{tt_guid}\",\"#{tt_token}\",\"#{user}\",\"#{account}\",\"#{product}\"])</script>")
|
60
119
|
end
|
61
120
|
|
62
121
|
def html_safe_if_needed(string)
|
@@ -67,9 +126,9 @@ module NewRelic
|
|
67
126
|
end
|
68
127
|
end
|
69
128
|
|
70
|
-
def obfuscate(text)
|
129
|
+
def obfuscate(config, text)
|
71
130
|
obfuscated = ""
|
72
|
-
key_bytes =
|
131
|
+
key_bytes = config.license_bytes
|
73
132
|
index = 0
|
74
133
|
text.each_byte{|byte|
|
75
134
|
obfuscated.concat((byte ^ key_bytes[index % 13].to_i))
|
@@ -14,7 +14,10 @@ module NewRelic
|
|
14
14
|
|
15
15
|
# For testability, add accessors:
|
16
16
|
attr_reader :harvest_start, :accumulator
|
17
|
-
|
17
|
+
|
18
|
+
# sets up busy calculations based on the start and end of
|
19
|
+
# transactions - used for a rough estimate of what percentage of
|
20
|
+
# wall clock time is spent processing requests
|
18
21
|
def dispatcher_start(time)
|
19
22
|
Thread.current[:busy_entries] ||= 0
|
20
23
|
callers = Thread.current[:busy_entries] += 1
|
@@ -23,7 +26,10 @@ module NewRelic
|
|
23
26
|
@entrypoint_stack.push time
|
24
27
|
end
|
25
28
|
end
|
26
|
-
|
29
|
+
|
30
|
+
# called when a transaction finishes, to add time to the
|
31
|
+
# instance variable accumulator. this is harvested when we send
|
32
|
+
# data to the server
|
27
33
|
def dispatcher_finish(end_time = Time.now)
|
28
34
|
callers = Thread.current[:busy_entries] -= 1
|
29
35
|
# Ignore nested calls
|
@@ -36,7 +42,9 @@ module NewRelic
|
|
36
42
|
end
|
37
43
|
end
|
38
44
|
end
|
39
|
-
|
45
|
+
|
46
|
+
# this returns the size of the entry point stack, which
|
47
|
+
# determines how many transactions are running
|
40
48
|
def busy_count
|
41
49
|
@entrypoint_stack.size
|
42
50
|
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
# This is used
|
2
|
-
|
1
|
+
# This class is used by NewRelic::Agent.set_sql_obfuscator to chain multiple
|
2
|
+
# obfuscation blocks when not using the default :replace action
|
3
3
|
class NewRelic::ChainedCall
|
4
4
|
def initialize(block1, block2)
|
5
5
|
@block1 = block1
|
@@ -0,0 +1,223 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module NewRelic
|
4
|
+
# columns for a mysql explain plan
|
5
|
+
MYSQL_EXPLAIN_COLUMNS = [
|
6
|
+
"Id",
|
7
|
+
"Select Type",
|
8
|
+
"Table",
|
9
|
+
"Type",
|
10
|
+
"Possible Keys",
|
11
|
+
"Key",
|
12
|
+
"Key Length",
|
13
|
+
"Ref",
|
14
|
+
"Rows",
|
15
|
+
"Extra"
|
16
|
+
].freeze
|
17
|
+
|
18
|
+
module Agent
|
19
|
+
module Database
|
20
|
+
extend self
|
21
|
+
|
22
|
+
def obfuscate_sql(sql)
|
23
|
+
Obfuscator.instance.obfuscator.call(sql)
|
24
|
+
end
|
25
|
+
|
26
|
+
def set_sql_obfuscator(type, &block)
|
27
|
+
Obfuscator.instance.set_sql_obfuscator(type, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_connection(config)
|
31
|
+
ConnectionManager.instance.get_connection(config)
|
32
|
+
end
|
33
|
+
|
34
|
+
def close_connections
|
35
|
+
ConnectionManager.instance.close_connections
|
36
|
+
end
|
37
|
+
|
38
|
+
# Perform this in the runtime environment of a managed
|
39
|
+
# application, to explain the sql statement executed within a
|
40
|
+
# segment of a transaction sample. Returns an array of
|
41
|
+
# explanations (which is an array rows consisting of an array of
|
42
|
+
# strings for each column returned by the the explain query)
|
43
|
+
# Note this happens only for statements whose execution time
|
44
|
+
# exceeds a threshold (e.g. 500ms) and only within the slowest
|
45
|
+
# transaction in a report period, selected for shipment to New
|
46
|
+
# Relic
|
47
|
+
def explain_sql(sql, connection_config)
|
48
|
+
return nil unless sql && connection_config
|
49
|
+
statement = sql.split(";\n")[0] # only explain the first
|
50
|
+
explain_sql = explain_statement(statement, connection_config)
|
51
|
+
return explain_sql || []
|
52
|
+
end
|
53
|
+
|
54
|
+
def explain_statement(statement, config)
|
55
|
+
if is_select?(statement)
|
56
|
+
handle_exception_in_explain do
|
57
|
+
connection = get_connection(config)
|
58
|
+
plan = nil
|
59
|
+
if connection
|
60
|
+
plan = process_resultset(connection.execute("EXPLAIN #{statement}"))
|
61
|
+
end
|
62
|
+
return plan
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def process_resultset(items)
|
68
|
+
# The resultset type varies for different drivers. Only thing you can count on is
|
69
|
+
# that it implements each. Also: can't use select_rows because the native postgres
|
70
|
+
# driver doesn't know that method.
|
71
|
+
|
72
|
+
headers = []
|
73
|
+
values = []
|
74
|
+
if items.respond_to?(:each_hash)
|
75
|
+
items.each_hash do |row|
|
76
|
+
headers = row.keys
|
77
|
+
values << headers.map{|h| row[h] }
|
78
|
+
end
|
79
|
+
elsif items.respond_to?(:each)
|
80
|
+
items.each do |row|
|
81
|
+
if row.kind_of?(Hash)
|
82
|
+
headers = row.keys
|
83
|
+
values << headers.map{|h| row[h] }
|
84
|
+
else
|
85
|
+
values << row
|
86
|
+
end
|
87
|
+
end
|
88
|
+
else
|
89
|
+
values = [items]
|
90
|
+
end
|
91
|
+
|
92
|
+
headers = nil if headers.empty?
|
93
|
+
[headers, values]
|
94
|
+
end
|
95
|
+
|
96
|
+
def handle_exception_in_explain
|
97
|
+
yield
|
98
|
+
rescue => e
|
99
|
+
begin
|
100
|
+
# guarantees no throw from explain_sql
|
101
|
+
NewRelic::Control.instance.log.error("Error getting query plan: #{e.message}")
|
102
|
+
NewRelic::Control.instance.log.debug(e.backtrace.join("\n"))
|
103
|
+
rescue
|
104
|
+
# double exception. throw up your hands
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def is_select?(statement)
|
109
|
+
# split the string into at most two segments on the
|
110
|
+
# system-defined field separator character
|
111
|
+
first_word, rest_of_statement = statement.split($;, 2)
|
112
|
+
(first_word.upcase == 'SELECT')
|
113
|
+
end
|
114
|
+
|
115
|
+
class ConnectionManager
|
116
|
+
include Singleton
|
117
|
+
|
118
|
+
# Returns a cached connection for a given ActiveRecord
|
119
|
+
# configuration - these are stored or reopened as needed, and if
|
120
|
+
# we cannot get one, we ignore it and move on without explaining
|
121
|
+
# the sql
|
122
|
+
def get_connection(config)
|
123
|
+
@connections ||= {}
|
124
|
+
|
125
|
+
connection = @connections[config]
|
126
|
+
|
127
|
+
return connection if connection
|
128
|
+
|
129
|
+
begin
|
130
|
+
connection = ActiveRecord::Base.send("#{config[:adapter]}_connection", config)
|
131
|
+
@connections[config] = connection
|
132
|
+
rescue => e
|
133
|
+
NewRelic::Agent.agent.log.error("Caught exception #{e} trying to get connection to DB for explain. Control: #{config}")
|
134
|
+
NewRelic::Agent.agent.log.error(e.backtrace.join("\n"))
|
135
|
+
nil
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# Closes all the connections in the internal connection cache
|
140
|
+
def close_connections
|
141
|
+
@connections ||= {}
|
142
|
+
@connections.values.each do |connection|
|
143
|
+
begin
|
144
|
+
connection.disconnect!
|
145
|
+
rescue
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
@connections = {}
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
class Obfuscator
|
154
|
+
include Singleton
|
155
|
+
|
156
|
+
attr_reader :obfuscator
|
157
|
+
|
158
|
+
def initialize
|
159
|
+
reset
|
160
|
+
end
|
161
|
+
|
162
|
+
def reset
|
163
|
+
@obfuscator = method(:default_sql_obfuscator)
|
164
|
+
end
|
165
|
+
|
166
|
+
# Sets the sql obfuscator used to clean up sql when sending it
|
167
|
+
# to the server. Possible types are:
|
168
|
+
#
|
169
|
+
# :before => sets the block to run before the existing
|
170
|
+
# obfuscators
|
171
|
+
#
|
172
|
+
# :after => sets the block to run after the existing
|
173
|
+
# obfuscator(s)
|
174
|
+
#
|
175
|
+
# :replace => removes the current obfuscator and replaces it
|
176
|
+
# with the provided block
|
177
|
+
def set_sql_obfuscator(type, &block)
|
178
|
+
if type == :before
|
179
|
+
@obfuscator = NewRelic::ChainedCall.new(block, @obfuscator)
|
180
|
+
elsif type == :after
|
181
|
+
@obfuscator = NewRelic::ChainedCall.new(@obfuscator, block)
|
182
|
+
elsif type == :replace
|
183
|
+
@obfuscator = block
|
184
|
+
else
|
185
|
+
fail "unknown sql_obfuscator type #{type}"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def default_sql_obfuscator(sql)
|
190
|
+
stmt = sql.kind_of?(Statement) ? sql : Statement.new(sql)
|
191
|
+
adapter = stmt.adapter
|
192
|
+
obfuscated = remove_escaped_quotes(stmt)
|
193
|
+
obfuscated = obfuscate_single_quote_literals(obfuscated)
|
194
|
+
if !(adapter.to_s =~ /postgres/ || adapter.to_s =~ /sqlite/)
|
195
|
+
obfuscated = obfuscate_double_quote_literals(obfuscated)
|
196
|
+
end
|
197
|
+
obfuscated = obfuscate_numeric_literals(obfuscated)
|
198
|
+
obfuscated.to_s # return back to a regular String
|
199
|
+
end
|
200
|
+
|
201
|
+
def remove_escaped_quotes(sql)
|
202
|
+
sql.gsub(/\\"/, '').gsub(/\\'/, '')
|
203
|
+
end
|
204
|
+
|
205
|
+
def obfuscate_single_quote_literals(sql)
|
206
|
+
sql.gsub(/'(?:[^']|'')*'/, '?')
|
207
|
+
end
|
208
|
+
|
209
|
+
def obfuscate_double_quote_literals(sql)
|
210
|
+
sql.gsub(/"(?:[^"]|"")*"/, '?')
|
211
|
+
end
|
212
|
+
|
213
|
+
def obfuscate_numeric_literals(sql)
|
214
|
+
sql.gsub(/\b\d+\b/, "?")
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
class Statement < String
|
219
|
+
attr_accessor :adapter
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|