onyx_newrelic_rpm 2.12.5

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