honkster-newrelic_rpm 2.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. data/CHANGELOG +462 -0
  2. data/LICENSE +37 -0
  3. data/README.rdoc +172 -0
  4. data/bin/mongrel_rpm +33 -0
  5. data/bin/newrelic +13 -0
  6. data/bin/newrelic_cmd +5 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +9 -0
  9. data/lib/new_relic/agent.rb +382 -0
  10. data/lib/new_relic/agent/agent.rb +741 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/error_collector.rb +131 -0
  14. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  15. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +92 -0
  16. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +45 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +409 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +58 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +22 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +40 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +9 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +319 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +17 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +22 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +98 -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/rails3/action_controller.rb +45 -0
  32. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +21 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -0
  34. data/lib/new_relic/agent/instrumentation/sunspot.rb +17 -0
  35. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +10 -0
  36. data/lib/new_relic/agent/method_tracer.rb +350 -0
  37. data/lib/new_relic/agent/sampler.rb +50 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +54 -0
  39. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +37 -0
  40. data/lib/new_relic/agent/samplers/memory_sampler.rb +142 -0
  41. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  42. data/lib/new_relic/agent/shim_agent.rb +25 -0
  43. data/lib/new_relic/agent/stats_engine.rb +24 -0
  44. data/lib/new_relic/agent/stats_engine/metric_stats.rb +118 -0
  45. data/lib/new_relic/agent/stats_engine/samplers.rb +83 -0
  46. data/lib/new_relic/agent/stats_engine/transactions.rb +149 -0
  47. data/lib/new_relic/agent/transaction_sampler.rb +330 -0
  48. data/lib/new_relic/agent/worker_loop.rb +81 -0
  49. data/lib/new_relic/collection_helper.rb +71 -0
  50. data/lib/new_relic/command.rb +85 -0
  51. data/lib/new_relic/commands/deployments.rb +105 -0
  52. data/lib/new_relic/commands/install.rb +81 -0
  53. data/lib/new_relic/control.rb +203 -0
  54. data/lib/new_relic/control/configuration.rb +149 -0
  55. data/lib/new_relic/control/frameworks/external.rb +13 -0
  56. data/lib/new_relic/control/frameworks/merb.rb +24 -0
  57. data/lib/new_relic/control/frameworks/rails.rb +126 -0
  58. data/lib/new_relic/control/frameworks/rails3.rb +60 -0
  59. data/lib/new_relic/control/frameworks/ruby.rb +36 -0
  60. data/lib/new_relic/control/frameworks/sinatra.rb +18 -0
  61. data/lib/new_relic/control/instrumentation.rb +95 -0
  62. data/lib/new_relic/control/logging_methods.rb +74 -0
  63. data/lib/new_relic/control/profiling.rb +24 -0
  64. data/lib/new_relic/control/server_methods.rb +88 -0
  65. data/lib/new_relic/delayed_job_injection.rb +27 -0
  66. data/lib/new_relic/histogram.rb +91 -0
  67. data/lib/new_relic/local_environment.rb +333 -0
  68. data/lib/new_relic/merbtasks.rb +6 -0
  69. data/lib/new_relic/metric_data.rb +42 -0
  70. data/lib/new_relic/metric_parser.rb +136 -0
  71. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  72. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  73. data/lib/new_relic/metric_parser/active_record.rb +28 -0
  74. data/lib/new_relic/metric_parser/apdex.rb +88 -0
  75. data/lib/new_relic/metric_parser/controller.rb +62 -0
  76. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  77. data/lib/new_relic/metric_parser/errors.rb +6 -0
  78. data/lib/new_relic/metric_parser/external.rb +50 -0
  79. data/lib/new_relic/metric_parser/mem_cache.rb +50 -0
  80. data/lib/new_relic/metric_parser/other_transaction.rb +36 -0
  81. data/lib/new_relic/metric_parser/view.rb +61 -0
  82. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  83. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  84. data/lib/new_relic/metric_spec.rb +67 -0
  85. data/lib/new_relic/metrics.rb +9 -0
  86. data/lib/new_relic/noticed_error.rb +24 -0
  87. data/lib/new_relic/rack/developer_mode.rb +257 -0
  88. data/lib/new_relic/rack/metric_app.rb +64 -0
  89. data/lib/new_relic/rack/mongrel_rpm.ru +26 -0
  90. data/lib/new_relic/rack/newrelic.yml +27 -0
  91. data/lib/new_relic/rack_app.rb +6 -0
  92. data/lib/new_relic/recipes.rb +82 -0
  93. data/lib/new_relic/stats.rb +368 -0
  94. data/lib/new_relic/timer_lib.rb +27 -0
  95. data/lib/new_relic/transaction_analysis.rb +119 -0
  96. data/lib/new_relic/transaction_sample.rb +586 -0
  97. data/lib/new_relic/url_rule.rb +14 -0
  98. data/lib/new_relic/version.rb +55 -0
  99. data/lib/new_relic_api.rb +276 -0
  100. data/lib/newrelic_rpm.rb +49 -0
  101. data/lib/tasks/all.rb +4 -0
  102. data/lib/tasks/install.rake +7 -0
  103. data/lib/tasks/tests.rake +15 -0
  104. data/newrelic.yml +246 -0
  105. data/newrelic_rpm.gemspec +254 -0
  106. data/recipes/newrelic.rb +6 -0
  107. data/test/active_record_fixtures.rb +55 -0
  108. data/test/config/newrelic.yml +48 -0
  109. data/test/config/test_control.rb +36 -0
  110. data/test/new_relic/agent/active_record_instrumentation_test.rb +286 -0
  111. data/test/new_relic/agent/agent_controller_test.rb +294 -0
  112. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  113. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  114. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  115. data/test/new_relic/agent/error_collector_test.rb +163 -0
  116. data/test/new_relic/agent/memcache_instrumentation_test.rb +103 -0
  117. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  118. data/test/new_relic/agent/metric_data_test.rb +53 -0
  119. data/test/new_relic/agent/metric_frame_test.rb +51 -0
  120. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  121. data/test/new_relic/agent/net_instrumentation_test.rb +77 -0
  122. data/test/new_relic/agent/rpm_agent_test.rb +142 -0
  123. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  124. data/test/new_relic/agent/stats_engine/samplers_test.rb +72 -0
  125. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  126. data/test/new_relic/agent/task_instrumentation_test.rb +188 -0
  127. data/test/new_relic/agent/testable_agent.rb +13 -0
  128. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  129. data/test/new_relic/agent/transaction_sample_test.rb +192 -0
  130. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  131. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  132. data/test/new_relic/control_test.rb +127 -0
  133. data/test/new_relic/deployments_api_test.rb +69 -0
  134. data/test/new_relic/environment_test.rb +75 -0
  135. data/test/new_relic/metric_parser_test.rb +226 -0
  136. data/test/new_relic/metric_spec_test.rb +177 -0
  137. data/test/new_relic/rack/episodes_test.rb +318 -0
  138. data/test/new_relic/shim_agent_test.rb +9 -0
  139. data/test/new_relic/stats_test.rb +312 -0
  140. data/test/new_relic/version_number_test.rb +89 -0
  141. data/test/test_contexts.rb +28 -0
  142. data/test/test_helper.rb +72 -0
  143. data/ui/helpers/developer_mode_helper.rb +359 -0
  144. data/ui/helpers/google_pie_chart.rb +49 -0
  145. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  146. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  147. data/ui/views/newrelic/_sample.rhtml +20 -0
  148. data/ui/views/newrelic/_segment.rhtml +29 -0
  149. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  150. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  151. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  152. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  153. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  154. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  155. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  156. data/ui/views/newrelic/_table.rhtml +12 -0
  157. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  158. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  159. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  160. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  161. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  162. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  163. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  164. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  165. data/ui/views/newrelic/file/images/textmate.png +0 -0
  166. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  167. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  168. data/ui/views/newrelic/file/stylesheets/style.css +484 -0
  169. data/ui/views/newrelic/index.rhtml +59 -0
  170. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  171. data/ui/views/newrelic/show_sample.rhtml +79 -0
  172. data/ui/views/newrelic/show_source.rhtml +3 -0
  173. data/ui/views/newrelic/threads.rhtml +52 -0
  174. metadata +307 -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,188 @@
1
+ # Run faster standalone
2
+ ENV['SKIP_RAILS'] = 'true'
3
+
4
+ require File.expand_path('../../../test_helper', __FILE__)
5
+
6
+ class TaskInstrumentationTest < Test::Unit::TestCase
7
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
8
+ extend TestContexts
9
+ attr_accessor :agent
10
+
11
+ with_running_agent do
12
+
13
+ should "run" do
14
+ run_task_inner 0
15
+ stat_names = %w[Controller/TaskInstrumentationTest/inner_task_0
16
+ HttpDispatcher
17
+ Apdex Apdex/TaskInstrumentationTest/inner_task_0].sort
18
+ expected_but_missing = stat_names - @agent.stats_engine.metrics
19
+ assert_equal 0, expected_but_missing.size, @agent.stats_engine.metrics.map { |n|
20
+ stat = @agent.stats_engine.get_stats_no_scope(n)
21
+ "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
22
+ }.join("\n ") + "\nmissing: #{expected_but_missing.inspect}"
23
+ assert_equal 0, @agent.stats_engine.get_stats_no_scope('Controller').call_count
24
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
25
+ end
26
+
27
+ should "run_recursive" do
28
+ run_task_inner(3)
29
+ assert_equal 1, @agent.stats_engine.lookup_stats(
30
+ 'Controller/TaskInstrumentationTest/inner_task_0',
31
+ 'Controller/TaskInstrumentationTest/inner_task_1').call_count
32
+ assert_equal 1, @agent.stats_engine.lookup_stats(
33
+ 'Controller/TaskInstrumentationTest/inner_task_1',
34
+ 'Controller/TaskInstrumentationTest/inner_task_2').call_count
35
+ assert_equal 1, @agent.stats_engine.lookup_stats(
36
+ 'Controller/TaskInstrumentationTest/inner_task_2',
37
+ 'Controller/TaskInstrumentationTest/inner_task_3').call_count
38
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
39
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_1').call_count
40
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_2').call_count
41
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_3').call_count
42
+ assert_equal 0, @agent.stats_engine.get_stats_no_scope('Controller').call_count
43
+ end
44
+
45
+ should "run_nested" do
46
+ run_task_outer(3)
47
+ @agent.stats_engine.metrics.sort.each do |n|
48
+ stat = @agent.stats_engine.get_stats_no_scope(n)
49
+ # puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
50
+ end
51
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/outer_task').call_count
52
+ assert_equal 2, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
53
+ end
54
+
55
+ should "reentrancy" do
56
+ assert_equal 0, NewRelic::Agent::BusyCalculator.busy_count
57
+ run_task_inner(1)
58
+ assert_equal 1, @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/TaskInstrumentationTest/inner_task_1').call_count
60
+ compare_metrics %w[
61
+ Controller/TaskInstrumentationTest/inner_task_0:Controller/TaskInstrumentationTest/inner_task_1
62
+ Controller/TaskInstrumentationTest/inner_task_0
63
+ Controller/TaskInstrumentationTest/inner_task_1
64
+ ], @agent.stats_engine.metrics.grep(/^Controller/)
65
+ end
66
+
67
+ should "transaction" do
68
+ assert_equal 0, @agent.transaction_sampler.scope_depth, "existing unfinished sample"
69
+ assert_nil @agent.transaction_sampler.last_sample
70
+ assert_equal @agent.transaction_sampler, @agent.stats_engine.instance_variable_get("@transaction_sampler")
71
+ run_task_outer(10)
72
+ assert_equal 0, @agent.transaction_sampler.scope_depth, "existing unfinished sample"
73
+ @agent.stats_engine.metrics.sort.each do |n|
74
+ stat = @agent.stats_engine.get_stats_no_scope(n)
75
+ # puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
76
+ end
77
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/outer_task').call_count
78
+ assert_equal 0, @agent.stats_engine.get_stats_no_scope('Controller').call_count
79
+ assert_equal 2, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/inner_task_0').call_count
80
+ assert_equal 0, @agent.transaction_sampler.scope_depth, "existing unfinished sample"
81
+ sample = @agent.transaction_sampler.last_sample
82
+ assert_not_nil sample
83
+ assert_not_nil sample.params[:cpu_time], "cpu time nil: \n#{sample}"
84
+ assert sample.params[:cpu_time] >= 0, "cpu time: #{sample.params[:cpu_time]},\n#{sample}"
85
+ assert_equal '10', sample.params[:request_params][:level]
86
+ end
87
+
88
+ should "abort" do
89
+ @acct = 'Redrocks'
90
+ perform_action_with_newrelic_trace(:name => 'hello', :force => true, :params => { :account => @acct}) do
91
+ self.class.inspect
92
+ NewRelic::Agent.abort_transaction!
93
+ end
94
+ # We record the controller metric still, but abort any transaction recording.
95
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/hello').call_count
96
+ assert_nil @agent.transaction_sampler.last_sample
97
+ end
98
+ should "block" do
99
+ assert_equal @agent, NewRelic::Agent.instance
100
+ @acct = 'Redrocks'
101
+ perform_action_with_newrelic_trace(:name => 'hello', :force => true, :params => { :account => @acct}) do
102
+ self.class.inspect
103
+ end
104
+ @agent.stats_engine.metrics.sort.each do |n|
105
+ stat = @agent.stats_engine.get_stats_no_scope(n)
106
+ #puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
107
+ end
108
+ assert_equal @agent, NewRelic::Agent.instance
109
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/TaskInstrumentationTest/hello').call_count
110
+ sample = @agent.transaction_sampler.last_sample
111
+ assert_not_nil sample
112
+ assert_equal 'Redrocks', sample.params[:request_params][:account]
113
+
114
+ end
115
+
116
+ should "error_handling" do
117
+ @agent.error_collector.ignore_error_filter
118
+ @agent.error_collector.harvest_errors([])
119
+ @agent.error_collector.expects(:notice_error).once
120
+ assert_equal @agent.error_collector, NewRelic::Agent.instance.error_collector
121
+ assert_raise RuntimeError do
122
+ run_task_exception
123
+ end
124
+ end
125
+
126
+ should "custom_params" do
127
+ @agent.error_collector.stubs(:enabled).returns(true)
128
+ @agent.error_collector.ignore_error_filter
129
+ @agent.error_collector.harvest_errors([])
130
+ assert_equal @agent.error_collector, NewRelic::Agent.instance.error_collector
131
+ assert_raise RuntimeError do
132
+ run_task_exception
133
+ end
134
+ errors = @agent.error_collector.harvest_errors([])
135
+ assert_equal 1, errors.size
136
+ error = errors.first
137
+ assert_equal "Controller/TaskInstrumentationTest/run_task_exception", error.path
138
+ assert_not_nil error.params[:stack_trace]
139
+ assert_not_nil error.params[:custom_params]
140
+ end
141
+
142
+ should "instrument_bg" do
143
+ run_background_job
144
+ stat_names = %w[OtherTransaction/Background/TaskInstrumentationTest/run_background_job
145
+ OtherTransaction/Background/all
146
+ OtherTransaction/all].sort
147
+
148
+ expected_but_missing = stat_names - @agent.stats_engine.metrics
149
+ assert_equal 0, expected_but_missing.size, @agent.stats_engine.metrics.map { |n|
150
+ stat = @agent.stats_engine.get_stats_no_scope(n)
151
+ "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
152
+ }.join("\n ") + "\nmissing: #{expected_but_missing.inspect}"
153
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('OtherTransaction/all').call_count
154
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('OtherTransaction/Background/all').call_count
155
+ end
156
+ end
157
+
158
+ private
159
+
160
+ def run_task_inner(n)
161
+ sleep 0.1
162
+ return if n == 0
163
+ assert_equal 1, NewRelic::Agent::BusyCalculator.busy_count
164
+ run_task_inner(n-1)
165
+ end
166
+
167
+ def run_task_outer(n=0)
168
+ assert_equal 1, NewRelic::Agent::BusyCalculator.busy_count
169
+ run_task_inner(n)
170
+ run_task_inner(n)
171
+ end
172
+
173
+ def run_task_exception
174
+ NewRelic::Agent.add_custom_parameters(:custom_one => 'one custom val')
175
+ assert_equal 1, NewRelic::Agent::BusyCalculator.busy_count
176
+ raise "This is an error"
177
+ end
178
+
179
+ def run_background_job
180
+ "This is a background job"
181
+ end
182
+
183
+ add_transaction_tracer :run_task_exception
184
+ add_transaction_tracer :run_task_inner, :name => 'inner_task_#{args[0]}'
185
+ add_transaction_tracer :run_task_outer, :name => 'outer_task', :params => '{ :level => args[0] }'
186
+ # Eventually we need th change this to :category => :task
187
+ add_transaction_tracer :run_background_job, :category => 'OtherTransaction/Background'
188
+ 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