newrelic_rpm 2.9.9 → 2.10.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of newrelic_rpm might be problematic. Click here for more details.

Files changed (120) hide show
  1. data/CHANGELOG +117 -49
  2. data/bin/mongrel_rpm +2 -2
  3. data/install.rb +42 -33
  4. data/lib/new_relic/agent.rb +149 -39
  5. data/lib/new_relic/agent/agent.rb +139 -122
  6. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  7. data/lib/new_relic/agent/collection_helper.rb +11 -2
  8. data/lib/new_relic/agent/error_collector.rb +33 -27
  9. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +30 -26
  10. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  11. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +316 -105
  12. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +22 -0
  13. data/lib/new_relic/agent/instrumentation/memcache.rb +18 -12
  14. data/lib/new_relic/agent/instrumentation/merb/errors.rb +2 -1
  15. data/lib/new_relic/agent/instrumentation/metric_frame.rb +258 -0
  16. data/lib/new_relic/agent/instrumentation/net.rb +7 -11
  17. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +1 -1
  18. data/lib/new_relic/agent/instrumentation/rack.rb +109 -0
  19. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +6 -5
  20. data/lib/new_relic/agent/instrumentation/rails/errors.rb +7 -7
  21. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -0
  22. data/lib/new_relic/agent/method_tracer.rb +305 -150
  23. data/lib/new_relic/agent/sampler.rb +34 -0
  24. data/lib/new_relic/agent/samplers/cpu_sampler.rb +11 -1
  25. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +37 -0
  26. data/lib/new_relic/agent/samplers/memory_sampler.rb +22 -10
  27. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  28. data/lib/new_relic/agent/shim_agent.rb +10 -0
  29. data/lib/new_relic/agent/stats_engine.rb +16 -278
  30. data/lib/new_relic/agent/stats_engine/metric_stats.rb +118 -0
  31. data/lib/new_relic/agent/stats_engine/samplers.rb +81 -0
  32. data/lib/new_relic/agent/stats_engine/transactions.rb +149 -0
  33. data/lib/new_relic/agent/transaction_sampler.rb +73 -67
  34. data/lib/new_relic/agent/worker_loop.rb +69 -68
  35. data/lib/new_relic/commands/deployments.rb +4 -6
  36. data/lib/new_relic/control.rb +121 -60
  37. data/lib/new_relic/control/external.rb +13 -0
  38. data/lib/new_relic/control/merb.rb +2 -0
  39. data/lib/new_relic/control/rails.rb +16 -6
  40. data/lib/new_relic/control/ruby.rb +8 -5
  41. data/lib/new_relic/control/sinatra.rb +18 -0
  42. data/lib/new_relic/delayed_job_injection.rb +25 -0
  43. data/lib/new_relic/histogram.rb +89 -0
  44. data/lib/new_relic/local_environment.rb +64 -30
  45. data/lib/new_relic/metric_data.rb +15 -6
  46. data/lib/new_relic/metric_parser.rb +14 -1
  47. data/lib/new_relic/metric_parser/active_record.rb +14 -0
  48. data/lib/new_relic/metric_parser/controller.rb +5 -2
  49. data/lib/new_relic/metric_parser/external.rb +50 -0
  50. data/lib/new_relic/metric_parser/other_transaction.rb +15 -0
  51. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  52. data/lib/new_relic/metric_spec.rb +39 -20
  53. data/lib/new_relic/metrics.rb +9 -7
  54. data/lib/new_relic/noticed_error.rb +6 -8
  55. data/lib/new_relic/rack/metric_app.rb +5 -4
  56. data/lib/new_relic/rack/{newrelic.ru → mongrel_rpm.ru} +4 -4
  57. data/lib/new_relic/rack/newrelic.yml +1 -0
  58. data/lib/new_relic/{rack.rb → rack_app.rb} +0 -0
  59. data/lib/new_relic/recipes.rb +1 -1
  60. data/lib/new_relic/stats.rb +40 -26
  61. data/lib/new_relic/transaction_analysis.rb +5 -2
  62. data/lib/new_relic/transaction_sample.rb +134 -55
  63. data/lib/new_relic/version.rb +27 -20
  64. data/lib/new_relic_api.rb +67 -47
  65. data/lib/newrelic_rpm.rb +5 -5
  66. data/lib/tasks/tests.rake +2 -0
  67. data/newrelic.yml +69 -29
  68. data/test/active_record_fixtures.rb +2 -2
  69. data/test/config/newrelic.yml +4 -7
  70. data/test/config/test_control.rb +1 -2
  71. data/test/new_relic/agent/active_record_instrumentation_test.rb +115 -31
  72. data/test/new_relic/agent/agent_controller_test.rb +274 -0
  73. data/test/new_relic/agent/agent_test_controller.rb +42 -6
  74. data/test/new_relic/agent/busy_calculator_test.rb +79 -0
  75. data/test/new_relic/agent/collection_helper_test.rb +10 -3
  76. data/test/new_relic/agent/error_collector_test.rb +35 -17
  77. data/test/new_relic/agent/method_tracer_test.rb +60 -20
  78. data/test/new_relic/agent/metric_data_test.rb +2 -2
  79. data/test/new_relic/agent/metric_frame_test.rb +51 -0
  80. data/test/new_relic/agent/net_instrumentation_test.rb +77 -0
  81. data/test/new_relic/agent/{agent_test.rb → rpm_agent_test.rb} +26 -5
  82. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  83. data/test/new_relic/{samplers_test.rb → agent/stats_engine/samplers_test.rb} +23 -22
  84. data/test/new_relic/agent/{stats_engine_test.rb → stats_engine/stats_engine_test.rb} +19 -101
  85. data/test/new_relic/agent/task_instrumentation_test.rb +176 -0
  86. data/test/new_relic/agent/transaction_sample_builder_test.rb +2 -2
  87. data/test/new_relic/agent/transaction_sample_test.rb +53 -38
  88. data/test/new_relic/agent/transaction_sampler_test.rb +101 -33
  89. data/test/new_relic/agent/worker_loop_test.rb +16 -14
  90. data/test/new_relic/control_test.rb +26 -13
  91. data/test/new_relic/metric_parser_test.rb +31 -1
  92. data/test/new_relic/metric_spec_test.rb +2 -2
  93. data/test/new_relic/stats_test.rb +0 -8
  94. data/test/new_relic/version_number_test.rb +31 -1
  95. data/test/test_helper.rb +37 -1
  96. data/ui/controllers/newrelic_controller.rb +19 -11
  97. data/ui/helpers/google_pie_chart.rb +5 -11
  98. data/ui/helpers/newrelic_helper.rb +40 -35
  99. data/ui/views/layouts/newrelic_default.rhtml +7 -7
  100. data/ui/views/newrelic/_sample.rhtml +5 -1
  101. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  102. data/ui/views/newrelic/images/textmate.png +0 -0
  103. data/ui/views/newrelic/index.rhtml +13 -1
  104. data/ui/views/newrelic/show_sample.rhtml +5 -2
  105. data/ui/views/newrelic/stylesheets/style.css +54 -3
  106. metadata +65 -145
  107. data/Manifest +0 -143
  108. data/Rakefile +0 -22
  109. data/init.rb +0 -38
  110. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +0 -127
  111. data/lib/new_relic/agent/instrumentation/error_instrumentation.rb +0 -14
  112. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +0 -13
  113. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +0 -38
  114. data/lib/new_relic/agent/patch_const_missing.rb +0 -125
  115. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +0 -22
  116. data/lib/new_relic/metric_parser/database.rb +0 -23
  117. data/newrelic_rpm.gemspec +0 -35
  118. data/test/new_relic/agent/classloader_patch_test.rb +0 -56
  119. data/test/new_relic/agent/controller_test.rb +0 -107
  120. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +0 -70
@@ -0,0 +1,118 @@
1
+ module NewRelic
2
+ module Agent
3
+ class StatsEngine
4
+ module MetricStats
5
+ # The stats hash hashes either a metric name for an unscoped metric,
6
+ # or a metric_spec for a scoped metric value.
7
+ def lookup_stat(metric_name)
8
+ stats_hash[metric_name]
9
+ end
10
+
11
+ def metrics
12
+ stats_hash.keys.map(&:to_s)
13
+ end
14
+
15
+ def get_stats_no_scope(metric_name)
16
+ stats_hash[metric_name] ||= NewRelic::MethodTraceStats.new
17
+ end
18
+
19
+ # This version allows a caller to pass a stat class to use
20
+ #
21
+ def get_custom_stats(metric_name, stat_class)
22
+ stats_hash[metric_name] ||= stat_class.new
23
+ end
24
+
25
+ # If use_scope is true, two chained metrics are created, one with scope and one without
26
+ # If scoped_metric_only is true, only a scoped metric is created (used by rendering metrics which by definition are per controller only)
27
+ def get_stats(metric_name, use_scope = true, scoped_metric_only = false)
28
+
29
+ if scoped_metric_only
30
+ spec = NewRelic::MetricSpec.new metric_name, scope_name
31
+ stats = stats_hash[spec] ||= NewRelic::MethodTraceStats.new
32
+ else
33
+ stats = stats_hash[metric_name] ||= NewRelic::MethodTraceStats.new
34
+ if use_scope && scope_name && scope_name != metric_name
35
+ spec = NewRelic::MetricSpec.new metric_name, scope_name
36
+ scoped_stats = stats_hash[spec] ||= NewRelic::ScopedMethodTraceStats.new(stats)
37
+ stats = scoped_stats
38
+ end
39
+ end
40
+ stats
41
+ end
42
+
43
+ def lookup_stats(metric_name, scope_name = nil)
44
+ stats_hash[NewRelic::MetricSpec.new(metric_name, scope_name)] ||
45
+ stats_hash[metric_name]
46
+ end
47
+ # Harvest the timeslice data. First recombine current statss
48
+ # with any previously
49
+ # unsent metrics, clear out stats cache, and return the current
50
+ # stats.
51
+ # ---
52
+ # Note: this is not synchronized. There is still some risk in this and
53
+ # we will revisit later to see if we can make this more robust without
54
+ # sacrificing efficiency.
55
+ # +++
56
+ def harvest_timeslice_data(previous_timeslice_data, metric_ids)
57
+ timeslice_data = {}
58
+ poll harvest_samplers
59
+ stats_hash.keys.each do | metric_spec |
60
+
61
+
62
+ # get a copy of the stats collected since the last harvest, and clear
63
+ # the stats inside our hash table for the next time slice.
64
+ stats = stats_hash[metric_spec]
65
+
66
+ # we have an optimization for unscoped metrics
67
+ if !(metric_spec.is_a? NewRelic::MetricSpec)
68
+ metric_spec = NewRelic::MetricSpec.new metric_spec
69
+ end
70
+
71
+ if stats.nil?
72
+ raise "Nil stats for #{metric_spec.name} (#{metric_spec.scope})"
73
+ end
74
+
75
+ stats_copy = stats.clone
76
+ stats.reset
77
+
78
+ # if the previous timeslice data has not been reported (due to an error of some sort)
79
+ # then we need to merge this timeslice with the previously accumulated - but not sent
80
+ # data
81
+ previous_metric_data = previous_timeslice_data[metric_spec]
82
+ stats_copy.merge! previous_metric_data.stats unless previous_metric_data.nil?
83
+ stats_copy.round!
84
+
85
+ # don't bother collecting and reporting stats that have zero-values for this timeslice.
86
+ # significant performance boost and storage savings.
87
+ unless stats_copy.is_reset?
88
+
89
+ id = metric_ids[metric_spec]
90
+ metric_spec_for_transport = id ? nil : metric_spec
91
+
92
+ metric_data = NewRelic::MetricData.new(metric_spec_for_transport, stats_copy, id)
93
+
94
+ timeslice_data[metric_spec] = metric_data
95
+ end
96
+ end
97
+
98
+ timeslice_data
99
+ end
100
+
101
+ # Remove all stats. For test code only.
102
+ def clear_stats
103
+ stats_hash.clear
104
+ NewRelic::Agent::BusyCalculator.reset
105
+ end
106
+
107
+ # Reset each of the stats, such as when a new passenger instance starts up.
108
+ def reset_stats
109
+ stats_hash.values.each { |s| s.reset }
110
+ end
111
+
112
+ def stats_hash
113
+ @stats_hash ||= {}
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,81 @@
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 spawn_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 spawn_sampler_thread
18
+
19
+ return if !@sampler_process.nil? && @sampler_process == $$
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
+ @sampler_process = $$
34
+ end
35
+
36
+ # Add an instance of Sampler to be invoked about every 10 seconds on a background
37
+ # thread.
38
+ def add_sampler sampler
39
+ periodic_samplers.each do |s|
40
+ raise "Sampler #{sampler.id} is already registered. Don't call add_sampler directly anymore." if s.id == sampler.id
41
+ end
42
+ periodic_samplers << sampler
43
+ sampler.stats_engine = self
44
+ log.debug "Adding sampler #{sampler.id.to_s}"
45
+ end
46
+
47
+ # Add a sampler to be invoked just before each harvest.
48
+ def add_harvest_sampler sampler
49
+ harvest_samplers << sampler
50
+ sampler.stats_engine = self
51
+ log.debug "Adding harvest time sampler: #{sampler.id.to_s}"
52
+ end
53
+
54
+ private
55
+
56
+ # Call poll on each of the samplers. Remove
57
+ # the sampler if it raises.
58
+ def poll(samplers)
59
+ samplers.delete_if do |sampled_item|
60
+ begin
61
+ sampled_item.poll
62
+ false # it's okay. don't delete it.
63
+ rescue Exception => e
64
+ log.error "Removing #{sampled_item} from list"
65
+ log.error e
66
+ log.debug e.backtrace.to_s
67
+ true # remove the sampler
68
+ end
69
+ end
70
+ end
71
+
72
+ def harvest_samplers
73
+ @harvest_samplers ||= []
74
+ end
75
+ def periodic_samplers
76
+ @periodic_samplers ||= []
77
+ end
78
+ end
79
+ end
80
+ end
81
+ 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
@@ -1,27 +1,30 @@
1
-
2
- module NewRelic::Agent
1
+ module NewRelic
2
+ module Agent
3
3
 
4
4
  class TransactionSampler
5
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
+
6
14
  BUILDER_KEY = :transaction_sample_builder
7
15
 
8
- attr_accessor :stack_trace_threshold, :random_sampling, :sampling_rate, :last_sample, :samples
16
+ attr_accessor :stack_trace_threshold, :random_sampling, :sampling_rate
17
+ attr_reader :samples, :last_sample, :disabled
9
18
 
10
- def initialize(agent)
19
+ def initialize
11
20
  @samples = []
12
-
13
21
  @harvest_count = 0
14
22
  @max_samples = 100
15
23
  @random_sample = nil
16
24
  config = NewRelic::Control.instance
17
25
  sampler_config = config.fetch('transaction_tracer', {})
26
+ @segment_limit = sampler_config.fetch('limit_segments', 4000)
18
27
  @stack_trace_threshold = sampler_config.fetch('stack_trace_threshold', 0.500).to_f
19
-
20
- agent.stats_engine.add_scope_stack_listener self
21
-
22
- agent.set_sql_obfuscator(:replace) do |sql|
23
- default_sql_obfuscator(sql)
24
- end
25
28
  @samples_lock = Mutex.new
26
29
  end
27
30
 
@@ -31,7 +34,8 @@ module NewRelic::Agent
31
34
  end
32
35
 
33
36
  def disable
34
- NewRelic::Agent.instance.stats_engine.remove_scope_stack_listener self
37
+ @disabled = true
38
+ NewRelic::Agent.instance.stats_engine.remove_transaction_sampler self
35
39
  end
36
40
 
37
41
  def sampling_rate=(val)
@@ -39,25 +43,8 @@ module NewRelic::Agent
39
43
  @harvest_count = rand(val)
40
44
  end
41
45
 
42
- def default_sql_obfuscator(sql)
43
- sql = sql.dup
44
- # This is hardly readable. Use the unit tests.
45
- # remove single quoted strings:
46
- sql.gsub!(/'(.*?[^\\'])??'(?!')/, '?')
47
- # remove double quoted strings:
48
- sql.gsub!(/"(.*?[^\\"])??"(?!")/, '?')
49
- # replace all number literals
50
- sql.gsub!(/\d+/, "?")
51
- sql
52
- end
53
-
54
-
55
46
  def notice_first_scope_push(time)
56
- if Thread::current[:record_tt] == false
57
- Thread::current[BUILDER_KEY] = nil
58
- else
59
- Thread::current[BUILDER_KEY] = TransactionSampleBuilder.new(time)
60
- end
47
+ start_builder(time) unless disabled
61
48
  end
62
49
 
63
50
  def notice_push_scope(scope, time=Time.now.to_f)
@@ -92,6 +79,7 @@ module NewRelic::Agent
92
79
 
93
80
  def notice_pop_scope(scope, time = Time.now.to_f)
94
81
  return unless builder
82
+ raise "frozen already???" if builder.sample.frozen?
95
83
  builder.trace_exit(scope, time)
96
84
  end
97
85
 
@@ -103,36 +91,37 @@ module NewRelic::Agent
103
91
  return unless last_builder
104
92
 
105
93
  last_builder.finish_trace(time)
106
- reset_builder
94
+ clear_builder
95
+ return if last_builder.ignored?
107
96
 
108
97
  @samples_lock.synchronize do
109
98
  @last_sample = last_builder.sample
110
99
 
111
- # We sometimes see "unanchored" transaction traces
112
- if @last_sample.params[:path]
113
- @random_sample = @last_sample if @random_sampling
114
-
115
- # ensure we don't collect more than a specified number of samples in memory
116
- @samples << @last_sample if NewRelic::Control.instance.developer_mode?
117
- @samples.shift while @samples.length > @max_samples
118
-
119
- if @slowest_sample.nil? || @slowest_sample.duration < @last_sample.duration
120
- @slowest_sample = @last_sample
121
- end
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
122
108
  end
123
109
  end
124
110
  end
125
111
 
126
- def notice_transaction(path, request, params)
127
- return unless builder
128
-
129
- builder.set_transaction_info(path, request, params)
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
130
121
  end
131
122
 
132
123
  def notice_transaction_cpu_time(cpu_time)
133
- return unless builder
134
-
135
- builder.set_transaction_cpu_time(cpu_time)
124
+ builder.set_transaction_cpu_time(cpu_time) if builder
136
125
  end
137
126
 
138
127
 
@@ -142,7 +131,7 @@ module NewRelic::Agent
142
131
  MAX_SQL_LENGTH = 16384
143
132
  def notice_sql(sql, config, duration)
144
133
  return unless builder
145
- if Thread::current[:record_sql].nil? || Thread::current[:record_sql]
134
+ if Thread::current[:record_sql] != false
146
135
  segment = builder.current_segment
147
136
  if segment
148
137
  current_sql = segment[:sql]
@@ -164,6 +153,7 @@ module NewRelic::Agent
164
153
  # and clear the collected sample list.
165
154
 
166
155
  def harvest(previous = nil, slow_threshold = 2.0)
156
+ return [] if disabled
167
157
  result = []
168
158
  previous ||= []
169
159
 
@@ -178,6 +168,8 @@ module NewRelic::Agent
178
168
 
179
169
  if (@harvest_count % @sampling_rate) == 0
180
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
181
173
  end
182
174
  end
183
175
 
@@ -193,21 +185,33 @@ module NewRelic::Agent
193
185
  end
194
186
 
195
187
  @random_sample = nil
188
+ @last_sample = nil
196
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) }
197
193
  result
198
194
  end
199
195
 
200
196
  # reset samples without rebooting the web server
201
197
  def reset!
202
198
  @samples = []
199
+ @last_sample = nil
203
200
  end
204
201
 
205
202
  private
206
-
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
207
211
  def builder
208
212
  Thread::current[BUILDER_KEY]
209
213
  end
210
- def reset_builder
214
+ def clear_builder
211
215
  Thread::current[BUILDER_KEY] = nil
212
216
  end
213
217
 
@@ -217,11 +221,12 @@ module NewRelic::Agent
217
221
  # generate the sampled data. It is a thread-local object, and is not
218
222
  # accessed by any other thread so no need for synchronization.
219
223
  class TransactionSampleBuilder
220
- attr_reader :current_segment
224
+ attr_reader :current_segment, :sample
221
225
 
222
226
  include CollectionHelper
223
227
 
224
- def initialize(time=Time.now.to_f)
228
+ def initialize(time=nil)
229
+ time ||= Time.now.to_f
225
230
  @sample = NewRelic::TransactionSample.new(time)
226
231
  @sample_start = time
227
232
  @current_segment = @sample.root_segment
@@ -230,7 +235,12 @@ module NewRelic::Agent
230
235
  def sample_id
231
236
  @sample.sample_id
232
237
  end
233
-
238
+ def ignored?
239
+ @ignore || @sample.params[:path].nil?
240
+ end
241
+ def ignore_transaction
242
+ @ignore = true
243
+ end
234
244
  def trace_entry(metric_name, time)
235
245
  segment = @sample.create_segment(time - @sample_start, metric_name)
236
246
  @current_segment.add_called_segment(segment)
@@ -241,7 +251,6 @@ module NewRelic::Agent
241
251
  if metric_name != @current_segment.metric_name
242
252
  fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
243
253
  end
244
-
245
254
  @current_segment.end_trace(time - @sample_start)
246
255
  @current_segment = @current_segment.parent_segment
247
256
  end
@@ -251,15 +260,11 @@ module NewRelic::Agent
251
260
  # log forensics and return gracefully
252
261
  if @sample.frozen?
253
262
  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
263
+ log.error "Unexpected double-freeze of Transaction Trace Object: \n#{@sample.to_s}"
258
264
  return
259
265
  end
260
-
261
266
  @sample.root_segment.end_trace(time - @sample_start)
262
- @sample.params[:custom_params] = normalize_params(NewRelic::Agent.instance.custom_params)
267
+ @sample.params[:custom_params] = normalize_params(NewRelic::Agent::Instrumentation::MetricFrame.custom_parameters)
263
268
  @sample.freeze
264
269
  @current_segment = nil
265
270
  end
@@ -280,8 +285,11 @@ module NewRelic::Agent
280
285
  @sample.freeze unless sample.frozen?
281
286
  end
282
287
 
288
+ def set_profile(profile)
289
+ @sample.profile = profile
290
+ end
283
291
 
284
- def set_transaction_info(path, request, params)
292
+ def set_transaction_info(path, uri, params)
285
293
  @sample.params[:path] = path
286
294
 
287
295
  if NewRelic::Control.instance.capture_params
@@ -291,19 +299,17 @@ module NewRelic::Agent
291
299
  @sample.params[:request_params].delete :controller
292
300
  @sample.params[:request_params].delete :action
293
301
  end
294
-
295
- @sample.params[:uri] = request.path if request
302
+ @sample.params[:uri] ||= uri || params[:uri]
296
303
  end
297
304
 
298
305
  def set_transaction_cpu_time(cpu_time)
299
306
  @sample.params[:cpu_time] = cpu_time
300
307
  end
301
308
 
302
-
303
309
  def sample
304
- fail "Not finished building" unless @sample.frozen?
305
310
  @sample
306
311
  end
307
312
 
308
313
  end
309
314
  end
315
+ end