genki-newrelic_rpm 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/CHANGELOG +316 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +156 -0
  4. data/README.md +138 -0
  5. data/Rakefile +22 -0
  6. data/bin/mongrel_rpm +33 -0
  7. data/bin/newrelic_cmd +4 -0
  8. data/cert/cacert.pem +34 -0
  9. data/genki-newrelic_rpm.gemspec +32 -0
  10. data/init.rb +38 -0
  11. data/install.rb +37 -0
  12. data/lib/new_relic/agent.rb +280 -0
  13. data/lib/new_relic/agent/agent.rb +627 -0
  14. data/lib/new_relic/agent/chained_call.rb +13 -0
  15. data/lib/new_relic/agent/collection_helper.rb +61 -0
  16. data/lib/new_relic/agent/error_collector.rb +125 -0
  17. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  18. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +83 -0
  19. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  20. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +368 -0
  21. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  22. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +132 -0
  23. data/lib/new_relic/agent/instrumentation/memcache.rb +21 -0
  24. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  25. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  26. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  27. data/lib/new_relic/agent/instrumentation/net.rb +12 -0
  28. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  29. data/lib/new_relic/agent/instrumentation/rack.rb +77 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  31. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  32. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +38 -0
  33. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  34. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  35. data/lib/new_relic/agent/method_tracer.rb +277 -0
  36. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  37. data/lib/new_relic/agent/sampler.rb +12 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  39. data/lib/new_relic/agent/samplers/memory_sampler.rb +137 -0
  40. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  41. data/lib/new_relic/agent/shim_agent.rb +21 -0
  42. data/lib/new_relic/agent/stats_engine.rb +24 -0
  43. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  44. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  45. data/lib/new_relic/agent/stats_engine/transactions.rb +155 -0
  46. data/lib/new_relic/agent/transaction_sampler.rb +319 -0
  47. data/lib/new_relic/agent/worker_loop.rb +118 -0
  48. data/lib/new_relic/commands/deployments.rb +145 -0
  49. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  50. data/lib/new_relic/control.rb +436 -0
  51. data/lib/new_relic/control/external.rb +13 -0
  52. data/lib/new_relic/control/merb.rb +22 -0
  53. data/lib/new_relic/control/rails.rb +143 -0
  54. data/lib/new_relic/control/ruby.rb +34 -0
  55. data/lib/new_relic/control/sinatra.rb +14 -0
  56. data/lib/new_relic/histogram.rb +89 -0
  57. data/lib/new_relic/local_environment.rb +285 -0
  58. data/lib/new_relic/merbtasks.rb +6 -0
  59. data/lib/new_relic/metric_data.rb +44 -0
  60. data/lib/new_relic/metric_parser.rb +120 -0
  61. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  62. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  63. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  64. data/lib/new_relic/metric_parser/controller.rb +54 -0
  65. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  66. data/lib/new_relic/metric_parser/errors.rb +6 -0
  67. data/lib/new_relic/metric_parser/external.rb +50 -0
  68. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  69. data/lib/new_relic/metric_parser/view.rb +61 -0
  70. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  71. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  72. data/lib/new_relic/metric_spec.rb +52 -0
  73. data/lib/new_relic/metrics.rb +7 -0
  74. data/lib/new_relic/noticed_error.rb +25 -0
  75. data/lib/new_relic/rack/metric_app.rb +56 -0
  76. data/lib/new_relic/rack/newrelic.ru +25 -0
  77. data/lib/new_relic/rack/newrelic.yml +25 -0
  78. data/lib/new_relic/rack_app.rb +5 -0
  79. data/lib/new_relic/recipes.rb +82 -0
  80. data/lib/new_relic/stats.rb +360 -0
  81. data/lib/new_relic/transaction_analysis.rb +121 -0
  82. data/lib/new_relic/transaction_sample.rb +583 -0
  83. data/lib/new_relic/version.rb +54 -0
  84. data/lib/new_relic_api.rb +315 -0
  85. data/lib/newrelic_rpm.rb +40 -0
  86. data/lib/tasks/all.rb +4 -0
  87. data/lib/tasks/install.rake +7 -0
  88. data/lib/tasks/tests.rake +13 -0
  89. data/newrelic.yml +214 -0
  90. data/recipes/newrelic.rb +6 -0
  91. data/test/active_record_fixtures.rb +55 -0
  92. data/test/config/newrelic.yml +46 -0
  93. data/test/config/test_control.rb +39 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +234 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +117 -0
  97. data/test/new_relic/agent/agent_test_controller.rb +44 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +118 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +155 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +335 -0
  103. data/test/new_relic/agent/metric_data_test.rb +56 -0
  104. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  105. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  106. data/test/new_relic/agent/net_instrumentation_test.rb +51 -0
  107. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  108. data/test/new_relic/agent/stats_engine/samplers_test.rb +78 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +177 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +67 -0
  111. data/test/new_relic/agent/testable_agent.rb +13 -0
  112. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  113. data/test/new_relic/agent/transaction_sample_test.rb +146 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +387 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +94 -0
  117. data/test/new_relic/deployments_api_test.rb +68 -0
  118. data/test/new_relic/environment_test.rb +75 -0
  119. data/test/new_relic/metric_parser_test.rb +172 -0
  120. data/test/new_relic/metric_spec_test.rb +177 -0
  121. data/test/new_relic/shim_agent_test.rb +9 -0
  122. data/test/new_relic/stats_test.rb +291 -0
  123. data/test/new_relic/version_number_test.rb +74 -0
  124. data/test/test_helper.rb +38 -0
  125. data/test/ui/newrelic_controller_test.rb +14 -0
  126. data/test/ui/newrelic_helper_test.rb +53 -0
  127. data/ui/controllers/newrelic_controller.rb +214 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +314 -0
  130. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  131. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  132. data/ui/views/newrelic/_sample.rhtml +15 -0
  133. data/ui/views/newrelic/_segment.rhtml +28 -0
  134. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  135. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  136. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  137. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  138. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  139. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  140. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  141. data/ui/views/newrelic/_table.rhtml +12 -0
  142. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  143. data/ui/views/newrelic/images/arrow-close.png +0 -0
  144. data/ui/views/newrelic/images/arrow-open.png +0 -0
  145. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  146. data/ui/views/newrelic/images/file_icon.png +0 -0
  147. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  148. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  149. data/ui/views/newrelic/images/textmate.png +0 -0
  150. data/ui/views/newrelic/index.rhtml +45 -0
  151. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  152. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  153. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  154. data/ui/views/newrelic/show_sample.rhtml +77 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +433 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +327 -0
@@ -0,0 +1,6 @@
1
+ # The capistrano recipes in plugins are automatically
2
+ # loaded from here. From gems, they are available from
3
+ # the lib directory. We have to make them available from
4
+ # both locations
5
+
6
+ require File.join(File.dirname(__FILE__),'..','lib','new_relic','recipes')
@@ -0,0 +1,55 @@
1
+ # Some test models
2
+ # These will get created and dropped during the active record tests
3
+ # Be sure and call setup and teardown
4
+ module ActiveRecordFixtures
5
+ extend self
6
+ def setup
7
+ ActiveRecordFixtures::Order.setup
8
+ ActiveRecordFixtures::Shipment.setup
9
+ end
10
+ def teardown
11
+ ActiveRecordFixtures::Shipment.teardown
12
+ ActiveRecordFixtures::Order.teardown
13
+ end
14
+ class Order < ActiveRecord::Base
15
+ self.table_name = 'newrelic_test_orders'
16
+ has_and_belongs_to_many :shipments, :class_name => 'ActiveRecordFixtures::Shipment'
17
+ def self.setup
18
+ connection.create_table self.table_name, :force => true do |t|
19
+ t.column :name, :string
20
+ end
21
+ end
22
+ def self.add_delay
23
+ # Introduce a 500 ms delay into db operations on Orders
24
+ def connection.log_info *args
25
+ sleep 0.5
26
+ super *args
27
+ end
28
+ end
29
+ def self.teardown
30
+ connection.drop_table table_name
31
+ def connection.log_info *args
32
+ super *args
33
+ end
34
+ end
35
+ end
36
+
37
+ class Shipment < ActiveRecord::Base
38
+ self.table_name = 'newrelic_test_shipment'
39
+ has_and_belongs_to_many :orders, :class_name => 'ActiveRecordFixtures::Order'
40
+ def self.setup
41
+ connection.create_table self.table_name, :force => true do |t|
42
+ t.column :order_id, :integer
43
+ end
44
+ connection.create_table 'orders_shipments', :force => true, :id => false do |t|
45
+ t.column :order_id, :integer
46
+ t.column :shipment_id, :integer
47
+ end
48
+ end
49
+
50
+ def self.teardown
51
+ connection.drop_table 'orders_shipments'
52
+ connection.drop_table self.table_name
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,46 @@
1
+ # This is the config file loaded when running unit tests locally.
2
+ common: &default_settings
3
+ transaction_tracer:
4
+ enabled: true
5
+ record_sql: raw
6
+
7
+ test:
8
+ <<: *default_settings
9
+ host: localhost
10
+ port: 3000
11
+ log_level: debug
12
+ capture_params: true
13
+ enabled: false
14
+ developer: true
15
+ monitor_daemons: false
16
+ disable_samplers: true
17
+ api_host: 127.0.0.1
18
+ api_port: 443
19
+ message: '<%= generated_for_user %>'
20
+ license_key: '<%= license_key %>'
21
+ agent_enabled: auto
22
+ apdex_t: 1.1
23
+
24
+ # Some fixtures for newrelic.yml parsing tests
25
+ erb_value: <%= 'hey'*3 %>
26
+ tval: true
27
+ tstring: 'true'
28
+ fval: false
29
+ yval: yes
30
+ sval: sure
31
+
32
+ # checks for loading classes on the background thread
33
+ check_bg_loading: true
34
+
35
+ transaction_tracer:
36
+ record_sql: raw
37
+ transaction_threshold: Apdex_f # case insensitive
38
+ explain_threshold: 0.1
39
+ explain_enabled: true
40
+ stack_trace_threshold: 0.1
41
+
42
+ error_collector:
43
+ enabled: true
44
+ capture_source: true
45
+
46
+
@@ -0,0 +1,39 @@
1
+ require 'new_relic/control/rails'
2
+ require 'new_relic/agent/agent_test_controller'
3
+
4
+ class NewRelic::Control::Test < NewRelic::Control::Rails #:nodoc:
5
+ def env
6
+ 'test'
7
+ end
8
+ def app
9
+ :rails
10
+ end
11
+ def config_file
12
+ File.join(File.dirname(__FILE__), "newrelic.yml")
13
+ end
14
+ def initialize local_env
15
+ super local_env
16
+ setup_log
17
+ end
18
+ # when running tests, don't write out stderr
19
+ def log!(msg, level=:info)
20
+ log.send level, msg if log
21
+ end
22
+
23
+ # Add the default route in case it's missing. Need it for testing.
24
+ def install_devmode_route
25
+ super
26
+ ActionController::Routing::RouteSet.class_eval do
27
+ return if defined? draw_without_test_route
28
+ def draw_with_test_route
29
+ draw_without_test_route do | map |
30
+ map.connect ':controller/:action/:id'
31
+ yield map
32
+ end
33
+ end
34
+ alias_method_chain :draw, :test_route
35
+ end
36
+ # Force the routes to be reloaded
37
+ ActionController::Routing::Routes.reload!
38
+ end
39
+ end
@@ -0,0 +1,234 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'active_record_fixtures'
3
+
4
+ class ActiveRecordInstrumentationTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ super
8
+ NewRelic::Agent.manual_start
9
+ NewRelic::Agent.instance.stats_engine.clear_stats
10
+ ActiveRecordFixtures.setup
11
+ NewRelic::Agent.instance.transaction_sampler.harvest
12
+ rescue
13
+ puts e
14
+ puts e.backtrace.join("\n")
15
+ end
16
+
17
+ def teardown
18
+ super
19
+ ActiveRecordFixtures.teardown
20
+ end
21
+ def test_agent_setup
22
+ assert NewRelic::Agent.instance.class == NewRelic::Agent::Agent
23
+ end
24
+ def test_finder
25
+ ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
26
+ ActiveRecordFixtures::Order.find(:all)
27
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
28
+ assert_equal 1, s.call_count
29
+ ActiveRecordFixtures::Order.find_all_by_name "jeff"
30
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
31
+ assert_equal 2, s.call_count
32
+ end
33
+
34
+ # multiple duplicate find calls should only cause metric trigger on the first
35
+ # call. the others are ignored.
36
+ def test_query_cache
37
+ ActiveRecordFixtures::Order.cache do
38
+ m = ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
39
+ ActiveRecordFixtures::Order.find(:all)
40
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
41
+ assert_equal 1, s.call_count
42
+
43
+ 10.times { ActiveRecordFixtures::Order.find m.id }
44
+ end
45
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
46
+ assert_equal 2, s.call_count
47
+ end
48
+
49
+ def test_metric_names
50
+ m = ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
51
+ m = ActiveRecordFixtures::Order.find(m.id)
52
+ m.id = 999
53
+ m.save!
54
+
55
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
56
+ # This doesn't work on hudson because the sampler metrics creep in.
57
+ # metrics = NewRelic::Agent.instance.stats_engine.metrics.select { |mname| mname =~ /ActiveRecord\/ActiveRecordFixtures::Order\// }.sort
58
+ expected = %W[
59
+ ActiveRecord/all
60
+ ActiveRecord/create
61
+ ActiveRecord/find
62
+ ActiveRecord/ActiveRecordFixtures::Order/create
63
+ ActiveRecord/ActiveRecordFixtures::Order/find
64
+ ]
65
+ expected += %W[ActiveRecord/save ActiveRecord/ActiveRecordFixtures::Order/save] if NewRelic::Control.instance.rails_version < '2.1.0'
66
+ compare_metrics expected, metrics
67
+ assert_equal 1, NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find").call_count
68
+ assert_equal 1, NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/create").call_count
69
+ end
70
+ def test_join_metrics
71
+ m = ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
72
+ m = ActiveRecordFixtures::Order.find(m.id)
73
+ s = m.shipments.create
74
+ m.shipments.to_a
75
+ m.destroy
76
+
77
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
78
+ # This doesn't work on hudson because the sampler metrics creep in.
79
+ # metrics = NewRelic::Agent.instance.stats_engine.metrics.select { |mname| mname =~ /ActiveRecord\/ActiveRecordFixtures::Order\// }.sort
80
+ compare_metrics %W[
81
+ ActiveRecord/all
82
+ ActiveRecord/destroy
83
+ ActiveRecord/ActiveRecordFixtures::Order/destroy
84
+ Database/SQL/insert
85
+ Database/SQL/delete
86
+ ActiveRecord/create
87
+ ActiveRecord/find
88
+ ActiveRecord/ActiveRecordFixtures::Order/create
89
+ ActiveRecord/ActiveRecordFixtures::Order/find
90
+ ActiveRecord/ActiveRecordFixtures::Shipment/find
91
+ ActiveRecord/ActiveRecordFixtures::Shipment/create
92
+ ], metrics
93
+ assert_equal 1, NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find").call_count
94
+ assert_equal 1, NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Shipment/find").call_count
95
+ assert_equal 1, NewRelic::Agent.get_stats("Database/SQL/insert").call_count
96
+ assert_equal 1, NewRelic::Agent.get_stats("Database/SQL/delete").call_count
97
+ end
98
+ def test_direct_sql
99
+ list = ActiveRecordFixtures::Order.connection.select_rows "select * from #{ActiveRecordFixtures::Order.table_name}"
100
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
101
+ compare_metrics %W[
102
+ ActiveRecord/all
103
+ Database/SQL/select
104
+ ], metrics
105
+ assert_equal 1, NewRelic::Agent.get_stats("Database/SQL/select").call_count
106
+ end
107
+
108
+ def test_blocked_instrumentation
109
+ ActiveRecordFixtures::Order.add_delay
110
+ NewRelic::Agent.disable_all_tracing do
111
+ ActiveRecordFixtures::Order.find(:all)
112
+ end
113
+ assert_nil NewRelic::Agent.instance.transaction_sampler.last_sample
114
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
115
+ compare_metrics [], metrics
116
+ end
117
+ def test_run_explains
118
+ ActiveRecordFixtures::Order.add_delay
119
+ ActiveRecordFixtures::Order.find(:all)
120
+
121
+ sample = NewRelic::Agent.instance.transaction_sampler.last_sample
122
+
123
+ segment = sample.root_segment.called_segments.first.called_segments.first
124
+ assert_match /^SELECT \* FROM ["`]?#{ActiveRecordFixtures::Order.table_name}["`]?$/i, segment.params[:sql].strip
125
+ NewRelic::TransactionSample::Segment.any_instance.expects(:explain_sql).returns([])
126
+ sample = sample.prepare_to_send(:obfuscate_sql => true, :explain_enabled => true, :explain_sql => 0.0)
127
+ segment = sample.root_segment.called_segments.first.called_segments.first
128
+ end
129
+ def test_prepare_to_send
130
+ ActiveRecordFixtures::Order.add_delay
131
+ ActiveRecordFixtures::Order.find(:all)
132
+
133
+ sample = NewRelic::Agent.instance.transaction_sampler.last_sample
134
+ #
135
+ sql_segment = sample.root_segment.called_segments.first.called_segments.first
136
+ assert_match /^SELECT /, sql_segment.params[:sql]
137
+ assert sql_segment.duration > 0.0, "Segment duration must be greater than zero."
138
+ sample = sample.prepare_to_send(:record_sql => :raw, :explain_enabled => true, :explain_sql => 0.0)
139
+ sql_segment = sample.root_segment.called_segments.first.called_segments.first
140
+ assert_match /^SELECT /, sql_segment.params[:sql]
141
+ explanations = sql_segment.params[:explanation]
142
+ if isMysql? || isPostgres?
143
+ assert_not_nil explanations, "No explains in segment: #{sql_segment}"
144
+ assert_equal 1, explanations.size,"No explains in segment: #{sql_segment}"
145
+ assert_equal 1, explanations.first.size
146
+ end
147
+ end
148
+ def test_transaction
149
+ ActiveRecordFixtures::Order.add_delay
150
+ ActiveRecordFixtures::Order.find(:all)
151
+
152
+ sample = NewRelic::Agent.instance.transaction_sampler.last_sample
153
+
154
+ sample = sample.prepare_to_send(:obfuscate_sql => true, :explain_enabled => true, :explain_sql => 0.0)
155
+ segment = sample.root_segment.called_segments.first.called_segments.first
156
+ assert_nil segment.params[:sql], "SQL should have been removed."
157
+ explanations = segment.params[:explanation]
158
+ if isMysql? || isPostgres?
159
+ assert_not_nil explanations, "No explains in segment: #{segment}"
160
+ assert_equal 1, explanations.size,"No explains in segment: #{segment}"
161
+ assert_equal 1, explanations.first.size
162
+ end
163
+ if isPostgres?
164
+ assert_equal Array, explanations.class
165
+ assert_equal Array, explanations[0].class
166
+ assert_equal Array, explanations[0][0].class
167
+ assert_match /Seq Scan on test_data/, explanations[0][0].join(";")
168
+ elsif isMysql?
169
+ assert_equal "1;SIMPLE;#{ActiveRecordFixtures::Order.table_name};ALL;;;;;1;", explanations.first.first.join(";")
170
+ end
171
+
172
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
173
+ assert_equal 1, s.call_count
174
+ end
175
+ # These are only valid for rails 2.1 and later
176
+ if NewRelic::Control.instance.rails_version >= NewRelic::VersionNumber.new("2.1.0")
177
+ ActiveRecordFixtures::Order.class_eval do
178
+ named_scope :jeffs, :conditions => { :name => 'Jeff' }
179
+ end
180
+ def test_named_scope
181
+ ActiveRecordFixtures::Order.create :name => 'Jeff'
182
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
183
+ before_count = s.call_count
184
+ x = ActiveRecordFixtures::Order.jeffs.find(:all)
185
+ assert_equal 1, x.size
186
+ se = NewRelic::Agent.instance.stats_engine
187
+ assert_equal before_count+1, s.call_count
188
+ end
189
+ end
190
+
191
+ # This is to make sure the all metric is recorded for exceptional cases
192
+ def test_error_handling
193
+ # have the AR select throw an error
194
+ ActiveRecordFixtures::Order.connection.stubs(:log_info).with do | sql, x, y |
195
+ raise "Error" if sql =~ /select/
196
+ true
197
+ end
198
+ ActiveRecordFixtures::Order.connection.select_rows "select * from #{ActiveRecordFixtures::Order.table_name}" rescue nil
199
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
200
+ compare_metrics %W[
201
+ ActiveRecord/all
202
+ Database/SQL/select
203
+ ], metrics
204
+ assert_equal 1, NewRelic::Agent.get_stats("Database/SQL/select").call_count
205
+ assert_equal 1, NewRelic::Agent.get_stats("ActiveRecord/all").call_count
206
+ end
207
+
208
+ def test_rescue_handling
209
+ begin
210
+ ActiveRecordFixtures::Order.transaction do
211
+ raise ActiveRecord::ActiveRecordError.new('preserve-me!')
212
+ end
213
+ rescue ActiveRecord::ActiveRecordError => e
214
+ assert_equal 'preserve-me!', e.message
215
+ rescue
216
+ fail "Rescue2: Got something COMPLETELY unexpected: $!:#{$!.inspect}"
217
+ end
218
+
219
+ end
220
+
221
+ private
222
+
223
+ def compare_metrics expected_list, actual_list
224
+ actual = Set.new actual_list
225
+ expected = Set.new expected_list
226
+ assert_equal expected, actual, "extra: #{(actual - expected).to_a.join(", ")}; missing: #{(expected - actual).to_a.join(", ")}"
227
+ end
228
+ def isPostgres?
229
+ ActiveRecordFixtures::Order.configurations[RAILS_ENV]['adapter'] =~ /postgres/
230
+ end
231
+ def isMysql?
232
+ ActiveRecordFixtures::Order.connection.class.name =~ /mysql/i
233
+ end
234
+ end
@@ -0,0 +1,107 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'action_controller/base'
3
+
4
+ class AgentControllerTest < ActionController::TestCase
5
+
6
+ self.controller_class = NewRelic::Agent::AgentTestController
7
+
8
+ attr_accessor :agent
9
+
10
+ # Normally you can do this with #setup but for some reason in rails 2.0.2
11
+ # setup is not called.
12
+ def initialize name
13
+ super name
14
+ Thread.current[:newrelic_ignore_controller] = nil
15
+ NewRelic::Agent.manual_start
16
+ @agent = NewRelic::Agent.instance
17
+ # @agent.instrument_app
18
+ agent.transaction_sampler.harvest
19
+ NewRelic::Agent::AgentTestController.class_eval do
20
+ newrelic_ignore :only => [:action_to_ignore, :entry_action, :base_action]
21
+ newrelic_ignore_apdex :only => :action_to_ignore_apdex
22
+ end
23
+ end
24
+
25
+ def teardown
26
+ Thread.current[:newrelic_ignore_controller] = nil
27
+ @agent.stats_engine.clear_stats
28
+ super
29
+ end
30
+
31
+ def test_metric__ignore
32
+ engine = @agent.stats_engine
33
+ get :action_to_ignore
34
+ assert_equal true, Thread.current[:newrelic_ignore_controller]
35
+ end
36
+ def test_metric__ignore_base
37
+ engine = @agent.stats_engine
38
+ get :base_action
39
+ assert_equal true, Thread.current[:newrelic_ignore_controller]
40
+ end
41
+ def test_metric__no_ignore
42
+ engine = @agent.stats_engine
43
+ path = 'new_relic/agent/agent_test/index'
44
+ index_stats = engine.get_stats_no_scope("Controller/#{path}")
45
+ index_apdex_stats = engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
46
+ assert_difference 'index_stats.call_count' do
47
+ assert_difference 'index_apdex_stats.call_count' do
48
+ get :index
49
+ end
50
+ end
51
+ assert_nil Thread.current[:newrelic_ignore_controller]
52
+ end
53
+ def test_metric__ignore_apdex
54
+ engine = @agent.stats_engine
55
+ path = 'new_relic/agent/agent_test/action_to_ignore_apdex'
56
+ cpu_stats = engine.get_stats_no_scope("ControllerCPU/#{path}")
57
+ index_stats = engine.get_stats_no_scope("Controller/#{path}")
58
+ index_apdex_stats = engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
59
+ assert_difference 'index_stats.call_count' do
60
+ assert_no_difference 'index_apdex_stats.call_count' do
61
+ get :action_to_ignore_apdex
62
+ end
63
+ end
64
+ assert_nil Thread.current[:newrelic_ignore_controller]
65
+
66
+ end
67
+ def test_metric__dispatched
68
+ engine = @agent.stats_engine
69
+ get :entry_action
70
+ assert_nil Thread.current[:newrelic_ignore_controller]
71
+ assert_nil engine.lookup_stat('Controller/agent_test/entry_action')
72
+ assert_nil engine.lookup_stat('Controller/agent_test_controller/entry_action')
73
+ assert_nil engine.lookup_stat('Controller/AgentTestController/entry_action')
74
+ assert_nil engine.lookup_stat('Controller/NewRelic::Agent::AgentTestController/internal_action')
75
+ assert_nil engine.lookup_stat('Controller/NewRelic::Agent::AgentTestController_controller/internal_action')
76
+ assert_not_nil engine.lookup_stat('Controller/NewRelic::Agent::AgentTestController/internal_traced_action')
77
+ end
78
+ def test_action_instrumentation
79
+ begin
80
+ get :index, :foo => 'bar'
81
+ assert_match /bar/, @response.body
82
+ #rescue ActionController::RoutingError
83
+ # you might get here if you don't have the default route installed.
84
+ end
85
+ end
86
+
87
+ def test_controller_params
88
+
89
+ assert agent.transaction_sampler
90
+
91
+ num_samples = NewRelic::Agent.instance.transaction_sampler.samples.length
92
+
93
+ assert_equal "[FILTERED]", @controller._filter_parameters({'social_security_number' => 'test'})['social_security_number']
94
+
95
+ get :index, 'social_security_number' => "001-555-1212"
96
+
97
+ samples = agent.transaction_sampler.samples
98
+
99
+ agent.transaction_sampler.expects(:notice_transaction).never
100
+
101
+ assert_equal num_samples + 1, samples.length
102
+
103
+ assert_equal "[FILTERED]", samples.last.params[:request_params]["social_security_number"]
104
+
105
+ end
106
+
107
+ end