newrelic_rpm 3.10.0.279 → 3.11.0.283

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +61 -0
  3. data/lib/new_relic/agent.rb +14 -8
  4. data/lib/new_relic/agent/agent.rb +43 -28
  5. data/lib/new_relic/agent/agent_logger.rb +21 -20
  6. data/lib/new_relic/agent/configuration/default_source.rb +31 -1
  7. data/lib/new_relic/agent/database.rb +2 -1
  8. data/lib/new_relic/agent/datastores.rb +177 -0
  9. data/lib/new_relic/agent/datastores/metric_helper.rb +85 -0
  10. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +11 -20
  11. data/lib/new_relic/agent/deprecator.rb +18 -0
  12. data/lib/new_relic/agent/instrumentation/active_record.rb +20 -35
  13. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +116 -57
  14. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +11 -20
  15. data/lib/new_relic/agent/instrumentation/data_mapper.rb +104 -172
  16. data/lib/new_relic/agent/instrumentation/memcache.rb +104 -52
  17. data/lib/new_relic/agent/instrumentation/metric_frame.rb +9 -0
  18. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +15 -2
  19. data/lib/new_relic/agent/instrumentation/mongo.rb +5 -18
  20. data/lib/new_relic/agent/instrumentation/sequel_helper.rb +36 -0
  21. data/lib/new_relic/agent/new_relic_service.rb +4 -0
  22. data/lib/new_relic/agent/stats_engine/metric_stats.rb +2 -17
  23. data/lib/new_relic/agent/threading/backtrace_service.rb +28 -5
  24. data/lib/new_relic/agent/transaction.rb +63 -34
  25. data/lib/new_relic/agent/transaction_event_aggregator.rb +0 -4
  26. data/lib/new_relic/agent/transaction_sampler.rb +11 -5
  27. data/lib/new_relic/rack/error_collector.rb +0 -1
  28. data/lib/new_relic/version.rb +1 -1
  29. data/lib/sequel/extensions/newrelic_instrumentation.rb +28 -56
  30. data/lib/sequel/plugins/newrelic_instrumentation.rb +28 -45
  31. data/newrelic_rpm.gemspec +0 -7
  32. data/test/agent_helper.rb +35 -16
  33. data/test/environments/rails31/Gemfile +1 -0
  34. data/test/environments/rails32/Gemfile +1 -0
  35. data/test/helpers/mongo_metric_builder.rb +2 -3
  36. data/test/multiverse/lib/multiverse/output_collector.rb +24 -9
  37. data/test/multiverse/lib/multiverse/suite.rb +5 -0
  38. data/test/multiverse/suites/active_record/Envfile +6 -4
  39. data/test/multiverse/suites/active_record/active_record_test.rb +32 -73
  40. data/test/multiverse/suites/active_record/ar_method_aliasing.rb +0 -1
  41. data/test/multiverse/suites/activemerchant/activemerchant_test.rb +0 -3
  42. data/test/multiverse/suites/agent_only/agent_run_id_handling_test.rb +0 -1
  43. data/test/multiverse/suites/agent_only/audit_log_test.rb +0 -1
  44. data/test/multiverse/suites/agent_only/collector_exception_handling_test.rb +0 -2
  45. data/test/multiverse/suites/agent_only/cross_application_tracing_test.rb +0 -1
  46. data/test/multiverse/suites/agent_only/custom_analytics_events_test.rb +0 -2
  47. data/test/multiverse/suites/agent_only/custom_queue_time_test.rb +0 -1
  48. data/test/multiverse/suites/agent_only/encoding_handling_test.rb +0 -2
  49. data/test/multiverse/suites/agent_only/exclusive_time_test.rb +0 -2
  50. data/test/multiverse/suites/agent_only/harvest_timestamps_test.rb +0 -1
  51. data/test/multiverse/suites/agent_only/http_response_code_test.rb +0 -1
  52. data/test/multiverse/suites/agent_only/keepalive_test.rb +0 -1
  53. data/test/multiverse/suites/agent_only/key_transactions_test.rb +54 -9
  54. data/test/multiverse/suites/agent_only/labels_test.rb +0 -2
  55. data/test/multiverse/suites/agent_only/logging_test.rb +0 -1
  56. data/test/multiverse/suites/agent_only/marshaling_test.rb +0 -1
  57. data/test/multiverse/suites/agent_only/pipe_manager_test.rb +0 -2
  58. data/test/multiverse/suites/agent_only/rename_rule_test.rb +5 -7
  59. data/test/multiverse/suites/agent_only/rum_instrumentation_test.rb +0 -1
  60. data/test/multiverse/suites/agent_only/set_transaction_name_test.rb +0 -2
  61. data/test/multiverse/suites/agent_only/ssl_test.rb +0 -2
  62. data/test/multiverse/suites/agent_only/synthetics_test.rb +0 -1
  63. data/test/multiverse/suites/agent_only/testing_app.rb +21 -0
  64. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +1 -2
  65. data/test/multiverse/suites/agent_only/transaction_ignoring_test.rb +0 -2
  66. data/test/multiverse/suites/agent_only/utilization_data_collection_test.rb +0 -1
  67. data/test/multiverse/suites/agent_only/xray_sessions_test.rb +69 -34
  68. data/test/multiverse/suites/capistrano/deployment_test.rb +0 -1
  69. data/test/multiverse/suites/capistrano2/deployment_test.rb +0 -1
  70. data/test/multiverse/suites/config_file_loading/config_file_loading_test.rb +0 -2
  71. data/test/multiverse/suites/curb/curb_test.rb +0 -2
  72. data/test/multiverse/suites/datamapper/Envfile +26 -3
  73. data/test/multiverse/suites/datamapper/config/newrelic.yml +1 -0
  74. data/test/multiverse/suites/datamapper/datamapper_test.rb +271 -37
  75. data/test/multiverse/suites/deferred_instrumentation/sinatra_test.rb +0 -1
  76. data/test/multiverse/suites/delayed_job/Envfile +31 -8
  77. data/test/multiverse/suites/delayed_job/delayed_job_sampler_test.rb +0 -3
  78. data/test/multiverse/suites/delayed_job/unsupported_backend_test.rb +0 -3
  79. data/test/multiverse/suites/excon/excon_test.rb +0 -2
  80. data/test/multiverse/suites/grape/grape_test.rb +0 -3
  81. data/test/multiverse/suites/grape/grape_versioning_test.rb +0 -3
  82. data/test/multiverse/suites/grape/unsupported_version_test.rb +0 -3
  83. data/test/multiverse/suites/high_security/high_security_test.rb +0 -1
  84. data/test/multiverse/suites/httpclient/httpclient_test.rb +0 -2
  85. data/test/multiverse/suites/json/json_test.rb +0 -1
  86. data/test/multiverse/suites/marshalling/marshalling_test.rb +0 -1
  87. data/test/multiverse/suites/memcached/Envfile +52 -0
  88. data/test/multiverse/suites/memcached/dalli_test.rb +89 -0
  89. data/test/multiverse/suites/memcached/memcache_client_test.rb +25 -0
  90. data/test/multiverse/suites/memcached/memcache_test_cases.rb +302 -0
  91. data/test/multiverse/suites/memcached/memcached_test.rb +159 -0
  92. data/test/multiverse/suites/mongo/helpers/mongo_operation_tests.rb +26 -17
  93. data/test/multiverse/suites/mongo/mongo_connection_test.rb +0 -1
  94. data/test/multiverse/suites/mongo/mongo_instrumentation_test.rb +0 -1
  95. data/test/multiverse/suites/mongo/mongo_unsupported_version_test.rb +0 -1
  96. data/test/multiverse/suites/net_http/net_http_test.rb +0 -2
  97. data/test/multiverse/suites/padrino/padrino_test.rb +0 -3
  98. data/test/multiverse/suites/rack/http_response_code_test.rb +0 -1
  99. data/test/multiverse/suites/rack/nested_non_rack_app_test.rb +1 -1
  100. data/test/multiverse/suites/rack/rack_auto_instrumentation_test.rb +12 -12
  101. data/test/multiverse/suites/rack/rack_cascade_test.rb +0 -1
  102. data/test/multiverse/suites/rack/rack_env_mutation_test.rb +0 -1
  103. data/test/multiverse/suites/rack/rack_parameter_filtering_test.rb +0 -1
  104. data/test/multiverse/suites/rack/rack_unsupported_version_test.rb +0 -2
  105. data/test/multiverse/suites/rack/url_map_test.rb +3 -2
  106. data/test/multiverse/suites/rails/Envfile +3 -0
  107. data/test/multiverse/suites/rails/activejob_test.rb +0 -1
  108. data/test/multiverse/suites/rails/app.rb +0 -1
  109. data/test/multiverse/suites/rails/parameter_capture_test.rb +13 -0
  110. data/test/multiverse/suites/rails/rails3_app/app_rails3_plus.rb +5 -0
  111. data/test/multiverse/suites/rails/transaction_ignoring_test.rb +0 -2
  112. data/test/multiverse/suites/resque/instrumentation_test.rb +0 -2
  113. data/test/multiverse/suites/resque/resque_marshalling_test.rb +0 -1
  114. data/test/multiverse/suites/sequel/sequel_extension_test.rb +135 -0
  115. data/test/multiverse/suites/sequel/sequel_helpers.rb +62 -0
  116. data/test/multiverse/suites/sequel/sequel_plugin_test.rb +230 -0
  117. data/test/multiverse/suites/sidekiq/sidekiq_instrumentation_test.rb +0 -2
  118. data/test/multiverse/suites/sinatra/ignoring_test.rb +0 -2
  119. data/test/multiverse/suites/sinatra/nested_middleware_test.rb +0 -2
  120. data/test/multiverse/suites/sinatra/sinatra_classic_test.rb +0 -1
  121. data/test/multiverse/suites/sinatra/sinatra_error_tracing_test.rb +0 -2
  122. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +14 -12
  123. data/test/multiverse/suites/sinatra/sinatra_modular_test.rb +0 -1
  124. data/test/multiverse/suites/sinatra/sinatra_routes_test.rb +0 -2
  125. data/test/multiverse/suites/sinatra/sinatra_test_cases.rb +0 -2
  126. data/test/multiverse/suites/typhoeus/typhoeus_test.rb +0 -2
  127. data/test/multiverse/suites/yajl/yajl_test.rb +0 -1
  128. data/test/new_relic/agent/agent/start_test.rb +2 -2
  129. data/test/new_relic/agent/agent_logger_test.rb +6 -3
  130. data/test/new_relic/agent/datastores/metric_helper_test.rb +61 -0
  131. data/test/new_relic/agent/datastores/mongo/metric_translator_test.rb +20 -21
  132. data/test/new_relic/agent/datastores_test.rb +195 -0
  133. data/test/new_relic/agent/deprecator_test.rb +52 -0
  134. data/test/new_relic/agent/instrumentation/action_view_subscriber_test.rb +20 -26
  135. data/test/new_relic/agent/instrumentation/active_record_helper_test.rb +58 -53
  136. data/test/new_relic/agent/instrumentation/active_record_subscriber_test.rb +7 -20
  137. data/test/new_relic/agent/instrumentation/middleware_proxy_test.rb +19 -0
  138. data/test/new_relic/agent/instrumentation/sequel_helper_test.rb +36 -0
  139. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +1 -0
  140. data/test/new_relic/agent/method_tracer_test.rb +3 -4
  141. data/test/new_relic/agent/pipe_channel_manager_test.rb +1 -1
  142. data/test/new_relic/agent/threading/backtrace_service_test.rb +29 -4
  143. data/test/new_relic/agent/transaction_event_aggregator_test.rb +0 -4
  144. data/test/new_relic/agent/transaction_test.rb +100 -2
  145. data/test/new_relic/agent_test.rb +3 -3
  146. data/test/new_relic/http_client_test_cases.rb +0 -1
  147. data/test/new_relic/multiverse_helpers.rb +7 -0
  148. data/test/new_relic/transaction_ignoring_test_cases.rb +0 -2
  149. data/test/new_relic/transaction_sample_test.rb +11 -2
  150. data/test/performance/README.md +37 -17
  151. data/test/performance/lib/performance.rb +1 -0
  152. data/test/performance/lib/performance/baseline_compare_reporter.rb +11 -7
  153. data/test/performance/lib/performance/console_reporter.rb +29 -5
  154. data/test/performance/lib/performance/formatting_helpers.rb +22 -0
  155. data/test/performance/lib/performance/instrumentation/stackprof.rb +11 -1
  156. data/test/performance/lib/performance/result.rb +17 -6
  157. data/test/performance/lib/performance/runner.rb +7 -3
  158. data/test/performance/lib/performance/test_case.rb +89 -21
  159. data/test/performance/script/runner +13 -1
  160. data/test/performance/suites/active_record.rb +47 -0
  161. data/test/performance/suites/config.rb +4 -48
  162. data/test/performance/suites/marshalling.rb +20 -30
  163. data/test/performance/suites/queue_time.rb +1 -1
  164. data/test/performance/suites/rack_middleware.rb +1 -1
  165. data/test/performance/suites/rum_autoinsertion.rb +1 -1
  166. data/test/performance/suites/sql_obfuscation.rb +2 -2
  167. data/test/performance/suites/startup.rb +1 -1
  168. data/test/performance/suites/stats_hash.rb +7 -11
  169. data/test/performance/suites/thread_profiling.rb +20 -25
  170. data/test/performance/suites/trace_execution_scoped.rb +2 -2
  171. data/test/performance/suites/transaction_tracing.rb +4 -2
  172. data/test/test_helper.rb +5 -1
  173. metadata +53 -100
  174. data.tar.gz.sig +0 -0
  175. data/gem-public_cert.pem +0 -20
  176. data/lib/new_relic/agent/datastores/mongo/metric_generator.rb +0 -33
  177. data/test/multiverse/suites/sequel/sequel_instrumentation_test.rb +0 -289
  178. data/test/new_relic/agent/datastores/mongo/metric_generator_test.rb +0 -69
  179. data/test/new_relic/agent/memcache_instrumentation_test.rb +0 -155
  180. metadata.gz.sig +0 -2
@@ -2,7 +2,6 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
- require 'multiverse_helpers'
6
5
  require File.expand_path(File.join(__FILE__, '..', 'app'))
7
6
 
8
7
  require 'logger'
@@ -2,7 +2,6 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
- require 'multiverse_helpers'
6
5
  require File.join(File.dirname(__FILE__), 'middlewares')
7
6
 
8
7
  suite_dir = File.expand_path(File.dirname(__FILE__))
@@ -9,6 +9,10 @@ class ParameterCaptureController < ApplicationController
9
9
  render :text => 'hi!'
10
10
  end
11
11
 
12
+ def create
13
+ render :text => 'created'
14
+ end
15
+
12
16
  def sql
13
17
  NewRelic::Agent.agent.sql_sampler.notice_sql(
14
18
  'SELECT * FROM table',
@@ -214,5 +218,14 @@ class ParameterCaptureTest < RailsMultiverseTest
214
218
  expected = {"secret" => "[FILTERED]", "name" => "name", "raise" => "1"}
215
219
  assert_equal expected, last_traced_error_request_params
216
220
  end
221
+
222
+ def test_file_upload_params_are_replaced_with_placeholder
223
+ with_config(:capture_params => true) do
224
+ params = { :file => Rack::Test::UploadedFile.new(__FILE__, 'text/plain') }
225
+ post '/parameter_capture', params
226
+ result = last_transaction_trace_request_params
227
+ assert_equal("#<ActionDispatch::Http::UploadedFile>", result["file"])
228
+ end
229
+ end
217
230
  end
218
231
  end
@@ -78,8 +78,13 @@ if !defined?(MyApp)
78
78
  :constraints => lambda do |_|
79
79
  raise ActionController::RoutingError.new('this is an uncaught routing error')
80
80
  end)
81
+
81
82
  mount SinatraTestApp, :at => '/sinatra_app' if defined?(Sinatra)
83
+
82
84
  post '/filtering_test' => FilteringTestApp.new
85
+
86
+ post '/parameter_capture', :to => 'parameter_capture#create'
87
+
83
88
  get '/:controller(/:action(/:id))'
84
89
  end
85
90
 
@@ -3,8 +3,6 @@
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
5
  require './app'
6
- require 'multiverse_helpers'
7
- require File.join(File.dirname(__FILE__), '..', '..', '..', 'agent_helper')
8
6
  require 'transaction_ignoring_test_cases'
9
7
 
10
8
  class TransactionIgnorerController < ApplicationController
@@ -8,8 +8,6 @@ require 'resque'
8
8
  require 'logger'
9
9
  require 'newrelic_rpm'
10
10
  require 'fake_collector'
11
- require 'multiverse_helpers'
12
- require File.join(File.dirname(__FILE__), "..", "..", "..", "agent_helper")
13
11
 
14
12
  class ResqueTest < Minitest::Test
15
13
  include MultiverseHelpers
@@ -2,7 +2,6 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
- require 'multiverse_helpers'
6
5
  require File.join(File.dirname(__FILE__), '..', '..', '..', 'new_relic', 'marshalling_test_cases')
7
6
 
8
7
  if NewRelic::LanguageSupport.can_fork?
@@ -0,0 +1,135 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.join(File.dirname(__FILE__), 'database.rb')
6
+ require File.join(File.dirname(__FILE__), 'sequel_helpers.rb')
7
+
8
+ if Sequel.const_defined?( :MAJOR ) &&
9
+ ( Sequel::MAJOR > 3 ||
10
+ Sequel::MAJOR == 3 && Sequel::MINOR >= 37 )
11
+
12
+ require 'newrelic_rpm'
13
+
14
+ class SequelExtensionTest < Minitest::Test
15
+ include SequelHelpers
16
+
17
+ def setup
18
+ super
19
+
20
+ DB.extension :newrelic_instrumentation
21
+
22
+ @posts = DB[:posts]
23
+
24
+ NewRelic::Agent.disable_all_tracing do
25
+ post_id = @posts.insert( :title => 'All The Things', :content => 'A nice short story.' )
26
+ @post = @posts[:id => post_id]
27
+ end
28
+ end
29
+
30
+ def teardown
31
+ super
32
+
33
+ NewRelic::Agent.shutdown
34
+ end
35
+
36
+ def expected_metrics_for_operation(operation)
37
+ [
38
+ ["Datastore/operation/#{product_name}/#{operation}", "dummy"],
39
+ "Datastore/operation/#{product_name}/#{operation}",
40
+ "Datastore/#{product_name}/allWeb",
41
+ "Datastore/allWeb",
42
+ "Datastore/#{product_name}/all",
43
+ "Datastore/all"
44
+ ]
45
+ end
46
+
47
+ def test_all
48
+ in_web_transaction { @posts.all }
49
+
50
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:select))
51
+ end
52
+
53
+ def test_find
54
+ in_web_transaction do
55
+ @posts[:id => 11]
56
+ end
57
+
58
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:select))
59
+ end
60
+
61
+ def test_model_create_method_generates_metrics
62
+ in_web_transaction do
63
+ @posts.insert( :title => 'The Thing', :content => 'A wicked short story.' )
64
+ end
65
+
66
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:insert))
67
+ end
68
+
69
+ def test_update
70
+ in_web_transaction do
71
+ @posts.where(:id => @post[:id]).update( :title => 'A Lot of the Things' )
72
+ end
73
+
74
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:update))
75
+ end
76
+
77
+ def test_delete
78
+ in_web_transaction do
79
+ @posts.where(:id => @post[:id]).delete
80
+ end
81
+
82
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:delete))
83
+ end
84
+
85
+ def test_slow_queries_get_an_explain_plan
86
+ with_config( :'transaction_tracer.explain_threshold' => -0.01,
87
+ :'transaction_tracer.record_sql' => 'raw' ) do
88
+ segment = last_segment_for do
89
+ @posts[:id => 11]
90
+ end
91
+ assert_match %r{select \* from `posts` where \(?`id` = 11\)?( limit 1)?}i, segment.params[:sql]
92
+ assert_segment_has_explain_plan( segment )
93
+ end
94
+ end
95
+
96
+ def test_sql_is_recorded_in_tt_for_non_select
97
+ with_config(:'transaction_tracer.record_sql' => 'raw') do
98
+ segment = last_segment_for do
99
+ @posts.insert(:title => 'title', :content => 'content')
100
+ end
101
+ assert_match %r{insert into `posts` \([^\)]*\) values \([^\)]*\)}i, segment.params[:sql]
102
+ end
103
+ end
104
+
105
+ def test_queries_can_get_explain_plan_with_obfuscated_sql
106
+ config = {
107
+ :'transaction_tracer.explain_threshold' => -0.01,
108
+ :'transaction_tracer.record_sql' => 'obfuscated'
109
+ }
110
+ with_config(config) do
111
+ segment = last_segment_for(:record_sql => :obfuscated) do
112
+ @posts[:id => 11]
113
+ end
114
+ assert_match %r{select \* from `posts` where \(?`id` = \?\)?}i, segment.params[:sql]
115
+ assert_segment_has_explain_plan( segment )
116
+ end
117
+ end
118
+
119
+ def test_notices_sql_with_proper_metric_name
120
+ config = {
121
+ :'transaction_tracer.explain_threshold' => -0.01,
122
+ :'transaction_tracer.record_sql' => 'obfuscated'
123
+ }
124
+ with_config(config) do
125
+ in_web_transaction { @posts.all }
126
+ expected_metric_name = "Datastore/operation/#{product_name}/select"
127
+ recorded_metric_names = NewRelic::Agent.agent.sql_sampler.sql_traces.values.map(&:database_metric_name)
128
+ assert_includes recorded_metric_names, expected_metric_name
129
+ end
130
+ end
131
+ end
132
+
133
+ else
134
+ puts "Skipping tests in #{__FILE__} because unsupported Sequel version"
135
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ module SequelHelpers
6
+ def setup
7
+ super
8
+
9
+ DB.extension :newrelic_instrumentation
10
+
11
+ NewRelic::Agent.drop_buffered_data
12
+ end
13
+
14
+ def teardown
15
+ super
16
+
17
+ NewRelic::Agent.shutdown
18
+ end
19
+ #
20
+ # Helpers
21
+ #
22
+
23
+ # Pattern to match the column headers of a Sqlite explain plan
24
+ SQLITE_EXPLAIN_PLAN_COLUMNS_RE =
25
+ %r{\|addr\s*\|opcode\s*\|p1\s*\|p2\s*\|p3\s*\|p4\s*\|p5\s*\|comment\s*\|}
26
+
27
+ # This is particular to sqlite plans currently. To abstract it up, we'd need to
28
+ # be able to specify a flavor (e.g., :sqlite, :postgres, :mysql, etc.)
29
+ def assert_segment_has_explain_plan( segment, msg=nil )
30
+ msg = "Expected #{segment.inspect} to have an explain plan"
31
+ assert_block( msg ) { segment.params[:explain_plan].join =~ SQLITE_EXPLAIN_PLAN_COLUMNS_RE }
32
+ end
33
+
34
+ def last_segment_for(options={})
35
+ in_transaction('sandwiches/index') do
36
+ yield
37
+ end
38
+ sample = NewRelic::Agent.instance.transaction_sampler.last_sample
39
+ sample.prepare_to_send!
40
+ last_segment(sample)
41
+ end
42
+
43
+ def last_segment(txn_sample)
44
+ l_segment = nil
45
+ txn_sample.root_segment.each_segment do |segment|
46
+ l_segment = segment
47
+ end
48
+ l_segment
49
+ end
50
+
51
+ def product_name
52
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
53
+ "JDBC"
54
+ else
55
+ "SQLite"
56
+ end
57
+ end
58
+
59
+ def assert_datastore_metrics_recorded_exclusive(metrics, options = {})
60
+ assert_metrics_recorded_exclusive(metrics, {:filter => /^Datastores/}.update(options))
61
+ end
62
+ end
@@ -0,0 +1,230 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.join(File.dirname(__FILE__), 'database.rb')
6
+ require File.join(File.dirname(__FILE__), 'sequel_helpers.rb')
7
+
8
+ if Sequel.const_defined?( :MAJOR ) &&
9
+ ( Sequel::MAJOR > 3 ||
10
+ Sequel::MAJOR == 3 && Sequel::MINOR >= 37 )
11
+
12
+ require 'newrelic_rpm'
13
+
14
+ class SequelPluginTest < Minitest::Test
15
+ include SequelHelpers
16
+
17
+ def expected_metrics_for_operation(operation)
18
+ [
19
+ ["Datastore/statement/#{product_name}/Post/#{operation}", "dummy"],
20
+ "Datastore/statement/#{product_name}/Post/#{operation}",
21
+ "Datastore/operation/#{product_name}/#{operation}",
22
+ "Datastore/#{product_name}/allWeb",
23
+ "Datastore/allWeb",
24
+ "Datastore/#{product_name}/all",
25
+ "Datastore/all",
26
+ "dummy",
27
+ "Apdex"
28
+ ]
29
+ end
30
+
31
+ def test_sequel_model_instrumentation_is_loaded
32
+ assert Post.respond_to?( :trace_execution_scoped )
33
+ end
34
+
35
+ def test_model_enumerator_generates_metrics
36
+ in_web_transaction { Post.all }
37
+
38
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:all))
39
+ end
40
+
41
+ def test_model_index_operator_generates_metrics
42
+ in_web_transaction { Post[11] }
43
+
44
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:get))
45
+ end
46
+
47
+ def test_model_create_method_generates_metrics
48
+ in_web_transaction do
49
+ Post.create( :title => 'The Thing', :content => 'A wicked short story.' )
50
+ end
51
+
52
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:create))
53
+ end
54
+
55
+ def test_model_update_method_generates_metrics
56
+ in_web_transaction do
57
+ post = NewRelic::Agent.disable_all_tracing do
58
+ Post.create( :title => 'All The Things', :content => 'A story about beans.' )
59
+ end
60
+
61
+ post.update( :title => 'A Lot of the Things' )
62
+ end
63
+
64
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:update))
65
+ end
66
+
67
+ def test_model_update_all_method_generates_metrics
68
+ in_web_transaction do
69
+ post = NewRelic::Agent.disable_all_tracing do
70
+ Post.create( :title => 'All The Things', :content => 'A nicer story than yours.' )
71
+ end
72
+
73
+ post.update_all( :title => 'A Whole Hell of a Lot of the Things' )
74
+ end
75
+
76
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:update_all))
77
+ end
78
+
79
+ def test_model_update_except_method_generates_metrics
80
+ in_web_transaction do
81
+ post = NewRelic::Agent.disable_all_tracing do
82
+ Post.create( :title => 'All The Things', :content => 'A story.' )
83
+ end
84
+
85
+ post.update_except( {:title => 'A Bit More of the Things'} )
86
+ end
87
+
88
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:update_except))
89
+ end
90
+
91
+ def test_model_update_fields_method_generates_metrics
92
+ in_web_transaction do
93
+ post = NewRelic::Agent.disable_all_tracing do
94
+ Post.create( :title => 'All The Things', :content => 'A venal short story.' )
95
+ end
96
+
97
+ post.update_fields( {:title => 'A Plethora of Things'}, [:title] )
98
+ end
99
+
100
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:update_fields))
101
+ end
102
+
103
+ def test_model_update_only_method_generates_metrics
104
+ in_web_transaction do
105
+ post = NewRelic::Agent.disable_all_tracing do
106
+ Post.create( :title => 'All The Things', :content => 'A meandering short story.' )
107
+ end
108
+
109
+ post.update_only( {:title => 'A Lot of the Things'}, :title )
110
+ end
111
+
112
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:update_only))
113
+ end
114
+
115
+ def test_model_save_method_generates_metrics
116
+ in_web_transaction do
117
+ post = NewRelic::Agent.disable_all_tracing do
118
+ Post.new( :title => 'An Endless Lot Full of Things',
119
+ :content => 'A lingering long story.' )
120
+ end
121
+
122
+ post.save
123
+ end
124
+
125
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:save))
126
+ end
127
+
128
+ def test_model_delete_method_generates_metrics
129
+ in_web_transaction do
130
+ post = NewRelic::Agent.disable_all_tracing do
131
+ Post.create( :title => 'All The Things', :content => 'A nice short story.' )
132
+ end
133
+
134
+ post.delete
135
+ end
136
+
137
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:delete))
138
+ end
139
+
140
+ def test_model_destroy_method_generates_metrics
141
+ in_web_transaction do
142
+ post = NewRelic::Agent.disable_all_tracing do
143
+ Post.create( :title => 'Most of the Things', :content => 'Another short story.' )
144
+ end
145
+
146
+ post.destroy
147
+ end
148
+
149
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:destroy))
150
+ end
151
+
152
+ def test_model_destroy_uses_the_class_name_for_the_metric
153
+ in_web_transaction do
154
+ post = NewRelic::Agent.disable_all_tracing do
155
+ Post.create( :title => 'Some of the Things', :content => 'A shorter story.' )
156
+ end
157
+
158
+ post.destroy
159
+ end
160
+
161
+ assert_datastore_metrics_recorded_exclusive(expected_metrics_for_operation(:destroy))
162
+ end
163
+
164
+ def test_slow_queries_get_an_explain_plan
165
+ with_config( :'transaction_tracer.explain_threshold' => -0.01,
166
+ :'transaction_tracer.record_sql' => 'raw' ) do
167
+ segment = last_segment_for do
168
+ Post[11]
169
+ end
170
+ assert_match %r{select \* from `posts` where `id` = 11}i, segment.params[:sql]
171
+ assert_segment_has_explain_plan( segment )
172
+ end
173
+ end
174
+
175
+ def test_sql_is_recorded_in_tt_for_non_select
176
+ with_config(:'transaction_tracer.record_sql' => 'raw') do
177
+ segment = last_segment_for do
178
+ Post.create(:title => 'title', :content => 'content')
179
+ end
180
+ assert_match %r{insert into `posts` \([^\)]*\) values \([^\)]*\)}i, segment.params[:sql]
181
+ end
182
+ end
183
+
184
+ def test_no_explain_plans_with_single_threaded_connection
185
+ connect_opts = DB.opts
186
+ single_threaded_db = Sequel.connect(connect_opts.merge(:single_threaded => true))
187
+ create_tables(single_threaded_db)
188
+ model_class = Class.new(Sequel::Model(single_threaded_db[:posts]))
189
+
190
+ with_config(:'transaction_tracer.explain_threshold' => -0.01,
191
+ :'transaction_tracer.record_sql' => 'raw') do
192
+ segment = last_segment_for do
193
+ model_class[11]
194
+ end
195
+ assert_match %r{select \* from `posts` where `id` = 11}i, segment.params[:sql]
196
+ assert_equal([], segment.params[:explain_plan], "Should not capture explain plan with single-threaded connection pool")
197
+ end
198
+ end
199
+
200
+ def test_queries_can_get_explain_plan_with_obfuscated_sql
201
+ config = {
202
+ :'transaction_tracer.explain_threshold' => -0.01,
203
+ :'transaction_tracer.record_sql' => 'obfuscated'
204
+ }
205
+ with_config(config) do
206
+ segment = last_segment_for(:record_sql => :obfuscated) do
207
+ Post[11]
208
+ end
209
+ assert_match %r{select \* from `posts` where `id` = \?}i, segment.params[:sql]
210
+ assert_segment_has_explain_plan( segment )
211
+ end
212
+ end
213
+
214
+ def test_notices_sql_with_proper_metric_name_for_select
215
+ config = {
216
+ :'transaction_tracer.explain_threshold' => -0.01,
217
+ :'transaction_tracer.record_sql' => 'obfuscated'
218
+ }
219
+ with_config(config) do
220
+ in_web_transaction { Post.all }
221
+ expected_metric_name = "Datastore/operation/#{product_name}/select"
222
+ recorded_metric_names = NewRelic::Agent.agent.sql_sampler.sql_traces.values.map(&:database_metric_name)
223
+ assert recorded_metric_names.include? expected_metric_name
224
+ end
225
+ end
226
+ end
227
+
228
+ else
229
+ puts "Skipping tests in #{__FILE__} because unsupported Sequel version"
230
+ end