techarch-newrelic_rpm 2.10.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/CHANGELOG +335 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +159 -0
  4. data/README.md +138 -0
  5. data/Rakefile +22 -0
  6. data/bin/mongrel_rpm +33 -0
  7. data/bin/newrelic_cmd +4 -0
  8. data/cert/cacert.pem +34 -0
  9. data/init.rb +38 -0
  10. data/install.rb +45 -0
  11. data/lib/new_relic/agent.rb +281 -0
  12. data/lib/new_relic/agent/agent.rb +636 -0
  13. data/lib/new_relic/agent/chained_call.rb +13 -0
  14. data/lib/new_relic/agent/collection_helper.rb +66 -0
  15. data/lib/new_relic/agent/error_collector.rb +121 -0
  16. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  17. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +88 -0
  18. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  19. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +437 -0
  20. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  21. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +167 -0
  22. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  23. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  24. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  25. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  26. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  27. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  28. data/lib/new_relic/agent/instrumentation/rack.rb +108 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  31. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +41 -0
  32. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  34. data/lib/new_relic/agent/method_tracer.rb +349 -0
  35. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  36. data/lib/new_relic/agent/sampler.rb +12 -0
  37. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  38. data/lib/new_relic/agent/samplers/memory_sampler.rb +138 -0
  39. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  40. data/lib/new_relic/agent/shim_agent.rb +21 -0
  41. data/lib/new_relic/agent/stats_engine.rb +22 -0
  42. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  43. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  44. data/lib/new_relic/agent/stats_engine/transactions.rb +152 -0
  45. data/lib/new_relic/agent/transaction_sampler.rb +310 -0
  46. data/lib/new_relic/agent/worker_loop.rb +118 -0
  47. data/lib/new_relic/commands/deployments.rb +145 -0
  48. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  49. data/lib/new_relic/control.rb +473 -0
  50. data/lib/new_relic/control/external.rb +13 -0
  51. data/lib/new_relic/control/merb.rb +22 -0
  52. data/lib/new_relic/control/rails.rb +145 -0
  53. data/lib/new_relic/control/ruby.rb +36 -0
  54. data/lib/new_relic/control/sinatra.rb +14 -0
  55. data/lib/new_relic/histogram.rb +89 -0
  56. data/lib/new_relic/local_environment.rb +328 -0
  57. data/lib/new_relic/merbtasks.rb +6 -0
  58. data/lib/new_relic/metric_data.rb +42 -0
  59. data/lib/new_relic/metric_parser.rb +124 -0
  60. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  61. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  62. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  63. data/lib/new_relic/metric_parser/controller.rb +54 -0
  64. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  65. data/lib/new_relic/metric_parser/errors.rb +6 -0
  66. data/lib/new_relic/metric_parser/external.rb +50 -0
  67. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  68. data/lib/new_relic/metric_parser/view.rb +61 -0
  69. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  70. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  71. data/lib/new_relic/metric_spec.rb +52 -0
  72. data/lib/new_relic/metrics.rb +7 -0
  73. data/lib/new_relic/noticed_error.rb +23 -0
  74. data/lib/new_relic/rack/metric_app.rb +56 -0
  75. data/lib/new_relic/rack/newrelic.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +25 -0
  77. data/lib/new_relic/rack_app.rb +5 -0
  78. data/lib/new_relic/recipes.rb +82 -0
  79. data/lib/new_relic/stats.rb +361 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +666 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +313 -0
  84. data/lib/newrelic_rpm.rb +40 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +13 -0
  88. data/newrelic.yml +227 -0
  89. data/recipes/newrelic.rb +6 -0
  90. data/techarch-newrelic_rpm.gemspec +32 -0
  91. data/test/active_record_fixtures.rb +55 -0
  92. data/test/config/newrelic.yml +46 -0
  93. data/test/config/test_control.rb +39 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +264 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +119 -0
  97. data/test/new_relic/agent/agent_test_controller.rb +44 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +172 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  103. data/test/new_relic/agent/metric_data_test.rb +56 -0
  104. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  105. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  106. data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
  107. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  108. data/test/new_relic/agent/stats_engine/samplers_test.rb +81 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +126 -0
  111. data/test/new_relic/agent/testable_agent.rb +13 -0
  112. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  113. data/test/new_relic/agent/transaction_sample_test.rb +186 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +113 -0
  117. data/test/new_relic/deployments_api_test.rb +68 -0
  118. data/test/new_relic/environment_test.rb +75 -0
  119. data/test/new_relic/metric_parser_test.rb +172 -0
  120. data/test/new_relic/metric_spec_test.rb +177 -0
  121. data/test/new_relic/shim_agent_test.rb +9 -0
  122. data/test/new_relic/stats_test.rb +291 -0
  123. data/test/new_relic/version_number_test.rb +74 -0
  124. data/test/test_helper.rb +38 -0
  125. data/test/ui/newrelic_controller_test.rb +14 -0
  126. data/test/ui/newrelic_helper_test.rb +53 -0
  127. data/ui/controllers/newrelic_controller.rb +220 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +317 -0
  130. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  131. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  132. data/ui/views/newrelic/_sample.rhtml +19 -0
  133. data/ui/views/newrelic/_segment.rhtml +28 -0
  134. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  135. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  136. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  137. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  138. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  139. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  140. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  141. data/ui/views/newrelic/_table.rhtml +12 -0
  142. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  143. data/ui/views/newrelic/images/arrow-close.png +0 -0
  144. data/ui/views/newrelic/images/arrow-open.png +0 -0
  145. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  146. data/ui/views/newrelic/images/file_icon.png +0 -0
  147. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  148. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  149. data/ui/views/newrelic/images/textmate.png +0 -0
  150. data/ui/views/newrelic/index.rhtml +57 -0
  151. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  152. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  153. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  154. data/ui/views/newrelic/show_sample.rhtml +80 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +484 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +330 -0
@@ -0,0 +1,125 @@
1
+ # This class is for debugging purposes only.
2
+ # It inserts instrumentation into class loading to verify
3
+ # that no classes are being loaded on the new relic thread,
4
+ # which can cause problems in the class loader code.
5
+ # It is only loaded by agent.rb when a particular newrelic.yml
6
+ # option is set.
7
+
8
+ module ClassLoadingWatcher # :nodoc: all
9
+
10
+ extend self
11
+ @@background_thread = nil
12
+ @@flag_const_missing = nil
13
+
14
+ def background_thread
15
+ @@background_thread
16
+ end
17
+ def flag_const_missing
18
+ @@flag_const_missing
19
+ end
20
+ def flag_const_missing=(val)
21
+ @@flag_const_missing = val
22
+ end
23
+
24
+ def background_thread=(thread)
25
+ @@background_thread = thread
26
+
27
+ # these tests verify that check is working
28
+ =begin
29
+ @@background_thread = nil
30
+ bad = ConstMissingInForegroundThread rescue nil
31
+ @@background_thread = thread
32
+ bad = ConstMissingInBackgroundThread rescue nil
33
+ require 'new_relic/agent/patch_const_missing'
34
+ load 'new_relic/agent/patch_const_missing.rb'
35
+ =end
36
+ end
37
+ module SanityCheck
38
+ def nr_check_for_classloading(*args)
39
+
40
+ if Thread.current == ClassLoadingWatcher.background_thread
41
+ nr_error "Agent background thread shouldn't be loading classes (#{args.inspect})"
42
+ end
43
+ end
44
+ #
45
+ def nr_check_for_constmissing(*args)
46
+ if ClassLoadingWatcher.flag_const_missing
47
+ nr_error "Classes in Agent should not be loaded via const_missing (#{args.inspect})"
48
+ end
49
+ end
50
+ private
51
+ def nr_error(msg)
52
+ exception = NewRelic::Agent::BackgroundLoadingError.new(msg)
53
+ backtrace = caller
54
+ backtrace.shift
55
+ exception.set_backtrace(backtrace)
56
+ NewRelic::Agent.instance.error_collector.notice_error(exception, nil)
57
+ msg << "\n" << backtrace.join("\n")
58
+ NewRelic::Control.instance.log.error msg
59
+ end
60
+ end
61
+ def enable_warning
62
+ Object.class_eval do
63
+ if !defined?(non_new_relic_require)
64
+ alias_method :non_new_relic_require, :require
65
+ alias_method :require, :new_relic_require
66
+ end
67
+
68
+ if !defined?(non_new_relic_load)
69
+ alias_method :non_new_relic_load, :load
70
+ alias_method :load, :new_relic_load
71
+ end
72
+ end
73
+ Module.class_eval do
74
+ if !defined?(non_new_relic_const_missing)
75
+ alias_method :non_new_relic_const_missing, :const_missing
76
+ alias_method :const_missing, :new_relic_const_missing
77
+ end
78
+ end
79
+ end
80
+
81
+ def disable_warning
82
+ Object.class_eval do
83
+ if defined?(non_new_relic_require)
84
+ alias_method :require, :non_new_relic_require
85
+ undef non_new_relic_require
86
+ end
87
+
88
+ if defined?(non_new_relic_load)
89
+ alias_method :load, :non_new_relic_load
90
+ undef non_new_relic_load
91
+ end
92
+ end
93
+ Module.class_eval do
94
+ if defined?(non_new_relic_const_missing)
95
+ alias_method :const_missing, :non_new_relic_const_missing
96
+ undef non_new_relic_const_missing
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ class Object # :nodoc:
103
+ include ClassLoadingWatcher::SanityCheck
104
+
105
+ def new_relic_require(*args)
106
+ nr_check_for_classloading("Object require", *args)
107
+ non_new_relic_require(*args)
108
+ end
109
+
110
+ def new_relic_load(*args)
111
+ nr_check_for_classloading("Object load", *args)
112
+ non_new_relic_load(*args)
113
+ end
114
+ end
115
+
116
+
117
+ class Module # :nodoc:
118
+ include ClassLoadingWatcher::SanityCheck
119
+
120
+ def new_relic_const_missing(*args)
121
+ nr_check_for_constmissing("Module #{self.name} const_missing", *args)
122
+ nr_check_for_classloading("Module #{self.name} const_missing", *args)
123
+ non_new_relic_const_missing(*args)
124
+ end
125
+ end
@@ -0,0 +1,12 @@
1
+ class NewRelic::Agent::Sampler
2
+ attr_accessor :stats_engine
3
+ attr_reader :id
4
+ def initialize(id)
5
+ @id = id
6
+ end
7
+
8
+ def poll
9
+ raise "Implement in the subclass"
10
+ end
11
+
12
+ end
@@ -0,0 +1,49 @@
1
+ module NewRelic::Agent::Samplers
2
+ class CpuSampler < NewRelic::Agent::Sampler
3
+ attr_reader :last_time
4
+ def initialize
5
+ super :cpu
6
+ poll
7
+ end
8
+ def user_util_stats
9
+ stats_engine.get_stats_no_scope("CPU/User/Utilization")
10
+ end
11
+ def system_util_stats
12
+ stats_engine.get_stats_no_scope("CPU/System/Utilization")
13
+ end
14
+ def usertime_stats
15
+ stats_engine.get_stats_no_scope("CPU/User Time")
16
+ end
17
+ def systemtime_stats
18
+ stats_engine.get_stats_no_scope("CPU/System Time")
19
+ end
20
+
21
+ def self.supported_on_this_platform?
22
+ not defined?(Java)
23
+ end
24
+
25
+ def poll
26
+ now = Time.now
27
+ t = Process.times
28
+ if @last_time
29
+ elapsed = now - @last_time
30
+ return if elapsed < 1 # Causing some kind of math underflow
31
+ num_processors = NewRelic::Control.instance.local_env.processors || 1
32
+ usertime = t.utime - @last_utime
33
+ systemtime = t.stime - @last_stime
34
+
35
+ systemtime_stats.record_data_point(systemtime) if systemtime >= 0
36
+ usertime_stats.record_data_point(usertime) if usertime >= 0
37
+
38
+ # Calculate the true utilization by taking cpu times and dividing by
39
+ # elapsed time X num_processors.
40
+ user_util_stats.record_data_point usertime / (elapsed * num_processors)
41
+ system_util_stats.record_data_point systemtime / (elapsed * num_processors)
42
+ end
43
+ @last_utime = t.utime
44
+ @last_stime = t.stime
45
+ @last_time = now
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,138 @@
1
+ module NewRelic::Agent::Samplers
2
+
3
+ class MemorySampler < NewRelic::Agent::Sampler
4
+ attr_accessor :sampler
5
+
6
+ def initialize
7
+ super :memory
8
+
9
+ # macos, linux, solaris
10
+ if defined? JRuby
11
+ @sampler = JavaHeapSampler.new
12
+ elsif platform =~ /linux/
13
+ @sampler = ProcStatus.new
14
+ if !@sampler.can_run?
15
+ NewRelic::Agent.instance.log.warn "Error attempting to use /proc/$$/status file for reading memory. Using ps command instead."
16
+ @sampler = ShellPS.new("ps -o rsz")
17
+ else
18
+ NewRelic::Agent.instance.log.info "Using /proc/$$/status for reading process memory."
19
+ end
20
+ elsif platform =~ /darwin9/ # 10.5
21
+ @sampler = ShellPS.new("ps -o rsz")
22
+ elsif platform =~ /darwin10/ # 10.6
23
+ @sampler = ShellPS.new("ps -o rss")
24
+ elsif platform =~ /freebsd/
25
+ @sampler = ShellPS.new("ps -o rss")
26
+ elsif platform =~ /solaris/
27
+ @sampler = ShellPS.new("/usr/bin/ps -o rss -p")
28
+ end
29
+
30
+ raise "Unsupported platform for getting memory: #{platform}" if @sampler.nil?
31
+ raise "Unable to run #{@sampler}" unless @sampler.can_run?
32
+ end
33
+ def self.supported_on_this_platform?
34
+ defined?(JRuby) or platform =~ /linux|darwin9|darwin10|freebsd|solaris/
35
+ end
36
+
37
+ def self.platform
38
+ if RUBY_PLATFORM =~ /java/
39
+ %x[uname -s].downcase
40
+ else
41
+ RUBY_PLATFORM.downcase
42
+ end
43
+ end
44
+ def platform
45
+ NewRelic::Agent::Samplers::MemorySampler.platform
46
+ end
47
+
48
+ def stats
49
+ stats_engine.get_stats("Memory/Physical", false)
50
+ end
51
+ def poll
52
+ sample = @sampler.get_sample
53
+ stats.record_data_point sample if sample
54
+ stats
55
+ end
56
+ class Base
57
+ def can_run?
58
+ return false if @broken
59
+ m = get_memory rescue nil
60
+ m && m > 0
61
+ end
62
+ def get_sample
63
+ return nil if @broken
64
+ begin
65
+ m = get_memory
66
+ if m.nil?
67
+ NewRelic::Agent.instance.log.error "Unable to get the resident memory for process #{$$}. Disabling memory sampler."
68
+ @broken = true
69
+ end
70
+ return m
71
+ rescue => e
72
+ NewRelic::Agent.instance.log.error "Unable to get the resident memory for process #{$$}. (#{e})"
73
+ NewRelic::Agent.instance.log.debug e.backtrace.join("\n ")
74
+ NewRelic::Agent.instance.log.error "Disabling memory sampler."
75
+ @broken = true
76
+ return nil
77
+ end
78
+ end
79
+ end
80
+
81
+ class JavaHeapSampler < Base
82
+
83
+ def get_memory
84
+ raise "Can't sample Java heap unless running in JRuby" unless defined? JRuby
85
+ java.lang.Runtime.getRuntime.totalMemory / (1024 * 1024).to_f rescue nil
86
+ end
87
+ def to_s
88
+ "JRuby Java heap sampler"
89
+ end
90
+ end
91
+
92
+ class ShellPS < Base
93
+ def initialize(command)
94
+ super()
95
+ @command = command
96
+ end
97
+ # Returns the amount of resident memory this process is using in MB
98
+ #
99
+ def get_memory
100
+ process = $$
101
+ memory = `#{@command} #{process}`.split("\n")[1].to_f / 1024.0 rescue nil
102
+ # if for some reason the ps command doesn't work on the resident os,
103
+ # then don't execute it any more.
104
+ raise "Faulty command: `#{@command} #{process}`" if memory.nil? || memory <= 0
105
+ memory
106
+ end
107
+ def to_s
108
+ "shell command sampler: #{@command}"
109
+ end
110
+ end
111
+
112
+ # ProcStatus
113
+ #
114
+ # A class that samples memory by reading the file /proc/$$/status, which is specific to linux
115
+ #
116
+ class ProcStatus < Base
117
+
118
+ # Returns the amount of resident memory this process is using in MB
119
+ #
120
+ def get_memory
121
+ File.open(proc_status_file, "r") do |f|
122
+ while !f.eof?
123
+ if f.readline =~ /RSS:\s*(\d+) kB/i
124
+ return $1.to_f / 1024.0
125
+ end
126
+ end
127
+ end
128
+ raise "Unable to find RSS in #{proc_status_file}"
129
+ end
130
+ def proc_status_file
131
+ "/proc/#{$$}/status"
132
+ end
133
+ def to_s
134
+ "proc status file sampler: #{proc_status_file}"
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,22 @@
1
+ module NewRelic::Agent::Samplers
2
+ # NewRelic Instrumentation for Mongrel - tracks the queue length of the mongrel server.
3
+ class MongrelSampler < NewRelic::Agent::Sampler
4
+ def initialize
5
+ super :mongrel
6
+ end
7
+ def queue_stats
8
+ stats_engine.get_stats("Mongrel/Queue Length", false)
9
+ end
10
+ def poll
11
+ mongrel = NewRelic::Control.instance.local_env.mongrel
12
+ if mongrel
13
+ # The mongrel workers list includes workers actively processing requests
14
+ # so you need to subtract what appears to be the active workers from the total
15
+ # number of workers to get the queue size.
16
+ qsize = mongrel.workers.list.length - NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
17
+ qsize = 0 if qsize < 0
18
+ queue_stats.record_data_point qsize
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # This agent is loaded by the plug when the plug-in is disabled
2
+ # It recreates just enough of the API to not break any clients that
3
+ # invoke the Agent.
4
+ class NewRelic::Agent::ShimAgent < NewRelic::Agent::Agent
5
+ def self.instance
6
+ @instance ||= self.new
7
+ end
8
+ def initialize
9
+ super
10
+ @histogram.extend NewRelic::Histogram::Shim
11
+ @stats_engine.extend NewRelic::Agent::StatsEngine::Shim
12
+ @stats_engine.extend NewRelic::Agent::StatsEngine::Transactions::Shim
13
+ @transaction_sampler.extend NewRelic::Agent::TransactionSampler::Shim
14
+ @error_collector.extend NewRelic::Agent::ErrorCollector::Shim
15
+ end
16
+ def ensure_worker_thread_started; end
17
+ def start *args; end
18
+ def shutdown; end
19
+ def push_trace_execution_flag(*args); end
20
+ def pop_trace_execution_flag(*args); end
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'new_relic/agent/stats_engine/metric_stats'
2
+ require 'new_relic/agent/stats_engine/samplers'
3
+ require 'new_relic/agent/stats_engine/transactions'
4
+
5
+ module NewRelic::Agent
6
+ class StatsEngine
7
+ include MetricStats
8
+ include Samplers
9
+ include Transactions
10
+
11
+ def initialize
12
+ # Makes the unit tests happy
13
+ Thread::current[:newrelic_scope_stack] = nil
14
+ spawn_sampler_thread
15
+ end
16
+
17
+ def log
18
+ NewRelic::Control.instance.log
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,111 @@
1
+ module NewRelic::Agent
2
+ class StatsEngine
3
+ module MetricStats
4
+ # The stats hash hashes either a metric name for an unscoped metric,
5
+ # or a metric_spec for a scoped metric value.
6
+ def lookup_stat(metric_name)
7
+ stats_hash[metric_name]
8
+ end
9
+
10
+ def metrics
11
+ stats_hash.keys.map(&:to_s)
12
+ end
13
+
14
+ def get_stats_no_scope(metric_name)
15
+ stats_hash[metric_name] ||= NewRelic::MethodTraceStats.new
16
+ end
17
+
18
+ # This version allows a caller to pass a stat class to use
19
+ #
20
+ def get_custom_stats(metric_name, stat_class)
21
+ stats_hash[metric_name] ||= stat_class.new
22
+ end
23
+
24
+ # If use_scope is true, two chained metrics are created, one with scope and one without
25
+ # If scoped_metric_only is true, only a scoped metric is created (used by rendering metrics which by definition are per controller only)
26
+ def get_stats(metric_name, use_scope = true, scoped_metric_only = false)
27
+
28
+ if scoped_metric_only
29
+ spec = NewRelic::MetricSpec.new metric_name, transaction_name
30
+ stats = stats_hash[spec] ||= NewRelic::MethodTraceStats.new
31
+ else
32
+ stats = stats_hash[metric_name] ||= NewRelic::MethodTraceStats.new
33
+ if use_scope && transaction_name
34
+ spec = NewRelic::MetricSpec.new metric_name, transaction_name
35
+ scoped_stats = stats_hash[spec] ||= NewRelic::ScopedMethodTraceStats.new(stats)
36
+ stats = scoped_stats
37
+ end
38
+ end
39
+ stats
40
+ end
41
+
42
+ # Harvest the timeslice data. First recombine current statss
43
+ # with any previously
44
+ # unsent metrics, clear out stats cache, and return the current
45
+ # stats.
46
+ # ---
47
+ # Note: this is not synchronized. There is still some risk in this and
48
+ # we will revisit later to see if we can make this more robust without
49
+ # sacrificing efficiency.
50
+ # +++
51
+ def harvest_timeslice_data(previous_timeslice_data, metric_ids)
52
+ timeslice_data = {}
53
+ poll harvest_samplers
54
+ stats_hash.keys.each do | metric_spec |
55
+
56
+
57
+ # get a copy of the stats collected since the last harvest, and clear
58
+ # the stats inside our hash table for the next time slice.
59
+ stats = stats_hash[metric_spec]
60
+
61
+ # we have an optimization for unscoped metrics
62
+ if !(metric_spec.is_a? NewRelic::MetricSpec)
63
+ metric_spec = NewRelic::MetricSpec.new metric_spec
64
+ end
65
+
66
+ if stats.nil?
67
+ raise "Nil stats for #{metric_spec.name} (#{metric_spec.scope})"
68
+ end
69
+
70
+ stats_copy = stats.clone
71
+ stats.reset
72
+
73
+ # if the previous timeslice data has not been reported (due to an error of some sort)
74
+ # then we need to merge this timeslice with the previously accumulated - but not sent
75
+ # data
76
+ previous_metric_data = previous_timeslice_data[metric_spec]
77
+ stats_copy.merge! previous_metric_data.stats unless previous_metric_data.nil?
78
+ stats_copy.round!
79
+
80
+ # don't bother collecting and reporting stats that have zero-values for this timeslice.
81
+ # significant performance boost and storage savings.
82
+ unless stats_copy.is_reset?
83
+
84
+ metric_spec_for_transport = (metric_ids[metric_spec].nil?) ? metric_spec : nil
85
+
86
+ metric_data = NewRelic::MetricData.new(metric_spec_for_transport, stats_copy, metric_ids[metric_spec])
87
+
88
+ timeslice_data[metric_spec] = metric_data
89
+ end
90
+ end
91
+
92
+ timeslice_data
93
+ end
94
+
95
+ # Remove all stats. For test code only.
96
+ def clear_stats
97
+ stats_hash.clear
98
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.reset
99
+ end
100
+
101
+ # Reset each of the stats, such as when a new passenger instance starts up.
102
+ def reset_stats
103
+ stats_hash.values.each { |s| s.reset }
104
+ end
105
+
106
+ def stats_hash
107
+ @stats_hash ||= {}
108
+ end
109
+ end
110
+ end
111
+ end