factorylabs-newrelic_rpm 2.10.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/CHANGELOG +354 -0
  2. data/LICENSE +37 -0
  3. data/README-2.10.2.2 +10 -0
  4. data/README.md +138 -0
  5. data/bin/mongrel_rpm +33 -0
  6. data/bin/newrelic_cmd +4 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +45 -0
  9. data/lib/new_relic/agent.rb +315 -0
  10. data/lib/new_relic/agent/agent.rb +647 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +86 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/collection_helper.rb +66 -0
  14. data/lib/new_relic/agent/error_collector.rb +117 -0
  15. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  16. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +91 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +389 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +12 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +199 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +21 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +109 -0
  28. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  30. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  31. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  32. data/lib/new_relic/agent/method_tracer.rb +348 -0
  33. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  34. data/lib/new_relic/agent/sampler.rb +46 -0
  35. data/lib/new_relic/agent/samplers/cpu_sampler.rb +50 -0
  36. data/lib/new_relic/agent/samplers/memory_sampler.rb +141 -0
  37. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +23 -0
  38. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  39. data/lib/new_relic/agent/shim_agent.rb +21 -0
  40. data/lib/new_relic/agent/stats_engine.rb +22 -0
  41. data/lib/new_relic/agent/stats_engine/metric_stats.rb +116 -0
  42. data/lib/new_relic/agent/stats_engine/samplers.rb +74 -0
  43. data/lib/new_relic/agent/stats_engine/transactions.rb +154 -0
  44. data/lib/new_relic/agent/transaction_sampler.rb +315 -0
  45. data/lib/new_relic/agent/worker_loop.rb +118 -0
  46. data/lib/new_relic/commands/deployments.rb +145 -0
  47. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  48. data/lib/new_relic/control.rb +484 -0
  49. data/lib/new_relic/control/external.rb +13 -0
  50. data/lib/new_relic/control/merb.rb +24 -0
  51. data/lib/new_relic/control/rails.rb +151 -0
  52. data/lib/new_relic/control/ruby.rb +36 -0
  53. data/lib/new_relic/control/sinatra.rb +14 -0
  54. data/lib/new_relic/histogram.rb +89 -0
  55. data/lib/new_relic/local_environment.rb +299 -0
  56. data/lib/new_relic/merbtasks.rb +6 -0
  57. data/lib/new_relic/metric_data.rb +42 -0
  58. data/lib/new_relic/metric_parser.rb +124 -0
  59. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  60. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  61. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  62. data/lib/new_relic/metric_parser/controller.rb +54 -0
  63. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  64. data/lib/new_relic/metric_parser/errors.rb +6 -0
  65. data/lib/new_relic/metric_parser/external.rb +50 -0
  66. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  67. data/lib/new_relic/metric_parser/other_transaction.rb +15 -0
  68. data/lib/new_relic/metric_parser/view.rb +61 -0
  69. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  70. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  71. data/lib/new_relic/metric_spec.rb +67 -0
  72. data/lib/new_relic/metrics.rb +7 -0
  73. data/lib/new_relic/noticed_error.rb +23 -0
  74. data/lib/new_relic/rack/metric_app.rb +56 -0
  75. data/lib/new_relic/rack/mongrel_rpm.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +26 -0
  77. data/lib/new_relic/rack_app.rb +5 -0
  78. data/lib/new_relic/recipes.rb +82 -0
  79. data/lib/new_relic/stats.rb +362 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +671 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +276 -0
  84. data/lib/newrelic_rpm.rb +45 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +15 -0
  88. data/newrelic.yml +227 -0
  89. data/newrelic_rpm.gemspec +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 +38 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +268 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +254 -0
  96. data/test/new_relic/agent/agent_test_controller.rb +78 -0
  97. data/test/new_relic/agent/busy_calculator_test.rb +79 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  100. data/test/new_relic/agent/error_collector_test.rb +173 -0
  101. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  102. data/test/new_relic/agent/metric_data_test.rb +56 -0
  103. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  104. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  105. data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
  106. data/test/new_relic/agent/rpm_agent_test.rb +125 -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 +88 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +177 -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 +186 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +404 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +110 -0
  117. data/test/new_relic/delayed_job_test.rb +108 -0
  118. data/test/new_relic/deployments_api_test.rb +68 -0
  119. data/test/new_relic/environment_test.rb +75 -0
  120. data/test/new_relic/metric_parser_test.rb +172 -0
  121. data/test/new_relic/metric_spec_test.rb +177 -0
  122. data/test/new_relic/shim_agent_test.rb +9 -0
  123. data/test/new_relic/stats_test.rb +291 -0
  124. data/test/new_relic/version_number_test.rb +76 -0
  125. data/test/test_helper.rb +53 -0
  126. data/test/ui/newrelic_controller_test.rb +14 -0
  127. data/test/ui/newrelic_helper_test.rb +53 -0
  128. data/ui/controllers/newrelic_controller.rb +220 -0
  129. data/ui/helpers/google_pie_chart.rb +49 -0
  130. data/ui/helpers/newrelic_helper.rb +320 -0
  131. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  132. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  133. data/ui/views/newrelic/_sample.rhtml +19 -0
  134. data/ui/views/newrelic/_segment.rhtml +28 -0
  135. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  136. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  137. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  138. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  139. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  140. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  141. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  142. data/ui/views/newrelic/_table.rhtml +12 -0
  143. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  144. data/ui/views/newrelic/images/arrow-close.png +0 -0
  145. data/ui/views/newrelic/images/arrow-open.png +0 -0
  146. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  147. data/ui/views/newrelic/images/file_icon.png +0 -0
  148. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  149. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  150. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  151. data/ui/views/newrelic/images/textmate.png +0 -0
  152. data/ui/views/newrelic/index.rhtml +57 -0
  153. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  154. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  155. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  156. data/ui/views/newrelic/show_sample.rhtml +80 -0
  157. data/ui/views/newrelic/show_source.rhtml +3 -0
  158. data/ui/views/newrelic/stylesheets/style.css +484 -0
  159. data/ui/views/newrelic/threads.rhtml +52 -0
  160. metadata +238 -0
@@ -0,0 +1,184 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..', 'test_helper'))
2
+
3
+
4
+ class NewRelic::Agent::StatsEngine::TransactionsTest < Test::Unit::TestCase
5
+ def setup
6
+ NewRelic::Agent.manual_start
7
+ @engine = NewRelic::Agent::StatsEngine.new
8
+ rescue => e
9
+ puts e
10
+ puts e.backtrace.join("\n")
11
+ end
12
+ def teardown
13
+ @engine.harvest_timeslice_data({},{})
14
+ end
15
+
16
+ def test_scope
17
+ @engine.push_scope "scope1"
18
+ assert @engine.peek_scope.name == "scope1"
19
+
20
+ expected = @engine.push_scope "scope2"
21
+ @engine.pop_scope expected, 0
22
+
23
+ scoped = @engine.get_stats "a"
24
+ scoped.trace_call 3
25
+
26
+ assert scoped.total_call_time == 3
27
+ unscoped = @engine.get_stats "a"
28
+
29
+ assert scoped == @engine.get_stats("a")
30
+ assert unscoped.total_call_time == 3
31
+ end
32
+
33
+ def test_scope__overlap
34
+ NewRelic::Agent.instance.stubs(:stats_engine).returns(@engine)
35
+
36
+ @engine.scope_name = 'orlando'
37
+ self.class.trace_execution_scoped('disney', :deduct_call_time_from_parent => false) { sleep 0.1 }
38
+ orlando_disney = @engine.get_stats 'disney'
39
+
40
+ @engine.scope_name = 'anaheim'
41
+ self.class.trace_execution_scoped('disney', :deduct_call_time_from_parent => false) { sleep 0.1 }
42
+ anaheim_disney = @engine.get_stats 'disney'
43
+
44
+ disney = @engine.get_stats_no_scope "disney"
45
+
46
+ assert_not_same orlando_disney, anaheim_disney
47
+ assert_not_equal orlando_disney, anaheim_disney
48
+ assert_equal 1, orlando_disney.call_count
49
+ assert_equal 1, anaheim_disney.call_count
50
+ assert_same disney, orlando_disney.unscoped_stats
51
+ assert_same disney, anaheim_disney.unscoped_stats
52
+ assert_equal 2, disney.call_count
53
+ assert_equal disney.total_call_time, orlando_disney.total_call_time + anaheim_disney.total_call_time
54
+
55
+ end
56
+
57
+ def test_simplethrowcase(depth=0)
58
+
59
+ fail "doh" if depth == 10
60
+
61
+ scope = @engine.push_scope "scope#{depth}"
62
+
63
+ begin
64
+ test_simplethrowcase(depth+1)
65
+ rescue StandardError => e
66
+ if (depth != 0)
67
+ raise e
68
+ end
69
+ ensure
70
+ @engine.pop_scope scope, 0
71
+ end
72
+
73
+ if depth == 0
74
+ assert @engine.peek_scope.nil?
75
+ end
76
+ end
77
+
78
+
79
+ def test_scope_failure
80
+ scope1 = @engine.push_scope "scope1"
81
+ @engine.push_scope "scope2"
82
+
83
+ begin
84
+ @engine.pop_scope scope1
85
+ fail "Didn't throw when scope push/pop mismatched"
86
+ rescue
87
+ # success
88
+ end
89
+ end
90
+
91
+ def test_children_time
92
+ t1 = Time.now
93
+
94
+ expected1 = @engine.push_scope "a"
95
+ sleep 0.1
96
+ t2 = Time.now
97
+
98
+ expected2 = @engine.push_scope "b"
99
+ sleep 0.2
100
+ t3 = Time.now
101
+
102
+ expected = @engine.push_scope "c"
103
+ sleep 0.3
104
+ scope = @engine.pop_scope expected, Time.now - t3
105
+
106
+ t4 = Time.now
107
+
108
+ check_time_approximate 0, scope.children_time
109
+ check_time_approximate 0.3, @engine.peek_scope.children_time
110
+
111
+ sleep 0.1
112
+ t5 = Time.now
113
+
114
+ expected = @engine.push_scope "d"
115
+ sleep 0.2
116
+ scope = @engine.pop_scope expected, Time.now - t5
117
+
118
+ t6 = Time.now
119
+
120
+ check_time_approximate 0, scope.children_time
121
+
122
+ scope = @engine.pop_scope expected2, Time.now - t2
123
+ assert_equal scope.name, 'b'
124
+
125
+ check_time_approximate (t4 - t3) + (t6 - t5), scope.children_time
126
+
127
+ scope = @engine.pop_scope expected1, Time.now - t1
128
+ assert_equal scope.name, 'a'
129
+
130
+ check_time_approximate (t6 - t2), scope.children_time
131
+ end
132
+
133
+ def test_simple_start_transaction
134
+ assert @engine.peek_scope.nil?
135
+ scope = @engine.push_scope "scope"
136
+ @engine.start_transaction
137
+ assert !@engine.peek_scope.nil?
138
+ @engine.pop_scope scope, 0.01
139
+ assert @engine.peek_scope.nil?
140
+ @engine.end_transaction
141
+ assert @engine.peek_scope.nil?
142
+ end
143
+
144
+
145
+ # test for when the scope stack contains an element only used for tts and not metrics
146
+ def test_simple_tt_only_scope
147
+ scope1 = @engine.push_scope "a", 0, true
148
+ scope2 = @engine.push_scope "b", 10, false
149
+ scope3 = @engine.push_scope "c", 20, true
150
+
151
+ @engine.pop_scope scope3, 10
152
+ @engine.pop_scope scope2, 10
153
+ @engine.pop_scope scope1, 10
154
+
155
+ assert_equal 0, scope3.children_time
156
+ assert_equal 10, scope2.children_time
157
+ assert_equal 10, scope1.children_time
158
+ end
159
+
160
+ def test_double_tt_only_scope
161
+ scope1 = @engine.push_scope "a", 0, true
162
+ scope2 = @engine.push_scope "b", 10, false
163
+ scope3 = @engine.push_scope "c", 20, false
164
+ scope4 = @engine.push_scope "d", 30, true
165
+
166
+ @engine.pop_scope scope4, 10
167
+ @engine.pop_scope scope3, 10
168
+ @engine.pop_scope scope2, 10
169
+ @engine.pop_scope scope1, 10
170
+
171
+ assert_equal 0, scope4.children_time
172
+ assert_equal 10, scope3.children_time
173
+ assert_equal 10, scope2.children_time
174
+ assert_equal 10, scope1.children_time
175
+ end
176
+
177
+
178
+ private
179
+ def check_time_approximate(expected, actual)
180
+ assert((expected - actual).abs < 0.1, "Expected between #{expected - 0.1} and #{expected + 0.1}, got #{actual}")
181
+ end
182
+
183
+ end
184
+
@@ -0,0 +1,177 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class TaskInstrumentationTest < Test::Unit::TestCase
4
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
5
+ attr_accessor :agent
6
+ def setup
7
+ super
8
+ NewRelic::Agent.manual_start
9
+ @agent = NewRelic::Agent.instance
10
+ @agent.transaction_sampler.send :clear_builder
11
+ @agent.transaction_sampler.reset!
12
+ @agent.stats_engine.clear_stats
13
+ end
14
+ def teardown
15
+ NewRelic::Agent.shutdown
16
+ super
17
+ end
18
+
19
+ def test_run
20
+ run_task_inner 0
21
+ stat_names = %w[Controller/TaskInstrumentationTest/inner_task_0
22
+ Controller HttpDispatcher
23
+ Apdex Apdex/TaskInstrumentationTest/inner_task_0].sort
24
+ expected_but_missing = stat_names - @agent.stats_engine.metrics
25
+ assert_equal 0, expected_but_missing.size, @agent.stats_engine.metrics.map { |n|
26
+ stat = @agent.stats_engine.get_stats_no_scope(n)
27
+ "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
28
+ }.join("\n ") + "\nmissing: #{expected_but_missing.inspect}"
29
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller').call_count
30
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
31
+ end
32
+
33
+ def test_run_recursive
34
+ run_task_inner(3)
35
+ assert_equal 1, @agent.stats_engine.lookup_stats(
36
+ 'Controller/TaskInstrumentationTest/inner_task_0',
37
+ 'Controller/TaskInstrumentationTest/inner_task_1').call_count
38
+ assert_equal 1, @agent.stats_engine.lookup_stats(
39
+ 'Controller/TaskInstrumentationTest/inner_task_1',
40
+ 'Controller/TaskInstrumentationTest/inner_task_2').call_count
41
+ assert_equal 1, @agent.stats_engine.lookup_stats(
42
+ 'Controller/TaskInstrumentationTest/inner_task_2',
43
+ 'Controller/TaskInstrumentationTest/inner_task_3').call_count
44
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
45
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_1').call_count
46
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_2').call_count
47
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_3').call_count
48
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller').call_count
49
+ end
50
+
51
+ def test_run_nested
52
+ run_task_outer(3)
53
+ @agent.stats_engine.metrics.sort.each do |n|
54
+ stat = @agent.stats_engine.get_stats_no_scope(n)
55
+ # puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
56
+ end
57
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/outer_task').call_count
58
+ assert_equal 2, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
59
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller').call_count
60
+ end
61
+
62
+ def test_reentrancy
63
+ assert_equal 0, NewRelic::Agent::BusyCalculator.busy_count
64
+ run_task_inner(1)
65
+ compare_metrics %w[
66
+ Controller
67
+ Controller/TaskInstrumentationTest/inner_task_0:Controller/TaskInstrumentationTest/inner_task_1
68
+ Controller/TaskInstrumentationTest/inner_task_0
69
+ Controller/TaskInstrumentationTest/inner_task_1
70
+ ], @agent.stats_engine.metrics.grep(/^Controller/)
71
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
72
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_1').call_count
73
+ end
74
+
75
+ def test_transaction
76
+ assert_equal 0, @agent.transaction_sampler.scope_depth, "existing unfinished sample"
77
+ assert_nil @agent.transaction_sampler.last_sample
78
+ assert_equal @agent.transaction_sampler, @agent.stats_engine.instance_variable_get("@transaction_sampler")
79
+ run_task_outer(10)
80
+ @agent.stats_engine.metrics.sort.each do |n|
81
+ stat = @agent.stats_engine.get_stats_no_scope(n)
82
+ # puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
83
+ end
84
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/outer_task').call_count
85
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller').call_count
86
+ assert_equal 2, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
87
+ assert_equal 0, @agent.transaction_sampler.scope_depth, "existing unfinished sample"
88
+ sample = @agent.transaction_sampler.last_sample
89
+ assert_not_nil sample
90
+ assert_not_nil sample.params[:cpu_time], "cpu time nil: \n#{sample}"
91
+ assert sample.params[:cpu_time] >= 0, "cpu time: #{sample.params[:cpu_time]},\n#{sample}"
92
+ assert_equal '10', sample.params[:request_params][:level]
93
+ end
94
+
95
+ def test_abort
96
+ @acct = 'Redrocks'
97
+ perform_action_with_newrelic_trace(:name => 'hello', :force => true, :params => { :account => @acct}) do
98
+ RAILS_DEFAULT_LOGGER.info "Hello world"
99
+ NewRelic::Agent.abort_transaction!
100
+ end
101
+ # We record the controller metric still, but abort any transaction recording.
102
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/hello').call_count
103
+ assert_nil @agent.transaction_sampler.last_sample
104
+ end
105
+
106
+ def test_block
107
+ assert_equal @agent, NewRelic::Agent.instance
108
+ @acct = 'Redrocks'
109
+ perform_action_with_newrelic_trace(:name => 'hello', :force => true, :params => { :account => @acct}) do
110
+ RAILS_DEFAULT_LOGGER.info "Hello world"
111
+ end
112
+ @agent.stats_engine.metrics.sort.each do |n|
113
+ stat = @agent.stats_engine.get_stats_no_scope(n)
114
+ #puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
115
+ end
116
+ assert_equal @agent, NewRelic::Agent.instance
117
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/hello').call_count
118
+ sample = @agent.transaction_sampler.last_sample
119
+ assert_not_nil sample
120
+ assert_equal 'Redrocks', sample.params[:request_params][:account]
121
+
122
+ end
123
+
124
+ def test_error_handling
125
+ @agent.error_collector.ignore_error_filter
126
+ @agent.error_collector.harvest_errors([])
127
+ @agent.error_collector.expects(:notice_error).once
128
+ assert_equal @agent.error_collector, NewRelic::Agent.instance.error_collector
129
+ assert_raise RuntimeError do
130
+ run_task_exception
131
+ end
132
+ end
133
+
134
+ def test_instrument_bg
135
+ run_background_job
136
+ stat_names = %w[OtherTransaction/Background/TaskInstrumentationTest/run_background_job
137
+ OtherTransaction/Background/all
138
+ OtherTransaction/all].sort
139
+
140
+ expected_but_missing = stat_names - @agent.stats_engine.metrics
141
+ assert_equal 0, expected_but_missing.size, @agent.stats_engine.metrics.map { |n|
142
+ stat = @agent.stats_engine.get_stats_no_scope(n)
143
+ "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
144
+ }.join("\n ") + "\nmissing: #{expected_but_missing.inspect}"
145
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('OtherTransaction/all').call_count
146
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('OtherTransaction/Background/all').call_count
147
+ end
148
+ private
149
+
150
+ def run_task_inner(n)
151
+ sleep 0.1
152
+ return if n == 0
153
+ assert_equal 1, NewRelic::Agent::BusyCalculator.busy_count
154
+ run_task_inner(n-1)
155
+ end
156
+
157
+ def run_task_outer(n=0)
158
+ assert_equal 1, NewRelic::Agent::BusyCalculator.busy_count
159
+ run_task_inner(n)
160
+ run_task_inner(n)
161
+ end
162
+
163
+ def run_task_exception
164
+ assert_equal 1, NewRelic::Agent::BusyCalculator.busy_count
165
+ raise "This is an error"
166
+ end
167
+
168
+ def run_background_job
169
+ "This is a background job"
170
+ end
171
+
172
+ add_transaction_tracer :run_task_exception
173
+ add_transaction_tracer :run_task_inner, :name => 'inner_task_#{args[0]}'
174
+ add_transaction_tracer :run_task_outer, :name => 'outer_task', :params => '{ :level => args[0] }'
175
+ # Eventually we need th change this to :category => :task
176
+ add_transaction_tracer :run_background_job, :category => 'OtherTransaction/Background'
177
+ end
@@ -0,0 +1,13 @@
1
+
2
+ ##require 'new_relic/agent'
3
+
4
+
5
+ RAILS_ROOT='.' if !defined? RAILS_ROOT
6
+
7
+
8
+ class String
9
+ def titleize
10
+ self
11
+ end
12
+ end
13
+
@@ -0,0 +1,195 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @builder = NewRelic::Agent::TransactionSampleBuilder.new
7
+ end
8
+
9
+ def test_build_sample
10
+ build_segment("a") do
11
+ build_segment("aa") do
12
+ build_segment("aaa")
13
+ end
14
+ build_segment("ab") do
15
+ build_segment("aba") do
16
+ build_segment("abaa")
17
+ end
18
+ build_segment("aba")
19
+ build_segment("abc") do
20
+ build_segment("abca")
21
+ build_segment("abcd")
22
+ end
23
+ end
24
+ end
25
+ build_segment "b"
26
+ build_segment "c" do
27
+ build_segment "ca"
28
+ build_segment "cb" do
29
+ build_segment "cba"
30
+ end
31
+ end
32
+
33
+ @builder.finish_trace(Time.now.to_f)
34
+ validate_builder
35
+ end
36
+
37
+ def test_freeze
38
+ build_segment "a" do
39
+ build_segment "aa"
40
+ end
41
+
42
+ begin
43
+ builder.sample
44
+ assert false
45
+ rescue Exception => e
46
+ # expected
47
+ end
48
+
49
+ @builder.finish_trace(Time.now.to_f)
50
+
51
+ validate_builder
52
+
53
+ begin
54
+ build_segment "b"
55
+ assert_false
56
+ rescue TypeError => e
57
+ # expected
58
+ end
59
+ end
60
+
61
+ # this is really a test for transaction sample
62
+ def test_omit_segments_with
63
+ build_segment "Controller/my_controller/index" do
64
+ sleep 0.010
65
+
66
+ build_segment "Rails/Application Code Loading" do
67
+ sleep 0.020
68
+
69
+ build_segment "foo/bar" do
70
+ sleep 0.010
71
+ end
72
+ end
73
+
74
+ build_segment "a" do
75
+ build_segment "ab"
76
+ sleep 0.010
77
+ end
78
+ build_segment "b" do
79
+ build_segment "ba"
80
+ sleep 0.05
81
+ build_segment "bb"
82
+ build_segment "bc" do
83
+ build_segment "bca"
84
+ sleep 0.05
85
+ end
86
+ end
87
+ build_segment "c"
88
+ end
89
+ @builder.finish_trace(Time.now.to_f)
90
+
91
+ validate_builder false
92
+
93
+ sample = @builder.sample
94
+
95
+ should_be_a_copy = sample.omit_segments_with('OMIT NOTHING')
96
+ validate_segment should_be_a_copy.root_segment, false
97
+
98
+ assert sample.to_s == should_be_a_copy.to_s
99
+
100
+ without_code_loading = sample.omit_segments_with('Rails/Application Code Loading')
101
+ validate_segment without_code_loading.root_segment, false
102
+
103
+ # after we take out code loading, the delta should be approximately
104
+ # 30 milliseconds
105
+ delta = (sample.duration - without_code_loading.duration) * 1000
106
+
107
+ # Need to allow substantial headroom on the upper bound to prevent
108
+ # spurious errors.
109
+ assert delta >= 28, "delta #{delta} should be between 28 and 100"
110
+ # disable this test for a couple days:
111
+ assert delta <= 100, "delta #{delta} should be between 28 and 100"
112
+
113
+ # ensure none of the segments have this regex
114
+ without_code_loading.each_segment do |segment|
115
+ assert_nil segment.metric_name =~ /Rails\/Application Code Loading/
116
+ end
117
+ end
118
+ def test_unbalanced_handling
119
+ assert_raise RuntimeError do
120
+ build_segment("a") do
121
+ begin
122
+ build_segment("aa") do
123
+ build_segment("aaa") do
124
+ raise "a problem"
125
+ end
126
+ end
127
+ rescue; end
128
+ end
129
+ end
130
+ end
131
+ def test_marshal
132
+ build_segment "a" do
133
+ build_segment "ab"
134
+ end
135
+ build_segment "b" do
136
+ build_segment "ba"
137
+ build_segment "bb"
138
+ build_segment "bc" do
139
+ build_segment "bca"
140
+ end
141
+ end
142
+ build_segment "c"
143
+
144
+ @builder.finish_trace(Time.now.to_f)
145
+ validate_builder
146
+
147
+ dump = Marshal.dump @builder.sample
148
+ sample = Marshal.restore(dump)
149
+ validate_segment(sample.root_segment)
150
+ end
151
+
152
+ def test_parallel_first_level_segments
153
+ build_segment "a" do
154
+ build_segment "ab"
155
+ end
156
+ build_segment "b"
157
+ build_segment "c"
158
+
159
+ @builder.finish_trace(Time.now.to_f)
160
+ validate_builder
161
+ end
162
+
163
+ def validate_builder(check_names = true)
164
+ validate_segment @builder.sample.root_segment, check_names
165
+ end
166
+
167
+ def validate_segment(s, check_names = true)
168
+ p = s.parent_segment
169
+
170
+ unless p.nil? || p.metric_name == 'ROOT'
171
+ assert p.called_segments.include?(s)
172
+ assert_equal p.metric_name.length, s.metric_name.length - 1, "p: #{p.metric_name}, s: #{s.metric_name}" if check_names
173
+ assert p.metric_name < s.metric_name if check_names
174
+ assert p.entry_timestamp <= s.entry_timestamp
175
+ end
176
+
177
+ assert s.exit_timestamp >= s.entry_timestamp
178
+
179
+ children = s.called_segments
180
+ last_segment = s
181
+ children.each do |child|
182
+ assert child.metric_name > last_segment.metric_name if check_names
183
+ assert child.entry_timestamp >= last_segment.entry_timestamp
184
+ last_metric = child
185
+
186
+ validate_segment(child, check_names)
187
+ end
188
+ end
189
+
190
+ def build_segment(metric, time = 0, &proc)
191
+ @builder.trace_entry(metric, Time.now.to_f)
192
+ proc.call if proc
193
+ @builder.trace_exit(metric, Time.now.to_f)
194
+ end
195
+ end