ghazel-newrelic_rpm 3.1.0.1 → 3.4.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. data/CHANGELOG +120 -35
  2. data/LICENSE +29 -2
  3. data/README.rdoc +2 -2
  4. data/bin/mongrel_rpm +0 -0
  5. data/bin/newrelic +0 -0
  6. data/bin/newrelic_cmd +0 -0
  7. data/lib/new_relic/agent.rb +50 -38
  8. data/lib/new_relic/agent/agent.rb +459 -337
  9. data/lib/new_relic/agent/beacon_configuration.rb +71 -11
  10. data/lib/new_relic/agent/browser_monitoring.rb +73 -14
  11. data/lib/new_relic/agent/busy_calculator.rb +11 -3
  12. data/lib/new_relic/agent/chained_call.rb +2 -2
  13. data/lib/new_relic/agent/database.rb +223 -0
  14. data/lib/new_relic/agent/error_collector.rb +231 -183
  15. data/lib/new_relic/agent/instrumentation.rb +2 -2
  16. data/lib/new_relic/agent/instrumentation/active_merchant.rb +10 -2
  17. data/lib/new_relic/agent/instrumentation/active_record.rb +138 -0
  18. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +7 -1
  19. data/lib/new_relic/agent/instrumentation/authlogic.rb +6 -0
  20. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +46 -14
  21. data/lib/new_relic/agent/instrumentation/data_mapper.rb +8 -2
  22. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +11 -3
  23. data/lib/new_relic/agent/instrumentation/memcache.rb +49 -25
  24. data/lib/new_relic/agent/instrumentation/merb/controller.rb +7 -2
  25. data/lib/new_relic/agent/instrumentation/merb/errors.rb +7 -1
  26. data/lib/new_relic/agent/instrumentation/metric_frame.rb +31 -4
  27. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +1 -5
  28. data/lib/new_relic/agent/instrumentation/net.rb +8 -2
  29. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +5 -2
  30. data/lib/new_relic/agent/instrumentation/queue_time.rb +1 -1
  31. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +66 -35
  32. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +7 -1
  33. data/lib/new_relic/agent/instrumentation/rails/errors.rb +7 -1
  34. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +121 -1
  35. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +7 -1
  36. data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +21 -0
  37. data/lib/new_relic/agent/instrumentation/resque.rb +80 -0
  38. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -20
  39. data/lib/new_relic/agent/instrumentation/sunspot.rb +6 -0
  40. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +7 -2
  41. data/lib/new_relic/agent/method_tracer.rb +205 -99
  42. data/lib/new_relic/agent/new_relic_service.rb +221 -0
  43. data/lib/new_relic/agent/pipe_channel_manager.rb +161 -0
  44. data/lib/new_relic/agent/pipe_service.rb +54 -0
  45. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +89 -0
  46. data/lib/new_relic/agent/samplers/memory_sampler.rb +6 -7
  47. data/lib/new_relic/agent/shim_agent.rb +5 -5
  48. data/lib/new_relic/agent/sql_sampler.rb +282 -0
  49. data/lib/new_relic/agent/stats_engine.rb +2 -0
  50. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +123 -0
  51. data/lib/new_relic/agent/stats_engine/metric_stats.rb +35 -30
  52. data/lib/new_relic/agent/stats_engine/samplers.rb +10 -4
  53. data/lib/new_relic/agent/stats_engine/transactions.rb +28 -87
  54. data/lib/new_relic/agent/transaction_info.rb +74 -0
  55. data/lib/new_relic/agent/transaction_sample_builder.rb +18 -3
  56. data/lib/new_relic/agent/transaction_sampler.rb +108 -20
  57. data/lib/new_relic/agent/worker_loop.rb +14 -6
  58. data/lib/new_relic/collection_helper.rb +19 -11
  59. data/lib/new_relic/command.rb +1 -1
  60. data/lib/new_relic/commands/deployments.rb +2 -2
  61. data/lib/new_relic/commands/install.rb +2 -13
  62. data/lib/new_relic/control.rb +2 -3
  63. data/lib/new_relic/control/class_methods.rb +12 -6
  64. data/lib/new_relic/control/configuration.rb +57 -8
  65. data/lib/new_relic/control/frameworks.rb +10 -0
  66. data/lib/new_relic/control/frameworks/external.rb +4 -4
  67. data/lib/new_relic/control/frameworks/merb.rb +2 -1
  68. data/lib/new_relic/control/frameworks/rails.rb +35 -22
  69. data/lib/new_relic/control/frameworks/rails3.rb +12 -7
  70. data/lib/new_relic/control/frameworks/ruby.rb +5 -5
  71. data/lib/new_relic/control/frameworks/sinatra.rb +1 -4
  72. data/lib/new_relic/control/instance_methods.rb +38 -12
  73. data/lib/new_relic/control/instrumentation.rb +23 -4
  74. data/lib/new_relic/control/logging_methods.rb +70 -15
  75. data/lib/new_relic/control/server_methods.rb +22 -9
  76. data/lib/new_relic/delayed_job_injection.rb +16 -3
  77. data/lib/new_relic/helper.rb +21 -0
  78. data/lib/new_relic/language_support.rb +95 -0
  79. data/lib/new_relic/local_environment.rb +92 -48
  80. data/lib/new_relic/metric_data.rb +7 -2
  81. data/lib/new_relic/metric_spec.rb +12 -9
  82. data/lib/new_relic/noticed_error.rb +6 -1
  83. data/lib/new_relic/rack/browser_monitoring.rb +18 -19
  84. data/lib/new_relic/rack/developer_mode.rb +3 -2
  85. data/lib/new_relic/recipes.rb +8 -4
  86. data/lib/new_relic/stats.rb +17 -60
  87. data/lib/new_relic/transaction_analysis.rb +2 -1
  88. data/lib/new_relic/transaction_analysis/segment_summary.rb +4 -2
  89. data/lib/new_relic/transaction_sample.rb +60 -75
  90. data/lib/new_relic/transaction_sample/segment.rb +31 -79
  91. data/lib/new_relic/version.rb +2 -2
  92. data/lib/newrelic_rpm.rb +1 -1
  93. data/newrelic.yml +2 -2
  94. data/newrelic_rpm.gemspec +46 -54
  95. data/test/active_record_fixtures.rb +3 -3
  96. data/test/config/newrelic.yml +1 -1
  97. data/test/fixtures/proc_cpuinfo.txt +575 -0
  98. data/test/new_relic/agent/agent/connect_test.rb +128 -25
  99. data/test/new_relic/agent/agent/start_test.rb +9 -94
  100. data/test/new_relic/agent/agent/start_worker_thread_test.rb +2 -4
  101. data/test/new_relic/agent/agent_test.rb +51 -78
  102. data/test/new_relic/agent/agent_test_controller.rb +1 -1
  103. data/test/new_relic/agent/agent_test_controller_test.rb +49 -33
  104. data/test/new_relic/agent/beacon_configuration_test.rb +12 -5
  105. data/test/new_relic/agent/browser_monitoring_test.rb +99 -50
  106. data/test/new_relic/agent/database_test.rb +161 -0
  107. data/test/new_relic/agent/error_collector_test.rb +47 -23
  108. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +96 -42
  109. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +0 -2
  110. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +1 -1
  111. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +3 -11
  112. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +9 -9
  113. data/test/new_relic/agent/instrumentation/queue_time_test.rb +6 -11
  114. data/test/new_relic/agent/memcache_instrumentation_test.rb +54 -18
  115. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +1 -1
  116. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +1 -1
  117. data/test/new_relic/agent/method_tracer_test.rb +3 -2
  118. data/test/new_relic/agent/new_relic_service_test.rb +151 -0
  119. data/test/new_relic/agent/pipe_channel_manager_test.rb +114 -0
  120. data/test/new_relic/agent/pipe_service_test.rb +113 -0
  121. data/test/new_relic/agent/rpm_agent_test.rb +4 -31
  122. data/test/new_relic/agent/sql_sampler_test.rb +192 -0
  123. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +19 -18
  124. data/test/new_relic/agent/stats_engine_test.rb +41 -6
  125. data/test/new_relic/agent/transaction_info_test.rb +13 -0
  126. data/test/new_relic/agent/transaction_sample_builder_test.rb +27 -4
  127. data/test/new_relic/agent/transaction_sampler_test.rb +68 -46
  128. data/test/new_relic/agent/worker_loop_test.rb +3 -3
  129. data/test/new_relic/agent_test.rb +242 -0
  130. data/test/new_relic/collection_helper_test.rb +50 -28
  131. data/test/new_relic/control/configuration_test.rb +77 -0
  132. data/test/new_relic/control/logging_methods_test.rb +49 -21
  133. data/test/new_relic/control_test.rb +115 -54
  134. data/test/new_relic/delayed_job_injection_test.rb +21 -0
  135. data/test/new_relic/fake_collector.rb +210 -0
  136. data/test/new_relic/fake_service.rb +44 -0
  137. data/test/new_relic/local_environment_test.rb +14 -1
  138. data/test/new_relic/metric_parser/metric_parser_test.rb +11 -0
  139. data/test/new_relic/rack/browser_monitoring_test.rb +84 -23
  140. data/test/new_relic/rack/developer_mode_helper_test.rb +141 -0
  141. data/test/new_relic/rack/developer_mode_test.rb +31 -0
  142. data/test/new_relic/stats_test.rb +3 -18
  143. data/test/new_relic/transaction_analysis/segment_summary_test.rb +14 -0
  144. data/test/new_relic/transaction_analysis_test.rb +3 -3
  145. data/test/new_relic/transaction_sample/segment_test.rb +15 -80
  146. data/test/new_relic/transaction_sample_test.rb +25 -18
  147. data/test/script/build_test_gem.sh +51 -0
  148. data/test/script/ci.sh +140 -0
  149. data/test/script/ci_agent-tests_runner.sh +82 -0
  150. data/test/script/ci_bench.sh +52 -0
  151. data/test/script/ci_multiverse_runner.sh +63 -0
  152. data/test/test_contexts.rb +1 -0
  153. data/test/test_helper.rb +18 -5
  154. data/ui/helpers/developer_mode_helper.rb +14 -8
  155. data/ui/helpers/google_pie_chart.rb +0 -1
  156. data/ui/views/newrelic/index.rhtml +2 -2
  157. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +4 -18
  158. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +10 -0
  159. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +11 -11
  160. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +17 -4
  161. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +4 -0
  162. metadata +50 -36
  163. data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +0 -108
  164. data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +0 -112
  165. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +0 -40
  166. data/lib/new_relic/data_serialization.rb +0 -84
  167. data/lib/new_relic/histogram.rb +0 -91
  168. data/lib/new_relic/rack/metric_app.rb +0 -65
  169. data/lib/new_relic/rack/mongrel_rpm.ru +0 -28
  170. data/lib/new_relic/rack/newrelic.yml +0 -27
  171. data/lib/new_relic/rack_app.rb +0 -6
  172. data/test/new_relic/data_serialization_test.rb +0 -70
  173. data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
  174. data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
  175. data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
@@ -1,7 +1,10 @@
1
1
  module NewRelic
2
2
  class MetricData
3
+ # nil, or a NewRelic::MetricSpec object if we have no cached ID
3
4
  attr_accessor :metric_spec
5
+ # nil or a cached integer ID for the metric from the collector.
4
6
  attr_accessor :metric_id
7
+ # the actual statistics object
5
8
  attr_accessor :stats
6
9
 
7
10
  def initialize(metric_spec, stats, metric_id)
@@ -17,7 +20,9 @@ module NewRelic
17
20
  def original_spec
18
21
  @original_spec || @metric_spec
19
22
  end
20
-
23
+
24
+ # assigns a new metric spec, and retains the old metric spec as
25
+ # @original_spec if it exists currently
21
26
  def metric_spec= new_spec
22
27
  @original_spec = @metric_spec if @metric_spec
23
28
  @metric_spec = new_spec
@@ -31,7 +36,7 @@ module NewRelic
31
36
  def to_json(*a)
32
37
  %Q[{"metric_spec":#{metric_id ? 'null' : metric_spec.to_json},"stats":{"total_exclusive_time":#{stats.total_exclusive_time},"min_call_time":#{stats.min_call_time},"call_count":#{stats.call_count},"sum_of_squares":#{stats.sum_of_squares},"total_call_time":#{stats.total_call_time},"max_call_time":#{stats.max_call_time}},"metric_id":#{metric_id ? metric_id : 'null'}}]
33
38
  end
34
-
39
+
35
40
  def to_s
36
41
  if metric_spec
37
42
  "#{metric_spec.name}(#{metric_spec.scope}): #{stats}"
@@ -3,21 +3,24 @@
3
3
  class NewRelic::MetricSpec
4
4
  attr_accessor :name
5
5
  attr_accessor :scope
6
-
6
+
7
+ # the maximum length of a metric name or metric scope
7
8
  MAX_LENGTH = 255
9
+ LENGTH_RANGE = (0...MAX_LENGTH)
8
10
  # Need a "zero-arg" constructor so it can be instantiated from java (using
9
11
  # jruby) for sending responses to ruby agents from the java collector.
10
12
  #
11
13
  def initialize(metric_name = '', metric_scope = '')
12
- self.name = (metric_name || '') && metric_name[0...MAX_LENGTH]
13
- self.scope = metric_scope && metric_scope[0...MAX_LENGTH]
14
+ self.name = (metric_name || '') && metric_name[LENGTH_RANGE]
15
+ self.scope = metric_scope && metric_scope[LENGTH_RANGE]
14
16
  end
15
-
17
+
18
+ # truncates the name and scope to the MAX_LENGTH
16
19
  def truncate!
17
- self.name = name[0...MAX_LENGTH] if name && name.size > MAX_LENGTH
18
- self.scope = scope[0...MAX_LENGTH] if scope && scope.size > MAX_LENGTH
20
+ self.name = name[LENGTH_RANGE] if name && name.size > MAX_LENGTH
21
+ self.scope = scope[LENGTH_RANGE] if scope && scope.size > MAX_LENGTH
19
22
  end
20
-
23
+
21
24
  def ==(o)
22
25
  self.eql?(o)
23
26
  end
@@ -40,10 +43,10 @@ class NewRelic::MetricSpec
40
43
  NewRelic::Control.instance.log.warn("The sub method on metric specs is deprecated") rescue nil
41
44
  return nil if name !~ pattern &&
42
45
  (!apply_to_scope || scope.nil? || scope !~ pattern)
43
- new_name = name.sub(pattern, replacement)[0...MAX_LENGTH]
46
+ new_name = name.sub(pattern, replacement)[LENGTH_RANGE]
44
47
 
45
48
  if apply_to_scope
46
- new_scope = (scope && scope.sub(pattern, replacement)[0...MAX_LENGTH])
49
+ new_scope = (scope && scope.sub(pattern, replacement)[LENGTH_RANGE])
47
50
  else
48
51
  new_scope = scope
49
52
  end
@@ -18,7 +18,12 @@ class NewRelic::NoticedError
18
18
  # clamp long messages to 4k so that we don't send a lot of
19
19
  # overhead across the wire
20
20
  self.message = self.message[0..4095] if self.message.length > 4096
21
-
21
+
22
+ # obfuscate error message if necessary
23
+ if NewRelic::Control.instance.fetch('high_security', false)
24
+ self.message = NewRelic::Agent::Database.obfuscate_sql(self.message)
25
+ end
26
+
22
27
  self.timestamp = timestamp
23
28
  end
24
29
  end
@@ -9,19 +9,14 @@ module NewRelic::Rack
9
9
 
10
10
  # method required by Rack interface
11
11
  def call(env)
12
-
13
- # clear out the thread locals we use in case this is a static request
14
- Thread.current[:newrelic_most_recent_transaction] = nil
15
- Thread.current[:newrelic_start_time] = Time.now
16
- Thread.current[:newrelic_queue_time] = 0
17
-
18
12
  result = @app.call(env) # [status, headers, response]
19
13
 
20
14
  if (NewRelic::Agent.browser_timing_header != "") && should_instrument?(result[0], result[1])
21
15
  response_string = autoinstrument_source(result[2], result[1])
22
16
 
23
- if (response_string)
24
- Rack::Response.new(response_string, result[0], result[1]).finish
17
+ if response_string
18
+ response = Rack::Response.new(response_string, result[0], result[1])
19
+ response.finish
25
20
  else
26
21
  result
27
22
  end
@@ -31,27 +26,30 @@ module NewRelic::Rack
31
26
  end
32
27
 
33
28
  def should_instrument?(status, headers)
34
- status == 200 && headers["Content-Type"] && headers["Content-Type"].include?("text/html")
29
+ status == 200 && headers["Content-Type"] && headers["Content-Type"].include?("text/html") &&
30
+ !headers['Content-Disposition'].to_s.include?('attachment')
35
31
  end
36
32
 
37
33
  def autoinstrument_source(response, headers)
38
34
  source = nil
39
- response.each {|fragment| (source) ? (source << fragment) : (source = fragment)}
35
+ response.each {|fragment| source ? (source << fragment.to_s) : (source = fragment.to_s)}
40
36
  return nil unless source
41
-
42
- body_start = source.index("<body")
43
- body_close = source.rindex("</body>")
44
37
 
45
- if body_start && body_close
38
+
39
+ # Only scan the first 50k (roughly) then give up.
40
+ beginning_of_source = source[0..50_000]
41
+ # Don't scan for body close unless we find body start
42
+ if (body_start = beginning_of_source.index("<body")) && (body_close = source.rindex("</body>"))
43
+
46
44
  footer = NewRelic::Agent.browser_timing_footer
47
45
  header = NewRelic::Agent.browser_timing_header
48
-
49
- if source.include?('X-UA-Compatible')
46
+
47
+ if beginning_of_source.include?('X-UA-Compatible')
50
48
  # put at end of header if UA-Compatible meta tag found
51
- head_pos = source.index("</head>")
52
- elsif head_open = source.index("<head")
49
+ head_pos = beginning_of_source.index("</head>")
50
+ elsif head_open = beginning_of_source.index("<head")
53
51
  # put at the beginning of the header
54
- head_pos = source.index(">", head_open) + 1
52
+ head_pos = beginning_of_source.index(">", head_open) + 1
55
53
  else
56
54
  # put the header right above body start
57
55
  head_pos = body_start
@@ -65,4 +63,5 @@ module NewRelic::Rack
65
63
  source
66
64
  end
67
65
  end
66
+
68
67
  end
@@ -67,6 +67,7 @@ module NewRelic
67
67
 
68
68
  def reset
69
69
  NewRelic::Agent.instance.transaction_sampler.reset!
70
+ NewRelic::Agent.instance.sql_sampler.reset!
70
71
  ::Rack::Response.new{|r| r.redirect('/newrelic/')}.finish
71
72
  end
72
73
 
@@ -82,9 +83,9 @@ module NewRelic
82
83
  @obfuscated_sql = @segment.obfuscated_sql
83
84
  end
84
85
 
85
- explanations = @segment.explain_sql
86
+ headers, explanations = @segment.explain_sql
86
87
  if explanations
87
- @explanation = explanations.first
88
+ @explanation = explanations
88
89
  if !@explanation.blank?
89
90
  first_row = @explanation.first
90
91
  # Show the standard headers if it looks like a mysql explain plan
@@ -33,10 +33,14 @@ make_notify_task = Proc.new do
33
33
  end
34
34
  changelog = `#{log_command}`
35
35
  end
36
- new_revision = rev || source.query_revision(source.head()) do |cmd|
37
- logger.debug "executing locally: '#{cmd}'"
38
- `#{cmd}`
36
+ if rev.nil?
37
+ rev = source.query_revision(source.head()) do |cmd|
38
+ logger.debug "executing locally: '#{cmd}'"
39
+ `#{cmd}`
40
+ end
41
+ rev = rev[0..6] if scm == :git
39
42
  end
43
+ new_revision = rev
40
44
  deploy_options = { :environment => rails_env,
41
45
  :revision => new_revision,
42
46
  :changelog => changelog,
@@ -50,7 +54,7 @@ make_notify_task = Proc.new do
50
54
  logger.info e.message
51
55
  rescue Capistrano::CommandError
52
56
  logger.info "Unable to notify New Relic of the deployment... skipping"
53
- rescue Exception => e
57
+ rescue => e
54
58
  logger.info "Error creating New Relic deployment (#{e})\n#{e.backtrace.join("\n")}"
55
59
  end
56
60
  # WIP: For rollbacks, let's update the deployment we created with an indication of the failure:
@@ -1,12 +1,13 @@
1
1
 
2
2
  module NewRelic
3
3
  module Stats
4
-
4
+
5
+ # a stat is absent if its call count equals zero
5
6
  def absent?
6
- # guess on absent values
7
7
  call_count == 0
8
8
  end
9
-
9
+
10
+ # outputs a useful human-readable time given a value in milliseconds
10
11
  def time_str(value_ms)
11
12
  case
12
13
  when value_ms >= 10000
@@ -26,7 +27,7 @@ module NewRelic
26
27
  numerator.to_f / denominator
27
28
  end
28
29
  end
29
-
30
+
30
31
  def average_call_time
31
32
  checked_calculation(total_call_time, call_count)
32
33
  end
@@ -46,6 +47,7 @@ module NewRelic
46
47
  def sum_attributes(other)
47
48
  update_totals(other)
48
49
  stack_min_max_from(other)
50
+ self.call_count = [self.call_count, other.call_count].max
49
51
  update_boundaries(other)
50
52
  end
51
53
 
@@ -102,43 +104,6 @@ module NewRelic
102
104
  stats.merge!(other_stats)
103
105
  end
104
106
 
105
- # split into an array of timeslices whose
106
- # time boundaries start on (begin_time + (n * duration)) and whose
107
- # end time ends on (begin_time * (n + 1) * duration), except for the
108
- # first and last elements, whose begin time and end time are the begin
109
- # and end times of this stats instance, respectively. Yield to caller
110
- # for the code that creates the actual stats instance
111
- def split(rollup_begin_time, rollup_period)
112
- rollup_begin_time = rollup_begin_time.to_f
113
- rollup_begin_time += ((self.begin_time - rollup_begin_time) / rollup_period).floor * rollup_period
114
-
115
- current_begin_time = self.begin_time
116
- current_end_time = rollup_begin_time + rollup_period
117
-
118
- return [self] if current_end_time >= self.end_time
119
-
120
- timeslices = []
121
- while current_end_time < self.end_time do
122
- ts = yield(current_begin_time, current_end_time)
123
- if ts
124
- ts.fraction_of(self)
125
- timeslices << ts
126
- end
127
- current_begin_time = current_end_time
128
- current_end_time = current_begin_time + rollup_period
129
- end
130
-
131
- if self.end_time > current_begin_time
132
- percentage = rollup_period / self.duration + (self.begin_time - rollup_begin_time) / rollup_period
133
- ts = yield(current_begin_time, self.end_time)
134
- if ts
135
- ts.fraction_of(self)
136
- timeslices << ts
137
- end
138
- end
139
-
140
- timeslices
141
- end
142
107
 
143
108
  def is_reset?
144
109
  call_count == 0 && total_call_time == 0.0 && total_exclusive_time == 0.0
@@ -214,22 +179,6 @@ module NewRelic
214
179
  "[#{Time.at(begin_time.to_f).utc.strftime(format)} UTC, #{'%2.3fs' % duration.to_f}; #{'%2i' % call_count.to_i} calls #{'%4i' % average_call_time.to_f}s]"
215
180
  end
216
181
 
217
- # calculate this set of stats to be a percentage fraction
218
- # of the provided stats, which has an overlapping time window.
219
- # used as a key part of the split algorithm
220
- def fraction_of(s)
221
- min_end = (end_time < s.end_time ? end_time : s.end_time)
222
- max_begin = (begin_time > s.begin_time ? begin_time : s.begin_time)
223
- percentage = (min_end - max_begin) / s.duration
224
-
225
- self.total_exclusive_time = s.total_exclusive_time * percentage
226
- self.total_call_time = s.total_call_time * percentage
227
- self.min_call_time = s.min_call_time
228
- self.max_call_time = s.max_call_time
229
- self.call_count = s.call_count * percentage
230
- self.sum_of_squares = (s.sum_of_squares || 0) * percentage
231
- end
232
-
233
182
  # multiply the total time and rate by the given percentage
234
183
  def multiply_by(percentage)
235
184
  self.total_call_time = total_call_time * percentage
@@ -336,7 +285,10 @@ module NewRelic
336
285
  end
337
286
 
338
287
  alias trace_call record_data_point
339
-
288
+
289
+ # Records multiple data points as one method call - this handles
290
+ # all the aggregation that would be done with multiple
291
+ # record_data_point calls
340
292
  def record_multiple_data_points(total_value, count=1)
341
293
  return record_data_point(total_value) if count == 1
342
294
  @call_count += count
@@ -348,11 +300,13 @@ module NewRelic
348
300
  @sum_of_squares += (avg_val * avg_val) * count
349
301
  self
350
302
  end
351
-
303
+
304
+ # increments the call_count by one
352
305
  def increment_count(value = 1)
353
306
  @call_count += value
354
307
  end
355
-
308
+
309
+ # outputs a human-readable version of the MethodTraceStats object
356
310
  def inspect
357
311
  "#<NewRelic::MethodTraceStats #{summary} >"
358
312
  end
@@ -369,6 +323,9 @@ module NewRelic
369
323
  unscoped_stats.trace_call call_time, exclusive_time
370
324
  super call_time, exclusive_time
371
325
  end
326
+ # Records multiple data points as one method call - this handles
327
+ # all the aggregation that would be done with multiple
328
+ # trace_call calls
372
329
  def record_multiple_data_points(total_value, count=1)
373
330
  unscoped_stats.record_multiple_data_points(total_value, count)
374
331
  super total_value, count
@@ -15,11 +15,12 @@ module NewRelic
15
15
  # limit the data set to the top n
16
16
  def breakdown_data(limit = nil)
17
17
  metric_hash = {}
18
- each_segment do |segment|
18
+ each_segment_with_nest_tracking do |segment|
19
19
  unless segment == root_segment
20
20
  metric_name = segment.metric_name
21
21
  metric_hash[metric_name] ||= SegmentSummary.new(metric_name, self)
22
22
  metric_hash[metric_name] << segment
23
+ metric_hash[metric_name]
23
24
  end
24
25
  end
25
26
 
@@ -3,11 +3,12 @@ module NewRelic
3
3
  # summarizes performance data for all calls to segments
4
4
  # with the same metric_name
5
5
  class SegmentSummary
6
- attr_accessor :metric_name, :total_time, :exclusive_time, :call_count
6
+ attr_accessor :metric_name, :total_time, :exclusive_time, :call_count, :current_nest_count
7
7
  def initialize(metric_name, sample)
8
8
  @metric_name = metric_name
9
9
  @total_time, @exclusive_time, @call_count = 0,0,0
10
10
  @sample = sample
11
+ @current_nest_count = 0
11
12
  end
12
13
 
13
14
  def <<(segment)
@@ -15,7 +16,8 @@ module NewRelic
15
16
  raise ArgumentError, "Metric Name Mismatch: #{segment.metric_name} != #{metric_name}"
16
17
  end
17
18
 
18
- @total_time += segment.duration
19
+ # a nested segment should use the sum of the top level totals
20
+ @total_time += segment.duration if current_nest_count == 0
19
21
  @exclusive_time += segment.exclusive_duration
20
22
  @call_count += 1
21
23
  end
@@ -3,89 +3,45 @@ require 'new_relic/transaction_sample/summary_segment'
3
3
  require 'new_relic/transaction_sample/fake_segment'
4
4
  require 'new_relic/transaction_sample/composite_segment'
5
5
  module NewRelic
6
+ # the number of segments that need to exist before we roll them up
7
+ # into one segment with multiple executions
6
8
  COLLAPSE_SEGMENTS_THRESHOLD = 2
7
9
 
8
- MYSQL_EXPLAIN_COLUMNS = [
9
- "Id",
10
- "Select Type",
11
- "Table",
12
- "Type",
13
- "Possible Keys",
14
- "Key",
15
- "Key Length",
16
- "Ref",
17
- "Rows",
18
- "Extra"
19
- ].freeze
20
-
21
10
  class TransactionSample
22
11
 
23
- attr_accessor :params, :root_segment
24
- attr_accessor :profile
25
- attr_reader :root_segment
26
- attr_reader :params
27
- attr_reader :sample_id
12
+ attr_accessor :params, :root_segment, :profile, :force_persist, :guid
13
+ attr_reader :root_segment, :params, :sample_id
28
14
 
29
15
  @@start_time = Time.now
30
16
 
31
17
  include TransactionAnalysis
32
18
 
33
- class << self
34
- def obfuscate_sql(sql)
35
- NewRelic::Agent.instance.obfuscator.call(sql)
36
- end
37
-
38
- def get_connection(config)
39
- @@connections ||= {}
40
-
41
- connection = @@connections[config]
42
-
43
- return connection if connection
44
-
45
- begin
46
- connection = ActiveRecord::Base.send("#{config[:adapter]}_connection", config)
47
- @@connections[config] = connection
48
- rescue => e
49
- NewRelic::Agent.agent.log.error("Caught exception #{e} trying to get connection to DB for explain. Control: #{config}")
50
- NewRelic::Agent.agent.log.error(e.backtrace.join("\n"))
51
- nil
52
- end
53
- end
54
-
55
- def close_connections
56
- @@connections ||= {}
57
- @@connections.values.each do |connection|
58
- begin
59
- connection.disconnect!
60
- rescue
61
- end
62
- end
63
-
64
- @@connections = {}
65
- end
66
-
67
- end
68
-
69
19
  def initialize(time = Time.now.to_f, sample_id = nil)
70
20
  @sample_id = sample_id || object_id
71
21
  @start_time = time
22
+ @params = { :segment_count => -1, :request_params => {} }
23
+ @segment_count = -1
72
24
  @root_segment = create_segment 0.0, "ROOT"
73
- @params = {}
74
- @params[:request_params] = {}
25
+
26
+ @guid = generate_guid
27
+ NewRelic::Agent::TransactionInfo.get.guid = @guid
75
28
  end
76
29
 
77
30
  def count_segments
78
- @root_segment.count_segments - 1 # don't count the root segment
31
+ @segment_count
79
32
  end
80
33
 
34
+ # Truncates the transaction sample to a maximum length determined
35
+ # by the passed-in parameter. Operates recursively on the entire
36
+ # tree of transaction segments in a depth-first manner
81
37
  def truncate(max)
82
- count = count_segments
83
- return if count < max
38
+ return if @segment_count < max
84
39
  @root_segment.truncate(max + 1)
85
-
86
- ensure_segment_count_set(count)
40
+ @segment_count = max
87
41
  end
88
42
 
43
+ # makes sure that the parameter cache for segment count is set to
44
+ # the correct value
89
45
  def ensure_segment_count_set(count)
90
46
  params[:segment_count] ||= count
91
47
  end
@@ -116,6 +72,8 @@ module NewRelic
116
72
 
117
73
  def create_segment(relative_timestamp, metric_name, segment_id = nil)
118
74
  raise TypeError.new("Frozen Transaction Sample") if frozen?
75
+ @params[:segment_count] += 1
76
+ @segment_count += 1
119
77
  NewRelic::TransactionSample::Segment.new(relative_timestamp, metric_name, segment_id)
120
78
  end
121
79
 
@@ -123,14 +81,24 @@ module NewRelic
123
81
  root_segment.duration
124
82
  end
125
83
 
84
+ # Iterates recursively over each segment in the entire transaction
85
+ # sample tree
126
86
  def each_segment(&block)
127
87
  @root_segment.each_segment(&block)
128
88
  end
129
89
 
90
+ # Iterates recursively over each segment in the entire transaction
91
+ # sample tree while keeping track of nested segments
92
+ def each_segment_with_nest_tracking(&block)
93
+ @root_segment.each_segment_with_nest_tracking(&block)
94
+ end
95
+
130
96
  def to_s_compact
131
97
  @root_segment.to_s_compact
132
98
  end
133
99
 
100
+ # Searches the tree recursively for the segment with the given
101
+ # id. note that this is an internal id, not an ActiveRecord id
134
102
  def find_segment(id)
135
103
  @root_segment.find_segment(id)
136
104
  end
@@ -147,6 +115,7 @@ module NewRelic
147
115
  when Enumerable then v.map(&:to_s).sort.join("; ")
148
116
  when String then v
149
117
  when Float then '%6.3s' % v
118
+ when Fixnum then v.to_s
150
119
  when nil then ''
151
120
  else
152
121
  raise "unexpected value type for #{k}: '#{v}' (#{v.class})"
@@ -165,9 +134,10 @@ module NewRelic
165
134
  regex = Regexp.new(regex)
166
135
 
167
136
  sample = TransactionSample.new(@start_time, sample_id)
168
-
169
- params.each {|k,v| sample.params[k] = v}
170
-
137
+
138
+ sample.params = params.dup
139
+ sample.params[:segment_count] = 0
140
+
171
141
  delta = build_segment_with_omissions(sample, 0.0, @root_segment, sample.root_segment, regex)
172
142
  sample.root_segment.end_trace(@root_segment.exit_timestamp - delta)
173
143
  sample.profile = self.profile
@@ -186,13 +156,11 @@ module NewRelic
186
156
  sample = TransactionSample.new(@start_time, sample_id)
187
157
 
188
158
  sample.params.merge! self.params
159
+ sample.guid = self.guid
160
+ sample.force_persist = self.force_persist if self.force_persist
189
161
 
190
- begin
191
- build_segment_for_transfer(sample, @root_segment, sample.root_segment, options)
192
- ensure
193
- self.class.close_connections
194
- end
195
-
162
+ build_segment_for_transfer(sample, @root_segment, sample.root_segment, options)
163
+
196
164
  sample.root_segment.end_trace(@root_segment.exit_timestamp)
197
165
  sample
198
166
  end
@@ -203,6 +171,19 @@ module NewRelic
203
171
 
204
172
  private
205
173
 
174
+
175
+
176
+ HEX_DIGITS = (0..15).map{|i| i.to_s(16)}
177
+ # generate a random 64 bit uuid
178
+ def generate_guid
179
+ guid = ''
180
+ HEX_DIGITS.each do |a|
181
+ guid << HEX_DIGITS[rand(16)]
182
+ end
183
+ guid
184
+ end
185
+
186
+ # This is badly in need of refactoring
206
187
  def build_segment_with_omissions(new_sample, time_delta, source_segment, target_segment, regex)
207
188
  source_segment.called_segments.each do |source_called_segment|
208
189
  # if this segment's metric name matches the given regular expression, bail
@@ -233,6 +214,8 @@ module NewRelic
233
214
  end
234
215
 
235
216
  # see prepare_to_send for what we do with options
217
+ #
218
+ # This is badly in need of refactoring
236
219
  def build_segment_for_transfer(new_sample, source_segment, target_segment, options)
237
220
  source_segment.called_segments.each do |source_called_segment|
238
221
  target_called_segment = new_sample.create_segment(
@@ -242,20 +225,22 @@ module NewRelic
242
225
 
243
226
  target_segment.add_called_segment target_called_segment
244
227
  source_called_segment.params.each do |k,v|
245
- case k
228
+ case k
246
229
  when :backtrace
247
230
  target_called_segment[k]=v if options[:keep_backtraces]
248
231
  when :sql
249
232
  # run an EXPLAIN on this sql if specified.
250
- if options[:record_sql] && options[:record_sql] && options[:explain_sql] && source_called_segment.duration > options[:explain_sql].to_f
251
- target_called_segment[:explanation] = source_called_segment.explain_sql
233
+ if options[:record_sql] && options[:record_sql] &&
234
+ options[:explain_sql] &&
235
+ source_called_segment.duration > options[:explain_sql].to_f
236
+ target_called_segment[:explain_plan] = source_called_segment.explain_sql
252
237
  end
253
238
 
254
239
  target_called_segment[:sql] = case options[:record_sql]
255
240
  when :raw then v
256
- when :obfuscated then TransactionSample.obfuscate_sql(v)
241
+ when :obfuscated then NewRelic::Agent::Database.obfuscate_sql(v)
257
242
  else raise "Invalid value for record_sql: #{options[:record_sql]}"
258
- end if options[:record_sql]
243
+ end.to_s if options[:record_sql]
259
244
  when :connection_config
260
245
  # don't copy it
261
246
  else