genki-newrelic_rpm 2.10.1

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 +316 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +156 -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/genki-newrelic_rpm.gemspec +32 -0
  10. data/init.rb +38 -0
  11. data/install.rb +37 -0
  12. data/lib/new_relic/agent.rb +280 -0
  13. data/lib/new_relic/agent/agent.rb +627 -0
  14. data/lib/new_relic/agent/chained_call.rb +13 -0
  15. data/lib/new_relic/agent/collection_helper.rb +61 -0
  16. data/lib/new_relic/agent/error_collector.rb +125 -0
  17. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  18. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +83 -0
  19. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  20. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +368 -0
  21. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  22. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +132 -0
  23. data/lib/new_relic/agent/instrumentation/memcache.rb +21 -0
  24. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  25. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  26. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  27. data/lib/new_relic/agent/instrumentation/net.rb +12 -0
  28. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  29. data/lib/new_relic/agent/instrumentation/rack.rb +77 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  31. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  32. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +38 -0
  33. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  34. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  35. data/lib/new_relic/agent/method_tracer.rb +277 -0
  36. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  37. data/lib/new_relic/agent/sampler.rb +12 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  39. data/lib/new_relic/agent/samplers/memory_sampler.rb +137 -0
  40. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  41. data/lib/new_relic/agent/shim_agent.rb +21 -0
  42. data/lib/new_relic/agent/stats_engine.rb +24 -0
  43. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  44. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  45. data/lib/new_relic/agent/stats_engine/transactions.rb +155 -0
  46. data/lib/new_relic/agent/transaction_sampler.rb +319 -0
  47. data/lib/new_relic/agent/worker_loop.rb +118 -0
  48. data/lib/new_relic/commands/deployments.rb +145 -0
  49. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  50. data/lib/new_relic/control.rb +436 -0
  51. data/lib/new_relic/control/external.rb +13 -0
  52. data/lib/new_relic/control/merb.rb +22 -0
  53. data/lib/new_relic/control/rails.rb +143 -0
  54. data/lib/new_relic/control/ruby.rb +34 -0
  55. data/lib/new_relic/control/sinatra.rb +14 -0
  56. data/lib/new_relic/histogram.rb +89 -0
  57. data/lib/new_relic/local_environment.rb +285 -0
  58. data/lib/new_relic/merbtasks.rb +6 -0
  59. data/lib/new_relic/metric_data.rb +44 -0
  60. data/lib/new_relic/metric_parser.rb +120 -0
  61. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  62. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  63. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  64. data/lib/new_relic/metric_parser/controller.rb +54 -0
  65. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  66. data/lib/new_relic/metric_parser/errors.rb +6 -0
  67. data/lib/new_relic/metric_parser/external.rb +50 -0
  68. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  69. data/lib/new_relic/metric_parser/view.rb +61 -0
  70. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  71. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  72. data/lib/new_relic/metric_spec.rb +52 -0
  73. data/lib/new_relic/metrics.rb +7 -0
  74. data/lib/new_relic/noticed_error.rb +25 -0
  75. data/lib/new_relic/rack/metric_app.rb +56 -0
  76. data/lib/new_relic/rack/newrelic.ru +25 -0
  77. data/lib/new_relic/rack/newrelic.yml +25 -0
  78. data/lib/new_relic/rack_app.rb +5 -0
  79. data/lib/new_relic/recipes.rb +82 -0
  80. data/lib/new_relic/stats.rb +360 -0
  81. data/lib/new_relic/transaction_analysis.rb +121 -0
  82. data/lib/new_relic/transaction_sample.rb +583 -0
  83. data/lib/new_relic/version.rb +54 -0
  84. data/lib/new_relic_api.rb +315 -0
  85. data/lib/newrelic_rpm.rb +40 -0
  86. data/lib/tasks/all.rb +4 -0
  87. data/lib/tasks/install.rake +7 -0
  88. data/lib/tasks/tests.rake +13 -0
  89. data/newrelic.yml +214 -0
  90. data/recipes/newrelic.rb +6 -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 +234 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +117 -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 +118 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +155 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +335 -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 +51 -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 +78 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +177 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +67 -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 +146 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +387 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +94 -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 +214 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +314 -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 +15 -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 +45 -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 +77 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +433 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +327 -0
@@ -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,137 @@
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?(Java) 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
+ end
77
+ end
78
+ end
79
+
80
+ class JavaHeapSampler < Base
81
+
82
+ def get_memory
83
+ raise "Can't sample Java heap unless running in JRuby" unless defined? Java
84
+ java.lang.Runtime.getRuntime.totalMemory / (1024 * 1024).to_f rescue nil
85
+ end
86
+ def to_s
87
+ "JRuby Java heap sampler"
88
+ end
89
+ end
90
+
91
+ class ShellPS < Base
92
+ def initialize(command)
93
+ super()
94
+ @command = command
95
+ end
96
+ # Returns the amount of resident memory this process is using in MB
97
+ #
98
+ def get_memory
99
+ process = $$
100
+ memory = `#{@command} #{process}`.split("\n")[1].to_f / 1024.0 rescue nil
101
+ # if for some reason the ps command doesn't work on the resident os,
102
+ # then don't execute it any more.
103
+ raise "Faulty command: `#{@command} #{process}`" if memory.nil? || memory <= 0
104
+ memory
105
+ end
106
+ def to_s
107
+ "shell command sampler: #{@command}"
108
+ end
109
+ end
110
+
111
+ # ProcStatus
112
+ #
113
+ # A class that samples memory by reading the file /proc/$$/status, which is specific to linux
114
+ #
115
+ class ProcStatus < Base
116
+
117
+ # Returns the amount of resident memory this process is using in MB
118
+ #
119
+ def get_memory
120
+ File.open(proc_status_file, "r") do |f|
121
+ while !f.eof?
122
+ if f.readline =~ /RSS:\s*(\d+) kB/i
123
+ return $1.to_f / 1024.0
124
+ end
125
+ end
126
+ end
127
+ raise "Unable to find RSS in #{proc_status_file}"
128
+ end
129
+ def proc_status_file
130
+ "/proc/#{$$}/status"
131
+ end
132
+ def to_s
133
+ "proc status file sampler: #{proc_status_file}"
134
+ end
135
+ end
136
+ end
137
+ 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,24 @@
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
+ # race condition
11
+ # include Transactions::Shim if NewRelic::Control.instance['disable_scope_tracking']
12
+
13
+ def initialize
14
+ # Makes the unit tests happy
15
+ Thread::current[:newrelic_scope_stack] = nil
16
+ spawn_sampler_thread
17
+ end
18
+
19
+ def log
20
+ NewRelic::Control.instance.log
21
+ end
22
+
23
+ end
24
+ 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
@@ -0,0 +1,71 @@
1
+ module NewRelic::Agent
2
+ class StatsEngine
3
+ module Shim # :nodoc:
4
+ def add_sampler(*args); end
5
+ def add_harvest_sampler(*args); end
6
+ def spawn_sampler_thread(*args); end
7
+ end
8
+
9
+ module Samplers
10
+
11
+ POLL_PERIOD = 10
12
+
13
+ def spawn_sampler_thread
14
+
15
+ return if !@sampler_process.nil? && @sampler_process == $$
16
+
17
+ # start up a thread that will periodically poll for metric samples
18
+ @sampler_thread = Thread.new do
19
+ while true do
20
+ begin
21
+ sleep POLL_PERIOD
22
+ poll periodic_samplers
23
+ end
24
+ end
25
+ end
26
+ @sampler_thread['newrelic_label'] = 'Sampler Tasks'
27
+ @sampler_process = $$
28
+ end
29
+
30
+ # Add an instance of Sampler to be invoked about every 10 seconds on a background
31
+ # thread.
32
+ def add_sampler sampler
33
+ periodic_samplers << sampler
34
+ sampler.stats_engine = self
35
+ log.debug "Adding sampler #{sampler.id.to_s}"
36
+ end
37
+
38
+ # Add a sampler to be invoked just before each harvest.
39
+ def add_harvest_sampler sampler
40
+ harvest_samplers << sampler
41
+ sampler.stats_engine = self
42
+ log.debug "Adding harvest time sampler: #{sampler.id.to_s}"
43
+ end
44
+
45
+ private
46
+
47
+ # Call poll on each of the samplers. Remove
48
+ # the sampler if it raises.
49
+ def poll(samplers)
50
+ samplers.delete_if do |sampled_item|
51
+ begin
52
+ sampled_item.poll
53
+ false # it's okay. don't delete it.
54
+ rescue => e
55
+ log.error "Removing #{sampled_item} from list"
56
+ log.error e
57
+ log.debug e.backtrace.to_s
58
+ true # remove the sampler
59
+ end
60
+ end
61
+ end
62
+
63
+ def harvest_samplers
64
+ @harvest_samplers ||= []
65
+ end
66
+ def periodic_samplers
67
+ @periodic_samplers ||= []
68
+ end
69
+ end
70
+ end
71
+ end