techarch-newrelic_rpm 2.10.2

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 +335 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +159 -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/init.rb +38 -0
  10. data/install.rb +45 -0
  11. data/lib/new_relic/agent.rb +281 -0
  12. data/lib/new_relic/agent/agent.rb +636 -0
  13. data/lib/new_relic/agent/chained_call.rb +13 -0
  14. data/lib/new_relic/agent/collection_helper.rb +66 -0
  15. data/lib/new_relic/agent/error_collector.rb +121 -0
  16. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  17. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +88 -0
  18. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  19. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +437 -0
  20. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  21. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +167 -0
  22. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  23. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  24. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  25. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  26. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  27. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  28. data/lib/new_relic/agent/instrumentation/rack.rb +108 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  31. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +41 -0
  32. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  34. data/lib/new_relic/agent/method_tracer.rb +349 -0
  35. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  36. data/lib/new_relic/agent/sampler.rb +12 -0
  37. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  38. data/lib/new_relic/agent/samplers/memory_sampler.rb +138 -0
  39. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  40. data/lib/new_relic/agent/shim_agent.rb +21 -0
  41. data/lib/new_relic/agent/stats_engine.rb +22 -0
  42. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  43. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  44. data/lib/new_relic/agent/stats_engine/transactions.rb +152 -0
  45. data/lib/new_relic/agent/transaction_sampler.rb +310 -0
  46. data/lib/new_relic/agent/worker_loop.rb +118 -0
  47. data/lib/new_relic/commands/deployments.rb +145 -0
  48. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  49. data/lib/new_relic/control.rb +473 -0
  50. data/lib/new_relic/control/external.rb +13 -0
  51. data/lib/new_relic/control/merb.rb +22 -0
  52. data/lib/new_relic/control/rails.rb +145 -0
  53. data/lib/new_relic/control/ruby.rb +36 -0
  54. data/lib/new_relic/control/sinatra.rb +14 -0
  55. data/lib/new_relic/histogram.rb +89 -0
  56. data/lib/new_relic/local_environment.rb +328 -0
  57. data/lib/new_relic/merbtasks.rb +6 -0
  58. data/lib/new_relic/metric_data.rb +42 -0
  59. data/lib/new_relic/metric_parser.rb +124 -0
  60. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  61. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  62. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  63. data/lib/new_relic/metric_parser/controller.rb +54 -0
  64. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  65. data/lib/new_relic/metric_parser/errors.rb +6 -0
  66. data/lib/new_relic/metric_parser/external.rb +50 -0
  67. data/lib/new_relic/metric_parser/mem_cache.rb +12 -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 +52 -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/newrelic.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +25 -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 +361 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +666 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +313 -0
  84. data/lib/newrelic_rpm.rb +40 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +13 -0
  88. data/newrelic.yml +227 -0
  89. data/recipes/newrelic.rb +6 -0
  90. data/techarch-newrelic_rpm.gemspec +32 -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 +264 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +119 -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 +125 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +172 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +340 -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 +63 -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 +81 -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 +126 -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 +385 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +113 -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 +220 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +317 -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 +19 -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 +57 -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 +80 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +484 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +330 -0
@@ -0,0 +1,71 @@
1
+ module NewRelic::Agent
2
+ class StatsEngine
3
+ module Shim # :nodoc:
4
+ def add_sampler(*args); end
5
+ def add_harvest_sampler(*args); end
6
+ def spawn_sampler_thread(*args); end
7
+ end
8
+
9
+ module Samplers
10
+
11
+ POLL_PERIOD = 10
12
+
13
+ def spawn_sampler_thread
14
+
15
+ return if !@sampler_process.nil? && @sampler_process == $$
16
+
17
+ # start up a thread that will periodically poll for metric samples
18
+ @sampler_thread = Thread.new do
19
+ while true do
20
+ begin
21
+ sleep POLL_PERIOD
22
+ poll periodic_samplers
23
+ end
24
+ end
25
+ end
26
+ @sampler_thread['newrelic_label'] = 'Sampler Tasks'
27
+ @sampler_process = $$
28
+ end
29
+
30
+ # Add an instance of Sampler to be invoked about every 10 seconds on a background
31
+ # thread.
32
+ def add_sampler sampler
33
+ periodic_samplers << sampler
34
+ sampler.stats_engine = self
35
+ log.debug "Adding sampler #{sampler.id.to_s}"
36
+ end
37
+
38
+ # Add a sampler to be invoked just before each harvest.
39
+ def add_harvest_sampler sampler
40
+ harvest_samplers << sampler
41
+ sampler.stats_engine = self
42
+ log.debug "Adding harvest time sampler: #{sampler.id.to_s}"
43
+ end
44
+
45
+ private
46
+
47
+ # Call poll on each of the samplers. Remove
48
+ # the sampler if it raises.
49
+ def poll(samplers)
50
+ samplers.delete_if do |sampled_item|
51
+ begin
52
+ sampled_item.poll
53
+ false # it's okay. don't delete it.
54
+ rescue => e
55
+ log.error "Removing #{sampled_item} from list"
56
+ log.error e
57
+ log.debug e.backtrace.to_s
58
+ true # remove the sampler
59
+ end
60
+ end
61
+ end
62
+
63
+ def harvest_samplers
64
+ @harvest_samplers ||= []
65
+ end
66
+ def periodic_samplers
67
+ @periodic_samplers ||= []
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,152 @@
1
+ module NewRelic::Agent
2
+ class StatsEngine
3
+
4
+ # Defines methods that stub out the stats engine methods
5
+ # when the agent is disabled
6
+
7
+ class ScopeStackElement
8
+ attr_reader :name, :deduct_call_time_from_parent
9
+ attr_accessor :children_time
10
+ def initialize(name, deduct_call_time)
11
+ @name = name
12
+ @deduct_call_time_from_parent = deduct_call_time
13
+ @children_time = 0
14
+ end
15
+ end
16
+
17
+ module Transactions
18
+ module Shim # :nodoc:
19
+ def start_transaction; end
20
+ def end_transaction; end
21
+ def push_scope(*args); end
22
+ def transaction_sampler=(*args); end
23
+ def transaction_name=(*args); end
24
+ def transaction_name; end
25
+ def pop_scope(*args); end
26
+ end
27
+
28
+ def transaction_sampler= sampler
29
+ fail "Can't add a scope listener midflight in a transaction" if scope_stack.any?
30
+ @transaction_sampler = sampler
31
+ end
32
+
33
+ def remove_transaction_sampler(l)
34
+ @transaction_sampler = nil
35
+ end
36
+
37
+ def push_scope(metric, time = Time.now.to_f, deduct_call_time_from_parent = true)
38
+
39
+ stack = scope_stack
40
+ if collecting_gc?
41
+ if stack.empty?
42
+ # reset the gc time so we only include gc time spent during this call
43
+ @last_gc_timestamp = GC.time
44
+ @last_gc_count = GC.collections
45
+ else
46
+ capture_gc_time
47
+ end
48
+ end
49
+ if @transaction_sampler
50
+ @transaction_sampler.notice_first_scope_push(time) if stack.empty?
51
+ @transaction_sampler.notice_push_scope metric, time
52
+ end
53
+ scope = ScopeStackElement.new(metric, deduct_call_time_from_parent)
54
+ stack.push scope
55
+ scope
56
+ end
57
+
58
+ def pop_scope(expected_scope, duration, time=Time.now.to_f)
59
+ capture_gc_time if collecting_gc?
60
+ stack = scope_stack
61
+ scope = stack.pop
62
+ fail "unbalanced pop from blame stack, got #{scope ? scope.name : 'nil'}, expected #{expected_scope ? expected_scope.name : 'nil'}" if scope != expected_scope
63
+
64
+ if !stack.empty?
65
+ if scope.deduct_call_time_from_parent
66
+ stack.last.children_time += duration
67
+ else
68
+ stack.last.children_time += scope.children_time
69
+ end
70
+ end
71
+
72
+ if @transaction_sampler
73
+ @transaction_sampler.notice_pop_scope(scope.name, time)
74
+ @transaction_sampler.notice_scope_empty(time) if stack.empty?
75
+ end
76
+ scope
77
+ end
78
+
79
+ def peek_scope
80
+ scope_stack.last
81
+ end
82
+
83
+ # set the name of the transaction for the current thread, which will be used
84
+ # to define the scope of all traced methods called on this thread until the
85
+ # scope stack is empty.
86
+ #
87
+ # currently the transaction name is the name of the controller action that
88
+ # is invoked via the dispatcher, but conceivably we could use other transaction
89
+ # names in the future if the traced application does more than service http request
90
+ # via controller actions
91
+ def transaction_name=(transaction)
92
+ Thread::current[:newrelic_transaction_name] = transaction
93
+ end
94
+
95
+ def transaction_name
96
+ Thread::current[:newrelic_transaction_name]
97
+ end
98
+
99
+ def start_transaction
100
+ Thread::current[:newrelic_scope_stack] ||= []
101
+ end
102
+
103
+ # Try to clean up gracefully, otherwise we leave things hanging around on thread locals.
104
+ # If it looks like a transaction is still in progress, then maybe this is an inner transaction
105
+ # and is ignored.
106
+ #
107
+ def end_transaction
108
+ stack = scope_stack
109
+
110
+ if stack && stack.empty?
111
+ Thread::current[:newrelic_scope_stack] = nil
112
+ Thread::current[:newrelic_transaction_name] = nil
113
+ end
114
+ end
115
+
116
+ private
117
+
118
+ # Make sure we don't do this in a multi-threaded environment
119
+ def collecting_gc?
120
+ @@collecting_gc ||= GC.respond_to?(:time) && GC.respond_to?(:collections) && !NewRelic::Control.instance.multi_threaded?
121
+ end
122
+
123
+ # Assumes collecting_gc?
124
+ def capture_gc_time
125
+ # Skip this if we are already in this segment
126
+ return if !scope_stack.empty? && scope_stack.last.name == "GC/cumulative"
127
+ num_calls = GC.collections - @last_gc_count
128
+ elapsed = (GC.time - @last_gc_timestamp).to_f
129
+ @last_gc_timestamp = GC.time
130
+ @last_gc_count = GC.collections
131
+ if num_calls > 0
132
+ # µs to seconds
133
+ elapsed = elapsed / 1000000.0
134
+ # Allocate the GC time to a scope as if the GC just ended
135
+ # right now.
136
+ time = Time.now.to_f
137
+ gc_scope = push_scope("GC/cumulative", time - elapsed)
138
+ # GC stats are collected into a blamed metric which allows
139
+ # us to show the stats controller by controller
140
+ gc_stats = NewRelic::Agent.get_stats(gc_scope.name, true)
141
+ gc_stats.record_multiple_data_points(elapsed, num_calls)
142
+ pop_scope(gc_scope, elapsed, time)
143
+ end
144
+ end
145
+
146
+ def scope_stack
147
+ Thread::current[:newrelic_scope_stack] ||= []
148
+ end
149
+
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,310 @@
1
+
2
+ module NewRelic::Agent
3
+
4
+ class TransactionSampler
5
+
6
+ # Module defining methods stubbed out when the agent is disabled
7
+ module Shim #:nodoc:
8
+ def notice_first_scope_push(*args); end
9
+ def notice_push_scope(*args); end
10
+ def notice_pop_scope(*args); end
11
+ def notice_scope_empty(*args); end
12
+ end
13
+
14
+ BUILDER_KEY = :transaction_sample_builder
15
+
16
+ attr_accessor :stack_trace_threshold, :random_sampling, :sampling_rate
17
+ attr_reader :samples, :last_sample
18
+
19
+ def initialize
20
+ @samples = []
21
+ @harvest_count = 0
22
+ @max_samples = 100
23
+ @random_sample = nil
24
+ config = NewRelic::Control.instance
25
+ sampler_config = config.fetch('transaction_tracer', {})
26
+ @segment_limit = sampler_config.fetch('limit_segments', 4000)
27
+ @stack_trace_threshold = sampler_config.fetch('stack_trace_threshold', 0.500).to_f
28
+ @samples_lock = Mutex.new
29
+ end
30
+
31
+ def current_sample_id
32
+ b=builder
33
+ b and b.sample_id
34
+ end
35
+
36
+ def disable
37
+ NewRelic::Agent.instance.stats_engine.remove_transaction_sampler self
38
+ end
39
+
40
+ def sampling_rate=(val)
41
+ @sampling_rate = val
42
+ @harvest_count = rand(val)
43
+ end
44
+
45
+ def notice_first_scope_push(time)
46
+ if Thread::current[:record_tt] == false || !NewRelic::Agent.is_execution_traced?
47
+ Thread::current[BUILDER_KEY] = nil
48
+ else
49
+ Thread::current[BUILDER_KEY] = TransactionSampleBuilder.new(time)
50
+ end
51
+ end
52
+
53
+ def notice_push_scope(scope, time=Time.now.to_f)
54
+
55
+ return unless builder
56
+
57
+ builder.trace_entry(scope, time)
58
+
59
+ # in developer mode, capture the stack trace with the segment.
60
+ # this is cpu and memory expensive and therefore should not be
61
+ # turned on in production mode
62
+ if NewRelic::Control.instance.developer_mode?
63
+ segment = builder.current_segment
64
+ if segment
65
+ # Strip stack frames off the top that match /new_relic/agent/
66
+ trace = caller
67
+ while trace.first =~/\/lib\/new_relic\/agent\//
68
+ trace.shift
69
+ end
70
+
71
+ trace = trace[0..39] if trace.length > 40
72
+ segment[:backtrace] = trace
73
+ end
74
+ end
75
+ end
76
+
77
+ def scope_depth
78
+ return 0 unless builder
79
+
80
+ builder.scope_depth
81
+ end
82
+
83
+ def notice_pop_scope(scope, time = Time.now.to_f)
84
+ return unless builder
85
+ builder.trace_exit(scope, time)
86
+ end
87
+
88
+ # This is called when we are done with the transaction. We've
89
+ # unwound the stack to the top level.
90
+ def notice_scope_empty(time=Time.now.to_f)
91
+
92
+ last_builder = builder
93
+ return unless last_builder
94
+
95
+ last_builder.finish_trace(time)
96
+ reset_builder
97
+
98
+
99
+ @samples_lock.synchronize do
100
+ @last_sample = last_builder.sample
101
+
102
+ # We sometimes see "unanchored" transaction traces
103
+ if @last_sample.params[:path]
104
+ @random_sample = @last_sample if @random_sampling
105
+
106
+ # ensure we don't collect more than a specified number of samples in memory
107
+ @samples << @last_sample if NewRelic::Control.instance.developer_mode?
108
+ @samples.shift while @samples.length > @max_samples
109
+
110
+ if @slowest_sample.nil? || @slowest_sample.duration < @last_sample.duration
111
+ @slowest_sample = @last_sample
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ def notice_transaction(path, request, params)
118
+ return unless builder
119
+ builder.set_transaction_info(path, request, params)
120
+ end
121
+
122
+ def notice_profile(profile)
123
+ return unless builder
124
+ builder.set_profile(profile)
125
+ end
126
+
127
+ def notice_transaction_cpu_time(cpu_time)
128
+ return unless builder
129
+ builder.set_transaction_cpu_time(cpu_time)
130
+ end
131
+
132
+
133
+ # some statements (particularly INSERTS with large BLOBS
134
+ # may be very large; we should trim them to a maximum usable length
135
+ # config is the driver configuration for the connection
136
+ MAX_SQL_LENGTH = 16384
137
+ def notice_sql(sql, config, duration)
138
+ return unless builder
139
+ if Thread::current[:record_sql].nil? || Thread::current[:record_sql]
140
+ segment = builder.current_segment
141
+ if segment
142
+ current_sql = segment[:sql]
143
+ sql = current_sql + ";\n" + sql if current_sql
144
+
145
+ if sql.length > (MAX_SQL_LENGTH - 4)
146
+ sql = sql[0..MAX_SQL_LENGTH-4] + '...'
147
+ end
148
+
149
+ segment[:sql] = sql
150
+ segment[:connection_config] = config
151
+ segment[:backtrace] = caller.join("\n") if duration >= @stack_trace_threshold
152
+ end
153
+ end
154
+ end
155
+
156
+
157
+ # get the set of collected samples, merging into previous samples,
158
+ # and clear the collected sample list.
159
+
160
+ def harvest(previous = nil, slow_threshold = 2.0)
161
+ result = []
162
+ previous ||= []
163
+
164
+ previous = [previous] unless previous.is_a?(Array)
165
+
166
+ previous_slowest = previous.inject(nil) {|a,ts| (a) ? ((a.duration > ts.duration) ? a : ts) : ts}
167
+
168
+ @samples_lock.synchronize do
169
+
170
+ if @random_sampling
171
+ @harvest_count += 1
172
+
173
+ if (@harvest_count % @sampling_rate) == 0
174
+ result << @random_sample if @random_sample
175
+ else
176
+ @random_sample = nil # if we don't nil this out, then we won't send the slowest if slowest == @random_sample
177
+ end
178
+ end
179
+
180
+ slowest = @slowest_sample
181
+ @slowest_sample = nil
182
+
183
+ if slowest && slowest != @random_sample && slowest.duration >= slow_threshold
184
+ if previous_slowest.nil? || previous_slowest.duration < slowest.duration
185
+ result << slowest
186
+ else
187
+ result << previous_slowest
188
+ end
189
+ end
190
+
191
+ @random_sample = nil
192
+ @last_sample = nil
193
+ end
194
+ # Truncate the samples at 2100 segments. The UI will clamp them at 2000 segments anyway.
195
+ # This will save us memory and bandwidth.
196
+ result.each { |sample| sample.truncate(@segment_limit) }
197
+ result
198
+ end
199
+
200
+ # reset samples without rebooting the web server
201
+ def reset!
202
+ @samples = []
203
+ @last_sample = nil
204
+ end
205
+
206
+ private
207
+
208
+ def builder
209
+ Thread::current[BUILDER_KEY]
210
+ end
211
+ def reset_builder
212
+ Thread::current[BUILDER_KEY] = nil
213
+ end
214
+
215
+ end
216
+
217
+ # a builder is created with every sampled transaction, to dynamically
218
+ # generate the sampled data. It is a thread-local object, and is not
219
+ # accessed by any other thread so no need for synchronization.
220
+ class TransactionSampleBuilder
221
+ attr_reader :current_segment
222
+
223
+ include CollectionHelper
224
+
225
+ def initialize(time=Time.now.to_f)
226
+ @sample = NewRelic::TransactionSample.new(time)
227
+ @sample_start = time
228
+ @current_segment = @sample.root_segment
229
+ end
230
+
231
+ def sample_id
232
+ @sample.sample_id
233
+ end
234
+
235
+ def trace_entry(metric_name, time)
236
+ segment = @sample.create_segment(time - @sample_start, metric_name)
237
+ @current_segment.add_called_segment(segment)
238
+ @current_segment = segment
239
+ end
240
+
241
+ def trace_exit(metric_name, time)
242
+ if metric_name != @current_segment.metric_name
243
+ fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
244
+ end
245
+ @current_segment.end_trace(time - @sample_start)
246
+ @current_segment = @current_segment.parent_segment
247
+ end
248
+
249
+ def finish_trace(time)
250
+ # This should never get called twice, but in a rare case that we can't reproduce in house it does.
251
+ # log forensics and return gracefully
252
+ if @sample.frozen?
253
+ log = NewRelic::Control.instance.log
254
+
255
+ log.warn "Unexpected double-freeze of Transaction Trace Object."
256
+ log.info "Please send this diagnostic data to New Relic"
257
+ log.info @sample.to_s
258
+ return
259
+ end
260
+ @sample.root_segment.end_trace(time - @sample_start)
261
+ @sample.params[:custom_params] = normalize_params(NewRelic::Agent.instance.custom_params)
262
+ @sample.freeze
263
+ @current_segment = nil
264
+ end
265
+
266
+ def scope_depth
267
+ depth = -1 # have to account for the root
268
+ current = @current_segment
269
+
270
+ while(current)
271
+ depth += 1
272
+ current = current.parent_segment
273
+ end
274
+
275
+ depth
276
+ end
277
+
278
+ def freeze
279
+ @sample.freeze unless sample.frozen?
280
+ end
281
+
282
+ def set_profile(profile)
283
+ @sample.profile = profile
284
+ end
285
+
286
+ def set_transaction_info(path, request, params)
287
+ @sample.params[:path] = path
288
+
289
+ if NewRelic::Control.instance.capture_params
290
+ params = normalize_params params
291
+
292
+ @sample.params[:request_params].merge!(params)
293
+ @sample.params[:request_params].delete :controller
294
+ @sample.params[:request_params].delete :action
295
+ end
296
+ @sample.params[:uri] = request.path if request
297
+ end
298
+
299
+ def set_transaction_cpu_time(cpu_time)
300
+ @sample.params[:cpu_time] = cpu_time
301
+ end
302
+
303
+
304
+ def sample
305
+ fail "Not finished building" unless @sample.frozen?
306
+ @sample
307
+ end
308
+
309
+ end
310
+ end