honkster-newrelic_rpm 2.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. data/CHANGELOG +462 -0
  2. data/LICENSE +37 -0
  3. data/README.rdoc +172 -0
  4. data/bin/mongrel_rpm +33 -0
  5. data/bin/newrelic +13 -0
  6. data/bin/newrelic_cmd +5 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +9 -0
  9. data/lib/new_relic/agent.rb +382 -0
  10. data/lib/new_relic/agent/agent.rb +741 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/error_collector.rb +131 -0
  14. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  15. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +92 -0
  16. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +45 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +409 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +58 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +22 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +40 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +9 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +319 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +17 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +22 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  28. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  30. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  31. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +45 -0
  32. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +21 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -0
  34. data/lib/new_relic/agent/instrumentation/sunspot.rb +17 -0
  35. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +10 -0
  36. data/lib/new_relic/agent/method_tracer.rb +350 -0
  37. data/lib/new_relic/agent/sampler.rb +50 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +54 -0
  39. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +37 -0
  40. data/lib/new_relic/agent/samplers/memory_sampler.rb +142 -0
  41. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  42. data/lib/new_relic/agent/shim_agent.rb +25 -0
  43. data/lib/new_relic/agent/stats_engine.rb +24 -0
  44. data/lib/new_relic/agent/stats_engine/metric_stats.rb +118 -0
  45. data/lib/new_relic/agent/stats_engine/samplers.rb +83 -0
  46. data/lib/new_relic/agent/stats_engine/transactions.rb +149 -0
  47. data/lib/new_relic/agent/transaction_sampler.rb +330 -0
  48. data/lib/new_relic/agent/worker_loop.rb +81 -0
  49. data/lib/new_relic/collection_helper.rb +71 -0
  50. data/lib/new_relic/command.rb +85 -0
  51. data/lib/new_relic/commands/deployments.rb +105 -0
  52. data/lib/new_relic/commands/install.rb +81 -0
  53. data/lib/new_relic/control.rb +203 -0
  54. data/lib/new_relic/control/configuration.rb +149 -0
  55. data/lib/new_relic/control/frameworks/external.rb +13 -0
  56. data/lib/new_relic/control/frameworks/merb.rb +24 -0
  57. data/lib/new_relic/control/frameworks/rails.rb +126 -0
  58. data/lib/new_relic/control/frameworks/rails3.rb +60 -0
  59. data/lib/new_relic/control/frameworks/ruby.rb +36 -0
  60. data/lib/new_relic/control/frameworks/sinatra.rb +18 -0
  61. data/lib/new_relic/control/instrumentation.rb +95 -0
  62. data/lib/new_relic/control/logging_methods.rb +74 -0
  63. data/lib/new_relic/control/profiling.rb +24 -0
  64. data/lib/new_relic/control/server_methods.rb +88 -0
  65. data/lib/new_relic/delayed_job_injection.rb +27 -0
  66. data/lib/new_relic/histogram.rb +91 -0
  67. data/lib/new_relic/local_environment.rb +333 -0
  68. data/lib/new_relic/merbtasks.rb +6 -0
  69. data/lib/new_relic/metric_data.rb +42 -0
  70. data/lib/new_relic/metric_parser.rb +136 -0
  71. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  72. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  73. data/lib/new_relic/metric_parser/active_record.rb +28 -0
  74. data/lib/new_relic/metric_parser/apdex.rb +88 -0
  75. data/lib/new_relic/metric_parser/controller.rb +62 -0
  76. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  77. data/lib/new_relic/metric_parser/errors.rb +6 -0
  78. data/lib/new_relic/metric_parser/external.rb +50 -0
  79. data/lib/new_relic/metric_parser/mem_cache.rb +50 -0
  80. data/lib/new_relic/metric_parser/other_transaction.rb +36 -0
  81. data/lib/new_relic/metric_parser/view.rb +61 -0
  82. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  83. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  84. data/lib/new_relic/metric_spec.rb +67 -0
  85. data/lib/new_relic/metrics.rb +9 -0
  86. data/lib/new_relic/noticed_error.rb +24 -0
  87. data/lib/new_relic/rack/developer_mode.rb +257 -0
  88. data/lib/new_relic/rack/metric_app.rb +64 -0
  89. data/lib/new_relic/rack/mongrel_rpm.ru +26 -0
  90. data/lib/new_relic/rack/newrelic.yml +27 -0
  91. data/lib/new_relic/rack_app.rb +6 -0
  92. data/lib/new_relic/recipes.rb +82 -0
  93. data/lib/new_relic/stats.rb +368 -0
  94. data/lib/new_relic/timer_lib.rb +27 -0
  95. data/lib/new_relic/transaction_analysis.rb +119 -0
  96. data/lib/new_relic/transaction_sample.rb +586 -0
  97. data/lib/new_relic/url_rule.rb +14 -0
  98. data/lib/new_relic/version.rb +55 -0
  99. data/lib/new_relic_api.rb +276 -0
  100. data/lib/newrelic_rpm.rb +49 -0
  101. data/lib/tasks/all.rb +4 -0
  102. data/lib/tasks/install.rake +7 -0
  103. data/lib/tasks/tests.rake +15 -0
  104. data/newrelic.yml +246 -0
  105. data/newrelic_rpm.gemspec +254 -0
  106. data/recipes/newrelic.rb +6 -0
  107. data/test/active_record_fixtures.rb +55 -0
  108. data/test/config/newrelic.yml +48 -0
  109. data/test/config/test_control.rb +36 -0
  110. data/test/new_relic/agent/active_record_instrumentation_test.rb +286 -0
  111. data/test/new_relic/agent/agent_controller_test.rb +294 -0
  112. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  113. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  114. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  115. data/test/new_relic/agent/error_collector_test.rb +163 -0
  116. data/test/new_relic/agent/memcache_instrumentation_test.rb +103 -0
  117. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  118. data/test/new_relic/agent/metric_data_test.rb +53 -0
  119. data/test/new_relic/agent/metric_frame_test.rb +51 -0
  120. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  121. data/test/new_relic/agent/net_instrumentation_test.rb +77 -0
  122. data/test/new_relic/agent/rpm_agent_test.rb +142 -0
  123. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  124. data/test/new_relic/agent/stats_engine/samplers_test.rb +72 -0
  125. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  126. data/test/new_relic/agent/task_instrumentation_test.rb +188 -0
  127. data/test/new_relic/agent/testable_agent.rb +13 -0
  128. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  129. data/test/new_relic/agent/transaction_sample_test.rb +192 -0
  130. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  131. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  132. data/test/new_relic/control_test.rb +127 -0
  133. data/test/new_relic/deployments_api_test.rb +69 -0
  134. data/test/new_relic/environment_test.rb +75 -0
  135. data/test/new_relic/metric_parser_test.rb +226 -0
  136. data/test/new_relic/metric_spec_test.rb +177 -0
  137. data/test/new_relic/rack/episodes_test.rb +318 -0
  138. data/test/new_relic/shim_agent_test.rb +9 -0
  139. data/test/new_relic/stats_test.rb +312 -0
  140. data/test/new_relic/version_number_test.rb +89 -0
  141. data/test/test_contexts.rb +28 -0
  142. data/test/test_helper.rb +72 -0
  143. data/ui/helpers/developer_mode_helper.rb +359 -0
  144. data/ui/helpers/google_pie_chart.rb +49 -0
  145. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  146. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  147. data/ui/views/newrelic/_sample.rhtml +20 -0
  148. data/ui/views/newrelic/_segment.rhtml +29 -0
  149. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  150. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  151. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  152. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  153. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  154. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  155. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  156. data/ui/views/newrelic/_table.rhtml +12 -0
  157. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  158. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  159. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  160. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  161. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  162. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  163. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  164. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  165. data/ui/views/newrelic/file/images/textmate.png +0 -0
  166. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  167. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  168. data/ui/views/newrelic/file/stylesheets/style.css +484 -0
  169. data/ui/views/newrelic/index.rhtml +59 -0
  170. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  171. data/ui/views/newrelic/show_sample.rhtml +79 -0
  172. data/ui/views/newrelic/show_source.rhtml +3 -0
  173. data/ui/views/newrelic/threads.rhtml +52 -0
  174. metadata +307 -0
@@ -0,0 +1,50 @@
1
+ # A Sampler is used to capture meaningful metrics in a background thread
2
+ # periodically. They will either be invoked once a minute just before the
3
+ # data is sent to the agent (default) or every 10 seconds, when #use_harvest_sampler?
4
+ # returns false.
5
+ #
6
+ # Samplers can be added to New Relic by subclassing NewRelic::Agent::Sampler.
7
+ # Instances are created when the agent is enabled and installed. Subclasses
8
+ # are registered for instantiation automatically.
9
+ module NewRelic
10
+ module Agent
11
+ class Sampler
12
+
13
+ # Exception denotes a sampler is not available and it will not be registered.
14
+ class Unsupported < StandardError; end
15
+
16
+ attr_accessor :stats_engine
17
+ attr_reader :id
18
+ @sampler_classes = []
19
+
20
+ def self.inherited(subclass)
21
+ @sampler_classes << subclass
22
+ end
23
+
24
+ # Override with check. Called before instantiating.
25
+ def self.supported_on_this_platform?
26
+ true
27
+ end
28
+
29
+ # Override to use the periodic sampler instead of running the sampler on the
30
+ # minute during harvests.
31
+ def self.use_harvest_sampler?
32
+ true
33
+ end
34
+
35
+ def self.sampler_classes
36
+ @sampler_classes
37
+ end
38
+
39
+ def initialize(id)
40
+ @id = id
41
+ end
42
+
43
+ def poll
44
+ raise "Implement in the subclass"
45
+ end
46
+
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,54 @@
1
+ module NewRelic
2
+ module Agent
3
+ module Samplers
4
+ class CpuSampler < NewRelic::Agent::Sampler
5
+ attr_reader :last_time
6
+ def initialize
7
+ super :cpu
8
+ poll
9
+ end
10
+
11
+ def user_util_stats
12
+ stats_engine.get_stats_no_scope("CPU/User/Utilization")
13
+ end
14
+ def system_util_stats
15
+ stats_engine.get_stats_no_scope("CPU/System/Utilization")
16
+ end
17
+ def usertime_stats
18
+ stats_engine.get_stats_no_scope("CPU/User Time")
19
+ end
20
+ def systemtime_stats
21
+ stats_engine.get_stats_no_scope("CPU/System Time")
22
+ end
23
+
24
+ def self.supported_on_this_platform?
25
+ not defined?(JRuby)
26
+ end
27
+
28
+ def poll
29
+ now = Time.now
30
+ t = Process.times
31
+ if @last_time
32
+ elapsed = now - @last_time
33
+ return if elapsed < 1 # Causing some kind of math underflow
34
+ num_processors = NewRelic::Control.instance.local_env.processors || 1
35
+ usertime = t.utime - @last_utime
36
+ systemtime = t.stime - @last_stime
37
+
38
+ systemtime_stats.record_data_point(systemtime) if systemtime >= 0
39
+ usertime_stats.record_data_point(usertime) if usertime >= 0
40
+
41
+ # Calculate the true utilization by taking cpu times and dividing by
42
+ # elapsed time X num_processors.
43
+ user_util_stats.record_data_point usertime / (elapsed * num_processors)
44
+ system_util_stats.record_data_point systemtime / (elapsed * num_processors)
45
+ end
46
+ @last_utime = t.utime
47
+ @last_stime = t.stime
48
+ @last_time = now
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,37 @@
1
+ module NewRelic
2
+ module Agent
3
+ module Samplers
4
+ class DelayedJobLockSampler < NewRelic::Agent::Sampler
5
+ def initialize
6
+ super :delayed_job_lock
7
+ raise Unsupported, "DJ instrumentation disabled" if NewRelic::Control.instance['disable_dj']
8
+ raise Unsupported, "No DJ worker present" unless NewRelic::DelayedJobInjection.worker_name
9
+ end
10
+
11
+ def stats
12
+ stats_engine.get_stats("Custom/DJ Locked Jobs", false)
13
+ end
14
+
15
+ def local_env
16
+ NewRelic::Control.instance.local_env
17
+ end
18
+
19
+ def worker_name
20
+ local_env.dispatcher_instance_id
21
+ end
22
+
23
+ def locked_jobs
24
+ Delayed::Job.count(:conditions => {:locked_by => NewRelic::DelayedJobInjection.worker_name})
25
+ end
26
+
27
+ def self.supported_on_this_platform?
28
+ defined?(Delayed::Job)
29
+ end
30
+
31
+ def poll
32
+ stats.record_data_point locked_jobs
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,142 @@
1
+ module NewRelic
2
+ module Agent
3
+ module Samplers
4
+
5
+ class MemorySampler < NewRelic::Agent::Sampler
6
+ attr_accessor :sampler
7
+
8
+ def initialize
9
+ super :memory
10
+ # macos, linux, solaris
11
+ if defined? JRuby
12
+ @sampler = JavaHeapSampler.new
13
+ elsif platform =~ /linux/
14
+ @sampler = ProcStatus.new
15
+ if !@sampler.can_run?
16
+ NewRelic::Agent.instance.warn.debug "Error attempting to use /proc/#{$$}/status file for reading memory. Using ps command instead."
17
+ @sampler = ShellPS.new("ps -o rsz")
18
+ else
19
+ NewRelic::Agent.instance.log.debug "Using /proc/#{$$}/status for reading process memory."
20
+ end
21
+ elsif platform =~ /darwin9/ # 10.5
22
+ @sampler = ShellPS.new("ps -o rsz")
23
+ elsif platform =~ /darwin10/ # 10.6
24
+ @sampler = ShellPS.new("ps -o rss")
25
+ elsif platform =~ /freebsd/
26
+ @sampler = ShellPS.new("ps -o rss")
27
+ elsif platform =~ /solaris/
28
+ @sampler = ShellPS.new("/usr/bin/ps -o rss -p")
29
+ end
30
+
31
+ raise Unsupported, "Unsupported platform for getting memory: #{platform}" if @sampler.nil?
32
+ raise Unsupported, "Unable to run #{@sampler}" unless @sampler.can_run?
33
+ end
34
+
35
+ def self.supported_on_this_platform?
36
+ defined?(JRuby) or platform =~ /linux|darwin9|darwin10|freebsd|solaris/
37
+ end
38
+
39
+ def self.platform
40
+ if RUBY_PLATFORM =~ /java/
41
+ %x[uname -s].downcase
42
+ else
43
+ RUBY_PLATFORM.downcase
44
+ end
45
+ end
46
+ def platform
47
+ NewRelic::Agent::Samplers::MemorySampler.platform
48
+ end
49
+
50
+ def stats
51
+ stats_engine.get_stats("Memory/Physical", false)
52
+ end
53
+ def poll
54
+ sample = @sampler.get_sample
55
+ stats.record_data_point sample if sample
56
+ stats
57
+ end
58
+ class Base
59
+ def can_run?
60
+ return false if @broken
61
+ m = get_memory rescue nil
62
+ m && m > 0
63
+ end
64
+ def get_sample
65
+ return nil if @broken
66
+ begin
67
+ m = get_memory
68
+ if m.nil?
69
+ NewRelic::Agent.instance.log.error "Unable to get the resident memory for process #{$$}. Disabling memory sampler."
70
+ @broken = true
71
+ end
72
+ return m
73
+ rescue => e
74
+ NewRelic::Agent.instance.log.error "Unable to get the resident memory for process #{$$}. (#{e})"
75
+ NewRelic::Agent.instance.log.debug e.backtrace.join("\n ")
76
+ NewRelic::Agent.instance.log.error "Disabling memory sampler."
77
+ @broken = true
78
+ return nil
79
+ end
80
+ end
81
+ end
82
+
83
+ class JavaHeapSampler < Base
84
+
85
+ def get_memory
86
+ raise "Can't sample Java heap unless running in JRuby" unless defined? JRuby
87
+ java.lang.Runtime.getRuntime.totalMemory / (1024 * 1024).to_f rescue nil
88
+ end
89
+ def to_s
90
+ "JRuby Java heap sampler"
91
+ end
92
+ end
93
+
94
+ class ShellPS < Base
95
+ def initialize(command)
96
+ super()
97
+ @command = command
98
+ end
99
+ # Returns the amount of resident memory this process is using in MB
100
+ #
101
+ def get_memory
102
+ process = $$
103
+ memory = `#{@command} #{process}`.split("\n")[1].to_f / 1024.0 rescue nil
104
+ # if for some reason the ps command doesn't work on the resident os,
105
+ # then don't execute it any more.
106
+ raise "Faulty command: `#{@command} #{process}`" if memory.nil? || memory <= 0
107
+ memory
108
+ end
109
+ def to_s
110
+ "shell command sampler: #{@command}"
111
+ end
112
+ end
113
+
114
+ # ProcStatus
115
+ #
116
+ # A class that samples memory by reading the file /proc/$$/status, which is specific to linux
117
+ #
118
+ class ProcStatus < Base
119
+
120
+ # Returns the amount of resident memory this process is using in MB
121
+ #
122
+ def get_memory
123
+ File.open(proc_status_file, "r") do |f|
124
+ while !f.eof?
125
+ if f.readline =~ /RSS:\s*(\d+) kB/i
126
+ return $1.to_f / 1024.0
127
+ end
128
+ end
129
+ end
130
+ raise "Unable to find RSS in #{proc_status_file}"
131
+ end
132
+ def proc_status_file
133
+ "/proc/#{$$}/status"
134
+ end
135
+ def to_s
136
+ "proc status file sampler: #{proc_status_file}"
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,24 @@
1
+ module NewRelic
2
+ module Agent
3
+ module Samplers
4
+ class ObjectSampler < NewRelic::Agent::Sampler
5
+
6
+ def initialize
7
+ super :objects
8
+ end
9
+
10
+ def stats
11
+ stats_engine.get_stats_no_scope("GC/objects")
12
+ end
13
+
14
+ def self.supported_on_this_platform?
15
+ defined?(ObjectSpace) && ObjectSpace.respond_to?(:live_objects)
16
+ end
17
+
18
+ def poll
19
+ stats.record_data_point(ObjectSpace.live_objects)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
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
+ module NewRelic
5
+ module Agent
6
+ class ShimAgent < NewRelic::Agent::Agent
7
+ def self.instance
8
+ @instance ||= self.new
9
+ end
10
+ def initialize
11
+ super
12
+ @histogram.extend NewRelic::Histogram::Shim
13
+ @stats_engine.extend NewRelic::Agent::StatsEngine::Shim
14
+ @stats_engine.extend NewRelic::Agent::StatsEngine::Transactions::Shim
15
+ @transaction_sampler.extend NewRelic::Agent::TransactionSampler::Shim
16
+ @error_collector.extend NewRelic::Agent::ErrorCollector::Shim
17
+ end
18
+ def after_fork *args; end
19
+ def start *args; end
20
+ def shutdown; end
21
+ def push_trace_execution_flag(*args); end
22
+ def pop_trace_execution_flag(*args); end
23
+ end
24
+ end
25
+ 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
6
+ module Agent
7
+ class StatsEngine
8
+ include MetricStats
9
+ include Samplers
10
+ include Transactions
11
+
12
+ def initialize
13
+ # Makes the unit tests happy
14
+ Thread::current[:newrelic_scope_stack] = nil
15
+ start_sampler_thread
16
+ end
17
+
18
+ def log
19
+ NewRelic::Control.instance.log
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,118 @@
1
+ module NewRelic
2
+ module Agent
3
+ class StatsEngine
4
+ module MetricStats
5
+ # The stats hash hashes either a metric name for an unscoped metric,
6
+ # or a metric_spec for a scoped metric value.
7
+ def lookup_stat(metric_name)
8
+ stats_hash[metric_name]
9
+ end
10
+
11
+ def metrics
12
+ stats_hash.keys.map(&:to_s)
13
+ end
14
+
15
+ def get_stats_no_scope(metric_name)
16
+ stats_hash[metric_name] ||= NewRelic::MethodTraceStats.new
17
+ end
18
+
19
+ # This version allows a caller to pass a stat class to use
20
+ #
21
+ def get_custom_stats(metric_name, stat_class)
22
+ stats_hash[metric_name] ||= stat_class.new
23
+ end
24
+
25
+ # If use_scope is true, two chained metrics are created, one with scope and one without
26
+ # If scoped_metric_only is true, only a scoped metric is created (used by rendering metrics which by definition are per controller only)
27
+ def get_stats(metric_name, use_scope = true, scoped_metric_only = false, scope = nil)
28
+ scope ||= scope_name if use_scope
29
+ if scoped_metric_only
30
+ spec = NewRelic::MetricSpec.new metric_name, scope
31
+ stats = stats_hash[spec] ||= NewRelic::MethodTraceStats.new
32
+ else
33
+ stats = stats_hash[metric_name] ||= NewRelic::MethodTraceStats.new
34
+ if scope && scope != metric_name
35
+ spec = NewRelic::MetricSpec.new metric_name, scope
36
+ scoped_stats = stats_hash[spec] ||= NewRelic::ScopedMethodTraceStats.new(stats)
37
+ stats = scoped_stats
38
+ end
39
+ end
40
+ stats
41
+ end
42
+
43
+ def lookup_stats(metric_name, scope_name = nil)
44
+ stats_hash[NewRelic::MetricSpec.new(metric_name, scope_name)] ||
45
+ stats_hash[metric_name]
46
+ end
47
+ # Harvest the timeslice data. First recombine current statss
48
+ # with any previously
49
+ # unsent metrics, clear out stats cache, and return the current
50
+ # stats.
51
+ # ---
52
+ # Note: this is not synchronized. There is still some risk in this and
53
+ # we will revisit later to see if we can make this more robust without
54
+ # sacrificing efficiency.
55
+ # +++
56
+ def harvest_timeslice_data(previous_timeslice_data, metric_ids)
57
+ timeslice_data = {}
58
+ poll harvest_samplers
59
+ stats_hash.keys.each do | metric_spec |
60
+
61
+
62
+ # get a copy of the stats collected since the last harvest, and clear
63
+ # the stats inside our hash table for the next time slice.
64
+ stats = stats_hash[metric_spec]
65
+
66
+ # we have an optimization for unscoped metrics
67
+ if !(metric_spec.is_a? NewRelic::MetricSpec)
68
+ metric_spec = NewRelic::MetricSpec.new metric_spec
69
+ end
70
+
71
+ if stats.nil?
72
+ raise "Nil stats for #{metric_spec.name} (#{metric_spec.scope})"
73
+ end
74
+
75
+ stats_copy = stats.clone
76
+ stats.reset
77
+
78
+ # if the previous timeslice data has not been reported (due to an error of some sort)
79
+ # then we need to merge this timeslice with the previously accumulated - but not sent
80
+ # data
81
+ previous_metric_data = previous_timeslice_data[metric_spec]
82
+ stats_copy.merge! previous_metric_data.stats unless previous_metric_data.nil?
83
+ stats_copy.round!
84
+
85
+ # don't bother collecting and reporting stats that have zero-values for this timeslice.
86
+ # significant performance boost and storage savings.
87
+ unless stats_copy.is_reset?
88
+
89
+ id = metric_ids[metric_spec]
90
+ metric_spec_for_transport = id ? nil : metric_spec
91
+
92
+ metric_data = NewRelic::MetricData.new(metric_spec_for_transport, stats_copy, id)
93
+
94
+ timeslice_data[metric_spec] = metric_data
95
+ end
96
+ end
97
+
98
+ timeslice_data
99
+ end
100
+
101
+ # Remove all stats. For test code only.
102
+ def clear_stats
103
+ stats_hash.clear
104
+ NewRelic::Agent::BusyCalculator.reset
105
+ end
106
+
107
+ # Reset each of the stats, such as when a new passenger instance starts up.
108
+ def reset_stats
109
+ stats_hash.values.each { |s| s.reset }
110
+ end
111
+
112
+ def stats_hash
113
+ @stats_hash ||= {}
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end