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,83 @@
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.object_id} is already registered. Don't call add_sampler directly anymore." if s.object_id == sampler.object_id
40
+ end
41
+ periodic_samplers << sampler
42
+ sampler.stats_engine = self
43
+ log.debug "Adding sampler #{sampler.object_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.each do |s|
49
+ raise "Sampler #{sampler.id} is already registered. Don't call add_sampler directly anymore." if s.id == sampler.id
50
+ end
51
+ harvest_samplers << sampler
52
+ sampler.stats_engine = self
53
+ log.debug "Adding harvest time sampler: #{sampler.id.to_s}"
54
+ end
55
+
56
+ private
57
+
58
+ # Call poll on each of the samplers. Remove
59
+ # the sampler if it raises.
60
+ def poll(samplers)
61
+ samplers.delete_if do |sampled_item|
62
+ begin
63
+ sampled_item.poll
64
+ false # it's okay. don't delete it.
65
+ rescue Exception => e
66
+ log.error "Removing #{sampled_item} from list"
67
+ log.error e
68
+ log.debug e.backtrace.to_s
69
+ true # remove the sampler
70
+ end
71
+ end
72
+ end
73
+
74
+ def harvest_samplers
75
+ @harvest_samplers ||= []
76
+ end
77
+ def periodic_samplers
78
+ @periodic_samplers ||= []
79
+ end
80
+ end
81
+ end
82
+ end
83
+ 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,330 @@
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 enable
37
+ @disabled = false
38
+ NewRelic::Agent.instance.stats_engine.transaction_sampler = self
39
+ end
40
+
41
+ def disable
42
+ @disabled = true
43
+ NewRelic::Agent.instance.stats_engine.remove_transaction_sampler self
44
+ end
45
+
46
+ def sampling_rate=(val)
47
+ @sampling_rate = val
48
+ @harvest_count = rand(val)
49
+ end
50
+
51
+ def notice_first_scope_push(time)
52
+ start_builder(time.to_f) unless disabled
53
+ end
54
+
55
+ def notice_push_scope(scope, time=Time.now)
56
+
57
+ return unless builder
58
+
59
+ builder.trace_entry(scope, time.to_f)
60
+
61
+ # in developer mode, capture the stack trace with the segment.
62
+ # this is cpu and memory expensive and therefore should not be
63
+ # turned on in production mode
64
+ if NewRelic::Control.instance.developer_mode?
65
+ segment = builder.current_segment
66
+ if segment
67
+ # Strip stack frames off the top that match /new_relic/agent/
68
+ trace = caller
69
+ while trace.first =~/\/lib\/new_relic\/agent\//
70
+ trace.shift
71
+ end
72
+
73
+ trace = trace[0..39] if trace.length > 40
74
+ segment[:backtrace] = trace
75
+ end
76
+ end
77
+ end
78
+
79
+ def scope_depth
80
+ return 0 unless builder
81
+
82
+ builder.scope_depth
83
+ end
84
+
85
+ def notice_pop_scope(scope, time = Time.now)
86
+ return unless builder
87
+ raise "frozen already???" if builder.sample.frozen?
88
+ builder.trace_exit(scope, time.to_f)
89
+ end
90
+
91
+ # This is called when we are done with the transaction. We've
92
+ # unwound the stack to the top level.
93
+ def notice_scope_empty(time=Time.now)
94
+
95
+ last_builder = builder
96
+ return unless last_builder
97
+
98
+ last_builder.finish_trace(time.to_f)
99
+ clear_builder
100
+ return if last_builder.ignored?
101
+
102
+ @samples_lock.synchronize do
103
+ @last_sample = last_builder.sample
104
+
105
+ @random_sample = @last_sample if @random_sampling
106
+
107
+ # ensure we don't collect more than a specified number of samples in memory
108
+ @samples << @last_sample if NewRelic::Control.instance.developer_mode?
109
+ @samples.shift while @samples.length > @max_samples
110
+
111
+ if @slowest_sample.nil? || @slowest_sample.duration < @last_sample.duration
112
+ @slowest_sample = @last_sample
113
+ end
114
+ end
115
+ end
116
+
117
+ def notice_transaction(path, uri=nil, params={})
118
+ builder.set_transaction_info(path, uri, params) if !disabled && builder
119
+ end
120
+
121
+ def ignore_transaction
122
+ builder.ignore_transaction if builder
123
+ end
124
+ def notice_profile(profile)
125
+ builder.set_profile(profile) if builder
126
+ end
127
+
128
+ def notice_transaction_cpu_time(cpu_time)
129
+ builder.set_transaction_cpu_time(cpu_time) if builder
130
+ end
131
+
132
+ MAX_DATA_LENGTH = 16384
133
+ # duration is seconds, float value.
134
+ def notice_extra_data(message, duration, key, config=nil, config_key=nil)
135
+ return unless builder
136
+ segment = builder.current_segment
137
+ if segment
138
+ current_message = segment[key]
139
+ message = current_message + ";\n" + message if current_message
140
+ if message.length > (MAX_DATA_LENGTH - 4)
141
+ message = message[0..MAX_DATA_LENGTH - 4] + '...'
142
+ end
143
+
144
+ segment[key] = message
145
+ segment[config_key] = config if config_key
146
+ segment[:backtrace] = caller.join("\n") if duration >= @stack_trace_threshold
147
+ end
148
+ end
149
+
150
+ private :notice_extra_data
151
+
152
+ # some statements (particularly INSERTS with large BLOBS
153
+ # may be very large; we should trim them to a maximum usable length
154
+ # config is the driver configuration for the connection
155
+ # duration is seconds, float value.
156
+ MAX_SQL_LENGTH = 16384
157
+ def notice_sql(sql, config, duration)
158
+ if Thread::current[:record_sql] != false
159
+ notice_extra_data(sql, duration, :sql, config, :connection_config)
160
+ end
161
+ end
162
+
163
+ # duration is seconds, float value.
164
+ def notice_nosql(key, duration)
165
+ notice_extra_data(key, duration, :key)
166
+ end
167
+
168
+ # get the set of collected samples, merging into previous samples,
169
+ # and clear the collected sample list.
170
+
171
+ def harvest(previous = nil, slow_threshold = 2.0)
172
+ return [] if disabled
173
+ result = []
174
+ previous ||= []
175
+
176
+ previous = [previous] unless previous.is_a?(Array)
177
+
178
+ previous_slowest = previous.inject(nil) {|a,ts| (a) ? ((a.duration > ts.duration) ? a : ts) : ts}
179
+
180
+ @samples_lock.synchronize do
181
+
182
+ if @random_sampling
183
+ @harvest_count += 1
184
+
185
+ if (@harvest_count % @sampling_rate) == 0
186
+ result << @random_sample if @random_sample
187
+ else
188
+ @random_sample = nil # if we don't nil this out, then we won't send the slowest if slowest == @random_sample
189
+ end
190
+ end
191
+
192
+ slowest = @slowest_sample
193
+ @slowest_sample = nil
194
+
195
+ if slowest && slowest != @random_sample && slowest.duration >= slow_threshold
196
+ if previous_slowest.nil? || previous_slowest.duration < slowest.duration
197
+ result << slowest
198
+ else
199
+ result << previous_slowest
200
+ end
201
+ end
202
+
203
+ @random_sample = nil
204
+ @last_sample = nil
205
+ end
206
+ # Truncate the samples at 2100 segments. The UI will clamp them at 2000 segments anyway.
207
+ # This will save us memory and bandwidth.
208
+ result.each { |sample| sample.truncate(@segment_limit) }
209
+ result
210
+ end
211
+
212
+ # reset samples without rebooting the web server
213
+ def reset!
214
+ @samples = []
215
+ @last_sample = nil
216
+ end
217
+
218
+ private
219
+
220
+ def start_builder(time=nil)
221
+ if disabled || Thread::current[:record_tt] == false || !NewRelic::Agent.is_execution_traced?
222
+ clear_builder
223
+ else
224
+ Thread::current[BUILDER_KEY] ||= TransactionSampleBuilder.new(time)
225
+ end
226
+ end
227
+ def builder
228
+ Thread::current[BUILDER_KEY]
229
+ end
230
+ def clear_builder
231
+ Thread::current[BUILDER_KEY] = nil
232
+ end
233
+
234
+ end
235
+
236
+ # a builder is created with every sampled transaction, to dynamically
237
+ # generate the sampled data. It is a thread-local object, and is not
238
+ # accessed by any other thread so no need for synchronization.
239
+ class TransactionSampleBuilder
240
+ attr_reader :current_segment, :sample
241
+
242
+ include NewRelic::CollectionHelper
243
+
244
+ def initialize(time=Time.now)
245
+ @sample = NewRelic::TransactionSample.new(time.to_f)
246
+ @sample_start = time.to_f
247
+ @current_segment = @sample.root_segment
248
+ end
249
+
250
+ def sample_id
251
+ @sample.sample_id
252
+ end
253
+ def ignored?
254
+ @ignore || @sample.params[:path].nil?
255
+ end
256
+ def ignore_transaction
257
+ @ignore = true
258
+ end
259
+ def trace_entry(metric_name, time)
260
+ segment = @sample.create_segment(time.to_f - @sample_start, metric_name)
261
+ @current_segment.add_called_segment(segment)
262
+ @current_segment = segment
263
+ end
264
+
265
+ def trace_exit(metric_name, time)
266
+ if metric_name != @current_segment.metric_name
267
+ fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
268
+ end
269
+ @current_segment.end_trace(time.to_f - @sample_start)
270
+ @current_segment = @current_segment.parent_segment
271
+ end
272
+
273
+ def finish_trace(time)
274
+ # This should never get called twice, but in a rare case that we can't reproduce in house it does.
275
+ # log forensics and return gracefully
276
+ if @sample.frozen?
277
+ log = NewRelic::Control.instance.log
278
+ log.error "Unexpected double-freeze of Transaction Trace Object: \n#{@sample.to_s}"
279
+ return
280
+ end
281
+ @sample.root_segment.end_trace(time.to_f - @sample_start)
282
+ @sample.params[:custom_params] = normalize_params(NewRelic::Agent::Instrumentation::MetricFrame.custom_parameters)
283
+ @sample.freeze
284
+ @current_segment = nil
285
+ end
286
+
287
+ def scope_depth
288
+ depth = -1 # have to account for the root
289
+ current = @current_segment
290
+
291
+ while(current)
292
+ depth += 1
293
+ current = current.parent_segment
294
+ end
295
+
296
+ depth
297
+ end
298
+
299
+ def freeze
300
+ @sample.freeze unless sample.frozen?
301
+ end
302
+
303
+ def set_profile(profile)
304
+ @sample.profile = profile
305
+ end
306
+
307
+ def set_transaction_info(path, uri, params)
308
+ @sample.params[:path] = path
309
+
310
+ if NewRelic::Control.instance.capture_params
311
+ params = normalize_params params
312
+
313
+ @sample.params[:request_params].merge!(params)
314
+ @sample.params[:request_params].delete :controller
315
+ @sample.params[:request_params].delete :action
316
+ end
317
+ @sample.params[:uri] ||= uri || params[:uri]
318
+ end
319
+
320
+ def set_transaction_cpu_time(cpu_time)
321
+ @sample.params[:cpu_time] = cpu_time
322
+ end
323
+
324
+ def sample
325
+ @sample
326
+ end
327
+
328
+ end
329
+ end
330
+ end