newrelic_rpm 2.14.1 → 3.0.0.beta1

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 (123) hide show
  1. data/CHANGELOG +8 -0
  2. data/README.rdoc +2 -0
  3. data/install.rb +2 -2
  4. data/lib/new_relic/agent.rb +34 -1
  5. data/lib/new_relic/agent/agent.rb +34 -25
  6. data/lib/new_relic/agent/browser_monitoring.rb +111 -0
  7. data/lib/new_relic/agent/error_collector.rb +4 -4
  8. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +3 -3
  9. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +5 -7
  10. data/lib/new_relic/agent/instrumentation/data_mapper.rb +8 -8
  11. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +1 -1
  12. data/lib/new_relic/agent/instrumentation/memcache.rb +1 -1
  13. data/lib/new_relic/agent/instrumentation/merb/controller.rb +1 -1
  14. data/lib/new_relic/agent/instrumentation/merb/errors.rb +1 -1
  15. data/lib/new_relic/agent/instrumentation/metric_frame.rb +1 -0
  16. data/lib/new_relic/agent/instrumentation/queue_time.rb +26 -26
  17. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +1 -1
  18. data/lib/new_relic/agent/instrumentation/rails/errors.rb +1 -1
  19. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +2 -2
  20. data/lib/new_relic/agent/instrumentation/sinatra.rb +1 -1
  21. data/lib/new_relic/agent/method_tracer.rb +15 -15
  22. data/lib/new_relic/agent/shim_agent.rb +2 -0
  23. data/lib/new_relic/agent/stats_engine/metric_stats.rb +3 -3
  24. data/lib/new_relic/agent/stats_engine/samplers.rb +2 -2
  25. data/lib/new_relic/agent/stats_engine/transactions.rb +2 -1
  26. data/lib/new_relic/agent/transaction_sample_builder.rb +101 -0
  27. data/lib/new_relic/agent/transaction_sampler.rb +299 -251
  28. data/lib/new_relic/control.rb +2 -2
  29. data/lib/new_relic/control/class_methods.rb +0 -5
  30. data/lib/new_relic/control/configuration.rb +4 -3
  31. data/lib/new_relic/control/frameworks/rails.rb +9 -12
  32. data/lib/new_relic/control/instance_methods.rb +2 -2
  33. data/lib/new_relic/control/instrumentation.rb +1 -1
  34. data/lib/new_relic/control/server_methods.rb +2 -2
  35. data/lib/new_relic/delayed_job_injection.rb +1 -1
  36. data/lib/new_relic/local_environment.rb +7 -7
  37. data/lib/new_relic/rack/browser_monitoring.rb +61 -0
  38. data/lib/new_relic/stats.rb +6 -6
  39. data/lib/new_relic/version.rb +4 -4
  40. data/newrelic.yml +19 -0
  41. data/newrelic_rpm.gemspec +9 -4
  42. data/test/active_record_fixtures.rb +5 -5
  43. data/test/config/test_control.rb +3 -3
  44. data/test/new_relic/agent/agent/connect_test.rb +27 -6
  45. data/test/new_relic/agent/agent/start_test.rb +13 -13
  46. data/test/new_relic/agent/agent/start_worker_thread_test.rb +8 -8
  47. data/test/new_relic/agent/agent_test.rb +85 -0
  48. data/test/new_relic/agent/agent_test_controller.rb +9 -9
  49. data/test/new_relic/agent/agent_test_controller_test.rb +37 -37
  50. data/test/new_relic/agent/browser_monitoring_test.rb +124 -0
  51. data/test/new_relic/agent/busy_calculator_test.rb +7 -7
  52. data/test/new_relic/agent/error_collector/notice_error_test.rb +9 -9
  53. data/test/new_relic/agent/error_collector_test.rb +54 -54
  54. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +69 -69
  55. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +36 -0
  56. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +3 -3
  57. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +11 -11
  58. data/test/new_relic/agent/instrumentation/queue_time_test.rb +38 -35
  59. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +18 -18
  60. data/test/new_relic/agent/memcache_instrumentation_test.rb +12 -12
  61. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +1 -1
  62. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +16 -15
  63. data/test/new_relic/agent/method_tracer_test.rb +60 -60
  64. data/test/new_relic/agent/mock_scope_listener.rb +8 -8
  65. data/test/new_relic/agent/rpm_agent_test.rb +26 -26
  66. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +17 -17
  67. data/test/new_relic/agent/stats_engine/samplers_test.rb +4 -4
  68. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +51 -51
  69. data/test/new_relic/agent/transaction_sample_builder_test.rb +36 -36
  70. data/test/new_relic/agent/transaction_sampler_test.rb +727 -178
  71. data/test/new_relic/agent/worker_loop_test.rb +4 -4
  72. data/test/new_relic/collection_helper_test.rb +15 -15
  73. data/test/new_relic/command/deployments_test.rb +5 -5
  74. data/test/new_relic/control_test.rb +25 -25
  75. data/test/new_relic/local_environment_test.rb +11 -11
  76. data/test/new_relic/metric_spec_test.rb +21 -21
  77. data/test/new_relic/rack/episodes_test.rb +35 -35
  78. data/test/new_relic/stats_test.rb +61 -43
  79. data/test/new_relic/transaction_sample_subtest_test.rb +15 -15
  80. data/test/new_relic/transaction_sample_test.rb +25 -25
  81. data/test/new_relic/version_number_test.rb +11 -11
  82. data/test/test_contexts.rb +7 -7
  83. data/test/test_helper.rb +6 -6
  84. data/ui/helpers/developer_mode_helper.rb +67 -67
  85. data/ui/helpers/google_pie_chart.rb +4 -4
  86. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +4 -4
  87. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +3 -3
  88. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +7 -7
  89. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +1 -1
  90. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +9 -9
  91. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +1 -1
  92. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +8 -8
  93. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +9 -9
  94. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +4 -4
  95. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +2 -2
  96. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +8 -8
  97. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +3 -3
  98. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +6 -6
  99. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +3 -3
  100. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +2 -2
  101. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +6 -6
  102. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +2 -2
  103. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +1 -1
  104. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +4 -4
  105. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +3 -3
  106. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +4 -4
  107. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +1 -1
  108. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +1 -1
  109. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +21 -21
  110. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +2 -2
  111. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +6 -6
  112. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +2 -2
  113. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +1 -1
  114. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +1 -1
  115. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +3 -3
  116. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +1 -1
  117. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +8 -8
  118. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +3 -3
  119. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +3 -3
  120. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +5 -5
  121. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +20 -20
  122. metadata +15 -9
  123. data/lib/new_relic/agent/instrumentation/sequel.rb +0 -109
@@ -7,7 +7,7 @@ module NewRelic
7
7
  @mutex = Mutex.new
8
8
  super
9
9
  end
10
-
10
+
11
11
  def []=(*args)
12
12
  @mutex.synchronize {
13
13
  super
@@ -31,14 +31,14 @@ module NewRelic
31
31
  super
32
32
  }
33
33
  end
34
-
34
+
35
35
  def delete_if(*args)
36
36
  @mutex.synchronize {
37
37
  super
38
38
  }
39
39
  end
40
40
  end
41
-
41
+
42
42
  # The stats hash hashes either a metric name for an unscoped metric,
43
43
  # or a metric_spec for a scoped metric value.
44
44
  def lookup_stat(metric_name)
@@ -33,7 +33,7 @@ module Agent
33
33
  end
34
34
 
35
35
  private
36
-
36
+
37
37
  def add_sampler_to(sampler_array, sampler)
38
38
  raise "Sampler #{sampler.inspect} is already registered. Don't call add_sampler directly anymore." if sampler_array.include?(sampler)
39
39
  sampler_array << sampler
@@ -43,7 +43,7 @@ module Agent
43
43
  def log_added_sampler(type, sampler)
44
44
  log.debug "Adding #{type} sampler: #{sampler.inspect}"
45
45
  end
46
-
46
+
47
47
  public
48
48
 
49
49
  # Add an instance of Sampler to be invoked about every 10 seconds on a background
@@ -104,6 +104,7 @@ module Agent
104
104
  stack = scope_stack
105
105
 
106
106
  if stack && stack.empty?
107
+ Thread.current[:newrelic_most_recent_transaction] = Thread.current[:newrelic_scope_name]
107
108
  Thread::current[:newrelic_scope_stack] = nil
108
109
  Thread::current[:newrelic_scope_name] = nil
109
110
  end
@@ -118,7 +119,7 @@ module Agent
118
119
  if !NewRelic::Control.instance.multi_threaded?
119
120
  @@collecting_gc = true if GC.respond_to?(:time) && GC.respond_to?(:collections) # 1.8.x
120
121
  @@collecting_gc = true if defined?(GC::Profiler) && GC::Profiler.enabled? # 1.9.2
121
- end
122
+ end
122
123
  end
123
124
  @@collecting_gc
124
125
  end
@@ -0,0 +1,101 @@
1
+ require 'new_relic/collection_helper'
2
+ require 'new_relic/transaction_sample'
3
+ require 'new_relic/control'
4
+ require 'new_relic/agent/instrumentation/metric_frame'
5
+ module NewRelic
6
+ module Agent
7
+ # a builder is created with every sampled transaction, to dynamically
8
+ # generate the sampled data. It is a thread-local object, and is not
9
+ # accessed by any other thread so no need for synchronization.
10
+ class TransactionSampleBuilder
11
+ attr_reader :current_segment, :sample
12
+
13
+ include NewRelic::CollectionHelper
14
+
15
+ def initialize(time=Time.now)
16
+ @sample = NewRelic::TransactionSample.new(time.to_f)
17
+ @sample_start = time.to_f
18
+ @current_segment = @sample.root_segment
19
+ end
20
+
21
+ def sample_id
22
+ @sample.sample_id
23
+ end
24
+ def ignored?
25
+ @ignore || @sample.params[:path].nil?
26
+ end
27
+ def ignore_transaction
28
+ @ignore = true
29
+ end
30
+ def trace_entry(metric_name, time)
31
+ segment = @sample.create_segment(time.to_f - @sample_start, metric_name)
32
+ @current_segment.add_called_segment(segment)
33
+ @current_segment = segment
34
+ end
35
+
36
+ def trace_exit(metric_name, time)
37
+ if metric_name != @current_segment.metric_name
38
+ fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
39
+ end
40
+ @current_segment.end_trace(time.to_f - @sample_start)
41
+ @current_segment = @current_segment.parent_segment
42
+ end
43
+
44
+ def finish_trace(time)
45
+ # This should never get called twice, but in a rare case that we can't reproduce in house it does.
46
+ # log forensics and return gracefully
47
+ if @sample.frozen?
48
+ log = NewRelic::Control.instance.log
49
+ log.error "Unexpected double-freeze of Transaction Trace Object: \n#{@sample.to_s}"
50
+ return
51
+ end
52
+ @sample.root_segment.end_trace(time.to_f - @sample_start)
53
+ @sample.params[:custom_params] = normalize_params(NewRelic::Agent::Instrumentation::MetricFrame.custom_parameters)
54
+ @sample.freeze
55
+ @current_segment = nil
56
+ end
57
+
58
+ def scope_depth
59
+ depth = -1 # have to account for the root
60
+ current = @current_segment
61
+
62
+ while(current)
63
+ depth += 1
64
+ current = current.parent_segment
65
+ end
66
+
67
+ depth
68
+ end
69
+
70
+ def freeze
71
+ @sample.freeze unless sample.frozen?
72
+ end
73
+
74
+ def set_profile(profile)
75
+ @sample.profile = profile
76
+ end
77
+
78
+ def set_transaction_info(path, uri, params)
79
+ @sample.params[:path] = path
80
+
81
+ if NewRelic::Control.instance.capture_params
82
+ params = normalize_params params
83
+
84
+ @sample.params[:request_params].merge!(params)
85
+ @sample.params[:request_params].delete :controller
86
+ @sample.params[:request_params].delete :action
87
+ end
88
+ @sample.params[:uri] ||= uri || params[:uri]
89
+ end
90
+
91
+ def set_transaction_cpu_time(cpu_time)
92
+ @sample.params[:cpu_time] = cpu_time
93
+ end
94
+
95
+ def sample
96
+ @sample
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -1,67 +1,105 @@
1
+ require 'new_relic/agent'
2
+ require 'new_relic/control'
3
+ require 'new_relic/agent/transaction_sample_builder'
1
4
  module NewRelic
2
- module Agent
5
+ module Agent
3
6
 
4
- class TransactionSampler
7
+ class TransactionSampler
5
8
 
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
9
+ # Module defining methods stubbed out when the agent is disabled
10
+ module Shim #:nodoc:
11
+ def notice_first_scope_push(*args); end
12
+ def notice_push_scope(*args); end
13
+ def notice_pop_scope(*args); end
14
+ def notice_scope_empty(*args); end
15
+ end
13
16
 
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
17
+ BUILDER_KEY = :transaction_sample_builder
30
18
 
31
- def current_sample_id
32
- b=builder
33
- b and b.sample_id
34
- end
19
+ attr_accessor :stack_trace_threshold, :random_sampling, :sampling_rate
20
+ attr_reader :samples, :last_sample, :disabled
35
21
 
36
- def enable
37
- @disabled = false
38
- NewRelic::Agent.instance.stats_engine.transaction_sampler = self
39
- end
22
+ def initialize
23
+ # @samples is an array of recent samples up to @max_samples in
24
+ # size - it's only used by developer mode
25
+ @samples = []
26
+ @max_samples = 100
40
27
 
41
- def disable
42
- @disabled = true
43
- NewRelic::Agent.instance.stats_engine.remove_transaction_sampler self
44
- end
28
+ # @harvest_count is a count of harvests used for random
29
+ # sampling - we pull 1 @random_sample in every @sampling_rate harvests
30
+ @harvest_count = 0
31
+ @random_sample = nil
32
+ @sampling_rate = 10
33
+
34
+ # @segment_limit and @stack_trace_threshold come from the
35
+ # configuration file, with built-in defaults that should
36
+ # suffice for most customers
37
+ config = NewRelic::Control.instance
38
+ sampler_config = config.fetch('transaction_tracer', {})
39
+ @segment_limit = sampler_config.fetch('limit_segments', 4000)
40
+ @stack_trace_threshold = sampler_config.fetch('stack_trace_threshold', 0.500).to_f
41
+
42
+ # This lock is used to synchronize access to the @last_sample
43
+ # and related variables. It can become necessary on JRuby or
44
+ # any 'honest-to-god'-multithreaded system
45
+ @samples_lock = Mutex.new
46
+ end
45
47
 
46
- def sampling_rate=(val)
47
- @sampling_rate = val
48
- @harvest_count = rand(val)
49
- end
48
+ # Returns the current sample id, delegated from `builder`
49
+ def current_sample_id
50
+ b=builder
51
+ b and b.sample_id
52
+ end
50
53
 
51
- def notice_first_scope_push(time)
52
- start_builder(time.to_f) unless disabled
53
- end
54
+ # Enable the transaction sampler - this also registers it with
55
+ # the statistics engine.
56
+ def enable
57
+ @disabled = false
58
+ NewRelic::Agent.instance.stats_engine.transaction_sampler = self
59
+ end
54
60
 
55
- def notice_push_scope(scope, time=Time.now)
61
+ # Disable the transaction sampler - this also deregisters it
62
+ # with the statistics engine.
63
+ def disable
64
+ @disabled = true
65
+ NewRelic::Agent.instance.stats_engine.remove_transaction_sampler(self)
66
+ end
56
67
 
57
- return unless builder
68
+ # Set with an integer value n, this takes one in every n
69
+ # harvested samples. It also resets the harvest count to a
70
+ # random integer between 0 and (n-1)
71
+ def sampling_rate=(val)
72
+ @sampling_rate = val.to_i
73
+ @harvest_count = rand(val.to_i).to_i
74
+ end
58
75
 
59
- builder.trace_entry(scope, time.to_f)
76
+
77
+ # Creates a new transaction sample builder, unless the
78
+ # transaction sampler is disabled. Takes a time parameter for
79
+ # the start of the transaction sample
80
+ def notice_first_scope_push(time)
81
+ start_builder(time.to_f) unless disabled
82
+ end
83
+
84
+ # This delegates to the builder to create a new open transaction
85
+ # segment for the specified scope, beginning at the optionally
86
+ # specified time.
87
+ #
88
+ # Note that in developer mode, this captures a stacktrace for
89
+ # the beginning of each segment, which can be fairly slow
90
+ def notice_push_scope(scope, time=Time.now)
91
+ return unless builder
92
+
93
+ builder.trace_entry(scope, time.to_f)
94
+
95
+ capture_segment_trace if NewRelic::Control.instance.developer_mode?
96
+ end
60
97
 
61
98
  # in developer mode, capture the stack trace with the segment.
62
99
  # this is cpu and memory expensive and therefore should not be
63
100
  # turned on in production mode
64
- if NewRelic::Control.instance.developer_mode?
101
+ def capture_segment_trace
102
+ return unless NewRelic::Control.instance.developer_mode?
65
103
  segment = builder.current_segment
66
104
  if segment
67
105
  # Strip stack frames off the top that match /new_relic/agent/
@@ -74,257 +112,267 @@ module Agent
74
112
  segment[:backtrace] = trace
75
113
  end
76
114
  end
77
- end
78
-
79
- def scope_depth
80
- return 0 unless builder
81
115
 
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)
116
+ # Defaults to zero, otherwise delegated to the transaction
117
+ # sample builder
118
+ def scope_depth
119
+ return 0 unless builder
94
120
 
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?
121
+ builder.scope_depth
122
+ end
101
123
 
102
- @samples_lock.synchronize do
103
- @last_sample = last_builder.sample
124
+ # Informs the transaction sample builder about the end of a
125
+ # traced scope
126
+ def notice_pop_scope(scope, time = Time.now)
127
+ return unless builder
128
+ raise "frozen already???" if builder.sample.frozen?
129
+ builder.trace_exit(scope, time.to_f)
130
+ end
104
131
 
105
- @random_sample = @last_sample if @random_sampling
132
+ # This is called when we are done with the transaction. We've
133
+ # unwound the stack to the top level. It also clears the
134
+ # transaction sample builder so that it won't continue to have
135
+ # scopes appended to it.
136
+ #
137
+ # It sets various instance variables to the finished sample,
138
+ # depending on which settings are active. See `store_sample`
139
+ def notice_scope_empty(time=Time.now)
140
+
141
+ last_builder = builder
142
+ return unless last_builder
143
+
144
+ last_builder.finish_trace(time.to_f)
145
+ clear_builder
146
+ return if last_builder.ignored?
147
+
148
+ @samples_lock.synchronize do
149
+ # NB this instance variable may be used elsewhere, it's not
150
+ # just a side effect
151
+ @last_sample = last_builder.sample
152
+ store_sample(@last_sample)
153
+ end
154
+ end
106
155
 
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
156
+ # Samples can be stored in three places: the random sample
157
+ # variable, when random sampling is active, the developer mode
158
+ # @samples array, and the @slowest_sample variable if it is
159
+ # slower than the current occupant of that slot
160
+ def store_sample(sample)
161
+ store_random_sample(sample)
162
+ store_sample_for_developer_mode(sample)
163
+ store_slowest_sample(sample)
164
+ end
110
165
 
111
- if @slowest_sample.nil? || @slowest_sample.duration < @last_sample.duration
112
- @slowest_sample = @last_sample
166
+ # Only active when random sampling is true - this is very rarely
167
+ # used. Always store the most recent sample so that random
168
+ # sampling can pick a few of the samples to store, upon harvest
169
+ def store_random_sample(sample)
170
+ if @random_sampling
171
+ @random_sample = sample
113
172
  end
114
173
  end
115
- end
116
174
 
117
- def notice_transaction(path, uri=nil, params={})
118
- builder.set_transaction_info(path, uri, params) if !disabled && builder
119
- end
175
+ # Samples take up a ton of memory, so we only store a lot of
176
+ # them in developer mode - we truncate to @max_samples
177
+ def store_sample_for_developer_mode(sample)
178
+ return unless NewRelic::Control.instance.developer_mode?
179
+ @samples = [] unless @samples
180
+ @samples << sample
181
+ truncate_samples
182
+ end
120
183
 
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
184
+ # Sets @slowest_sample to the passed in sample if it is slower
185
+ # than the current sample in @slowest_sample
186
+ def store_slowest_sample(sample)
187
+ @slowest_sample = sample if slowest_sample?(@slowest_sample, sample)
188
+ end
127
189
 
128
- def notice_transaction_cpu_time(cpu_time)
129
- builder.set_transaction_cpu_time(cpu_time) if builder
130
- end
190
+ # Checks to see if the old sample exists, or if it's duration is
191
+ # less than the new sample
192
+ def slowest_sample?(old_sample, new_sample)
193
+ old_sample.nil? || (new_sample.duration > old_sample.duration)
194
+ end
131
195
 
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] + '...'
196
+ # Smashes the @samples array down to the length of @max_samples
197
+ # by taking the last @max_samples elements of the array
198
+ def truncate_samples
199
+ if @samples.length > @max_samples
200
+ @samples = @samples[-@max_samples..-1]
142
201
  end
202
+ end
143
203
 
144
- segment[key] = message
145
- segment[config_key] = config if config_key
146
- segment[:backtrace] = caller.join("\n") if duration >= @stack_trace_threshold
204
+ # Delegates to the builder to store the path, uri, and
205
+ # parameters if the sampler is active
206
+ def notice_transaction(path, uri=nil, params={})
207
+ builder.set_transaction_info(path, uri, params) if !disabled && builder
147
208
  end
148
- end
149
209
 
150
- private :notice_extra_data
210
+ # Tells the builder to ignore a transaction, if we are currently
211
+ # creating one. Only causes the sample to be ignored upon end of
212
+ # the transaction, and does not change the metrics gathered
213
+ # outside of the sampler
214
+ def ignore_transaction
215
+ builder.ignore_transaction if builder
216
+ end
151
217
 
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)
218
+ # For developer mode profiling support - delegates to the builder
219
+ def notice_profile(profile)
220
+ builder.set_profile(profile) if builder
160
221
  end
161
- end
162
222
 
163
- # duration is seconds, float value.
164
- def notice_nosql(key, duration)
165
- notice_extra_data(key, duration, :key)
166
- end
223
+ # Sets the CPU time used by a transaction, delegates to the builder
224
+ def notice_transaction_cpu_time(cpu_time)
225
+ builder.set_transaction_cpu_time(cpu_time) if builder
226
+ end
227
+
228
+ MAX_DATA_LENGTH = 16384
229
+ # This method is used to record metadata into the currently
230
+ # active segment like a sql query, memcache key, or Net::HTTP uri
231
+ #
232
+ # duration is seconds, float value.
233
+ def notice_extra_data(message, duration, key, config=nil, config_key=nil)
234
+ return unless builder
235
+ segment = builder.current_segment
236
+ if segment
237
+ segment[key] = truncate_message(append_new_message(segment[key], message))
238
+ segment[config_key] = config if config_key
239
+ append_backtrace(segment, duration)
240
+ end
241
+ end
167
242
 
168
- # get the set of collected samples, merging into previous samples,
169
- # and clear the collected sample list.
243
+ private :notice_extra_data
170
244
 
171
- def harvest(previous = nil, slow_threshold = 2.0)
172
- return [] if disabled
173
- result = []
174
- previous ||= []
245
+ # Truncates the message to `MAX_DATA_LENGTH` if needed, and
246
+ # appends an ellipsis because it makes the trucation clearer in
247
+ # the UI
248
+ def truncate_message(message)
249
+ if message.length > (MAX_DATA_LENGTH - 4)
250
+ message[0..MAX_DATA_LENGTH - 4] + '...'
251
+ else
252
+ message
253
+ end
254
+ end
175
255
 
176
- previous = [previous] unless previous.is_a?(Array)
256
+ # Allows the addition of multiple pieces of metadata to one
257
+ # segment - i.e. traced method calls multiple sql queries
258
+ def append_new_message(old_message, message)
259
+ if old_message
260
+ old_message + ";\n" + message
261
+ else
262
+ message
263
+ end
264
+ end
177
265
 
178
- previous_slowest = previous.inject(nil) {|a,ts| (a) ? ((a.duration > ts.duration) ? a : ts) : ts}
266
+ # Appends a backtrace to a segment if that segment took longer
267
+ # than the specified duration
268
+ def append_backtrace(segment, duration)
269
+ segment[:backtrace] = caller.join("\n") if duration >= @stack_trace_threshold
270
+ end
179
271
 
180
- @samples_lock.synchronize do
272
+ # some statements (particularly INSERTS with large BLOBS
273
+ # may be very large; we should trim them to a maximum usable length
274
+ # config is the driver configuration for the connection
275
+ # duration is seconds, float value.
276
+ def notice_sql(sql, config, duration)
277
+ if NewRelic::Agent.is_sql_recorded?
278
+ notice_extra_data(sql, duration, :sql, config, :connection_config)
279
+ end
280
+ end
181
281
 
182
- if @random_sampling
183
- @harvest_count += 1
282
+ # Adds non-sql metadata to a segment - generally the memcached
283
+ # key
284
+ #
285
+ # duration is seconds, float value.
286
+ def notice_nosql(key, duration)
287
+ notice_extra_data(key, duration, :key)
288
+ end
184
289
 
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
290
+ # Every 1/n harvests, adds the most recent sample to the harvest
291
+ # array if it exists. Makes sure that the random sample is not
292
+ # also the slowest sample for this harvest by `uniq!`ing the
293
+ # result array
294
+ #
295
+ # random sampling is very, very seldom used
296
+ def add_random_sample_to(result)
297
+ return unless @random_sampling && @sampling_rate && @sampling_rate.to_i > 0
298
+ @harvest_count += 1
299
+ if (@harvest_count.to_i % @sampling_rate.to_i) == 0
300
+ result << @random_sample if @random_sample
190
301
  end
302
+ result.uniq!
303
+ nil # don't assume this method returns anything
304
+ end
191
305
 
192
- slowest = @slowest_sample
193
- @slowest_sample = nil
306
+ # Returns an array of slow samples, with either one or two
307
+ # elements - one element unless random sampling is enabled. The
308
+ # sample returned will be the slowest sample among those
309
+ # available during this harvest
310
+ def add_samples_to(result, slow_threshold)
311
+ if @slowest_sample && @slowest_sample.duration >= slow_threshold
312
+ result << @slowest_sample
313
+ end
314
+ result.compact!
315
+ result = result.sort_by { |x| x.duration }
316
+ result = result[-1..-1] || []
317
+ add_random_sample_to(result)
318
+ result
319
+ end
194
320
 
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
321
+ # get the set of collected samples, merging into previous samples,
322
+ # and clear the collected sample list. Truncates samples to a
323
+ # specified @segment_limit to save memory and bandwith
324
+ # transmitting samples to the server.
325
+ def harvest(previous = [], slow_threshold = 2.0)
326
+ return [] if disabled
327
+ result = Array(previous)
328
+ @samples_lock.synchronize do
329
+ result = add_samples_to(result, slow_threshold)
330
+ # clear previous transaction samples
331
+ @slowest_sample = nil
332
+ @random_sample = nil
333
+ @last_sample = nil
201
334
  end
335
+ # Truncate the samples at 2100 segments. The UI will clamp them at 2000 segments anyway.
336
+ # This will save us memory and bandwidth.
337
+ result.each { |sample| sample.truncate(@segment_limit) }
338
+ result
339
+ end
202
340
 
203
- @random_sample = nil
341
+ # reset samples without rebooting the web server
342
+ def reset!
343
+ @samples = []
204
344
  @last_sample = nil
345
+ @random_sample = nil
346
+ @slowest_sample = nil
205
347
  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
348
 
218
- private
349
+ private
219
350
 
351
+ # Checks to see if the transaction sampler is disabled, if
352
+ # transaction trace recording is disabled by a thread local, or
353
+ # if execution is untraced - if so it clears the transaction
354
+ # sample builder from the thread local, otherwise it generates a
355
+ # new transaction sample builder with the stated time as a
356
+ # starting point and saves it in the thread local variable
220
357
  def start_builder(time=nil)
221
- if disabled || Thread::current[:record_tt] == false || !NewRelic::Agent.is_execution_traced?
358
+ if disabled || !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced?
222
359
  clear_builder
223
360
  else
224
361
  Thread::current[BUILDER_KEY] ||= TransactionSampleBuilder.new(time)
225
362
  end
226
363
  end
364
+
365
+ # The current thread-local transaction sample builder
227
366
  def builder
228
367
  Thread::current[BUILDER_KEY]
229
368
  end
369
+
370
+ # Sets the thread local variable storing the transaction sample
371
+ # builder to nil to clear it
230
372
  def clear_builder
231
373
  Thread::current[BUILDER_KEY] = nil
232
374
  end
233
375
 
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
376
  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
377
  end
329
378
  end
330
- end