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,64 @@
1
+ require 'fileutils'
2
+ module NewRelic
3
+ module Rack
4
+ class MetricApp
5
+ def initialize(options)
6
+ if options[:install]
7
+ src = File.join(File.dirname(__FILE__), "newrelic.yml")
8
+ require 'new_relic/command'
9
+ begin
10
+ NewRelic::Command::Install.new(:quiet => true, :src_file => src).run
11
+ NewRelic::Agent.logger.info "A newrelic.yml template was copied to #{File.expand_path('.')}."
12
+ NewRelic::Agent.logger.info "Please add a license key to the file and restart #{$0}"
13
+ exit 0
14
+ rescue NewRelic::Command::CommandFailure => e
15
+ NewRelic::Agent.logger.error e.message
16
+ exit 1
17
+ end
18
+ end
19
+ options[:app_name] ||= 'EPM Monitor'
20
+ options[:disable_samplers] = true
21
+ NewRelic::Agent.manual_start options
22
+ unless NewRelic::Control.instance.license_key
23
+ raise "Please add a valid license key to newrelic.yml."
24
+ end
25
+ end
26
+ def call(env)
27
+ request = ::Rack::Request.new env
28
+ params = request.params
29
+ if !(params['uri'] || params['metric'])
30
+ [400, { 'Content-Type' => 'text/plain' }, "Missing 'uri' or 'metric' parameter: #{params.inspect}" ]
31
+ elsif !params['value']
32
+ [400, { 'Content-Type' => 'text/plain' }, "Missing 'value' parameter: #{params.inspect}" ]
33
+ else
34
+ NewRelic::Agent.record_transaction( params['value'].to_f, params )
35
+ ::Rack::Response.new(params.collect { |k, v| "#{k}=#{v} " }.join).finish
36
+ end
37
+ end
38
+ end
39
+ class Status
40
+ def call(env)
41
+ request = ::Rack::Request.new env
42
+ data_url = "http://#{env['HTTP_HOST']}/metrics/path?value=nnn"
43
+ body = StringIO.new
44
+ body.puts "<html><body>"
45
+ body.puts "<h1>New Relic Actively Monitoring #{NewRelic::Control.instance.app_names.join(' and ')}</h1>"
46
+ body.puts "<p>To submit a metric value, use <a href='#{data_url}'>#{data_url}</a></p>"
47
+ body.puts "<h2>Request Details</h2>"
48
+ body.puts "<dl>"
49
+ body.puts "<dt>ip<dd>#{request.ip}"
50
+ body.puts "<dt>host<dd>#{request.host}"
51
+ body.puts "<dt>path<dd>#{request.url}"
52
+ body.puts "<dt>query<dd>#{request.query_string}"
53
+ body.puts "<dt>params<dd>#{request.params.inspect}"
54
+ body.puts "</dl>"
55
+ body.puts "<h2>Complete ENV</h2>"
56
+ body.puts "<ul>"
57
+ body.puts env.to_a.map{|k,v| "<li>#{k} = #{v}</li>" }.join("\n")
58
+ body.puts "</ul></body></html>"
59
+ response = ::Rack::Response.new body.string
60
+ response.finish
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ # This is so that we don't detect a dispatcher like mongrel and think we are
2
+ # monitoring it.
3
+ ENV['NEWRELIC_DISPATCHER'] = 'none'
4
+
5
+ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..','..'))
6
+
7
+ require 'new_relic/rack_app'
8
+
9
+ # Valid options which may be present in this binding:
10
+ # :license_key optional license key override
11
+ # :app_name optional name of app
12
+ # :logging optional, false to omit request logging to stdout
13
+
14
+ # use Rack::CommonLogger unless options[:logging] == false
15
+ # use Rack::ShowExceptions
16
+ # use Rack::Reloader if ENV['RACK_ENV'] == 'development'
17
+
18
+ map "/newrelic/record_value" do
19
+ run NewRelic::Rack::MetricApp.new(options)
20
+ end
21
+
22
+ map "/" do
23
+ run NewRelic::Rack::Status.new
24
+ end
25
+
26
+
@@ -0,0 +1,27 @@
1
+ #
2
+ # This file configures the Rack NewRelic Metric Application. Place a copy of this file
3
+ # in the directory where you start the mongrel_rpm process.
4
+ #
5
+ # <%= generated_for_user %>
6
+ #
7
+ common: &default_settings
8
+ log_level: info
9
+ enabled: true
10
+
11
+ # This is the key generated by script/create_admin
12
+ license_key: '<%= license_key %>'
13
+
14
+ # Replace this with the name of the application feeding
15
+ # metrics into the Metric application;
16
+ app_name: Application Monitor
17
+
18
+ # Set RACK_ENV to one of the following:
19
+
20
+ production:
21
+ <<: *default_settings
22
+ app_name: Production Monitor
23
+
24
+ staging:
25
+ <<: *default_settings
26
+ app_name: Staging Monitor
27
+
@@ -0,0 +1,6 @@
1
+ require 'rack/response'
2
+ require 'newrelic_rpm'
3
+
4
+ # Rack middlewares and applications used by New Relic
5
+ require 'new_relic/rack/metric_app'
6
+ require 'new_relic/rack/developer_mode'
@@ -0,0 +1,82 @@
1
+ # When installed as a plugin this is loaded automatically.
2
+ #
3
+ # When installed as a gem, you need to add
4
+ # require 'new_relic/recipes'
5
+ # to your deploy.rb
6
+ #
7
+ # Defined deploy:notify_rpm which will send information about the deploy to RPM.
8
+ # The task will run on app servers except where no_release is true.
9
+ # If it fails, it will not affect the task execution or do a rollback.
10
+ #
11
+ make_notify_task = Proc.new do
12
+
13
+ namespace :newrelic do
14
+
15
+ # on all deployments, notify RPM
16
+ desc "Record a deployment in New Relic RPM (rpm.newrelic.com)"
17
+ task :notice_deployment, :roles => :app, :except => {:no_release => true } do
18
+ rails_env = fetch(:newrelic_rails_env, fetch(:rails_env, "production"))
19
+ require File.join(File.dirname(__FILE__), 'command.rb')
20
+ begin
21
+ # Try getting the changelog from the server. Then fall back to local changelog
22
+ # if it doesn't work. Problem is that I don't know what directory the .git is
23
+ # in when using git. I could possibly use the remote cache but i don't know
24
+ # if that's always there.
25
+ =begin
26
+ run "cd #{current_release}; #{log_command}" do | io, stream_id, output |
27
+ changelog = output
28
+ end
29
+ =end
30
+ # allow overrides to be defined for revision, description, changelog and appname
31
+ rev = fetch(:newrelic_revision) if exists?(:newrelic_revision)
32
+ description = fetch(:newrelic_desc) if exists?(:newrelic_desc)
33
+ changelog = fetch(:newrelic_changelog) if exists?(:newrelic_changelog)
34
+ appname = fetch(:newrelic_appname) if exists?(:newrelic_appname)
35
+ if !changelog
36
+ logger.debug "Getting log of changes for New Relic Deployment details"
37
+ from_revision = source.next_revision(current_revision)
38
+ if scm == :git
39
+ log_command = "git log --no-color --pretty=format:' * %an: %s' --abbrev-commit --no-merges #{previous_revision}..#{real_revision}"
40
+ else
41
+ log_command = "#{source.log(from_revision)}"
42
+ end
43
+ changelog = `#{log_command}`
44
+ end
45
+ new_revision = rev || source.query_revision(source.head()) do |cmd|
46
+ logger.debug "executing locally: '#{cmd}'"
47
+ `#{cmd}`
48
+ end
49
+ deploy_options = { :environment => rails_env,
50
+ :revision => new_revision,
51
+ :changelog => changelog,
52
+ :description => description,
53
+ :appname => appname }
54
+ logger.debug "Uploading deployment to New Relic"
55
+ deployment = NewRelic::Command::Deployments.new deploy_options
56
+ deployment.run
57
+ logger.info "Uploaded deployment information to New Relic"
58
+ rescue NewRelic::Command::CommandFailure => e
59
+ logger.info e.message
60
+ rescue Capistrano::CommandError
61
+ logger.info "Unable to notify New Relic of the deployment... skipping"
62
+ rescue Exception => e
63
+ logger.info "Error creating New Relic deployment (#{e})\n#{e.backtrace.join("\n")}"
64
+ end
65
+ # WIP: For rollbacks, let's update the deployment we created with an indication of the failure:
66
+ # on_rollback do
67
+ # run(...)
68
+ # end
69
+ end
70
+ end
71
+ end
72
+ require 'capistrano/version'
73
+ if defined?(Capistrano::Version::MAJOR) && Capistrano::Version::MAJOR < 2
74
+ STDERR.puts "Unable to load #{__FILE__}\nNew Relic Capistrano hooks require at least version 2.0.0"
75
+ else
76
+ instance = Capistrano::Configuration.instance
77
+ if instance
78
+ instance.load &make_notify_task
79
+ else
80
+ make_notify_task.call
81
+ end
82
+ end
@@ -0,0 +1,368 @@
1
+
2
+ module NewRelic
3
+ module Stats
4
+
5
+ def absent?
6
+ # guess on absent values
7
+ call_count == 0
8
+ end
9
+
10
+ def time_str(value_ms)
11
+ case
12
+ when value_ms >= 10000
13
+ "%.1f s" % (value_ms / 1000.0)
14
+ when value_ms >= 5000
15
+ "%.2f s" % (value_ms / 1000.0)
16
+ else
17
+ "%.0f ms" % value_ms
18
+ end
19
+ end
20
+
21
+ def average_call_time
22
+ return 0 if call_count == 0
23
+ total_call_time / call_count
24
+ end
25
+ def average_exclusive_time
26
+ return 0 if call_count == 0
27
+ total_exclusive_time / call_count
28
+ end
29
+
30
+ # merge by adding to average response time
31
+ # - used to compose multiple metrics e.g. dispatcher time + mongrel queue time
32
+ def sum_merge! (other_stats)
33
+ Array(other_stats).each do |s|
34
+ self.total_call_time += s.total_call_time
35
+ self.total_exclusive_time += s.total_exclusive_time
36
+ self.min_call_time += s.min_call_time
37
+ self.max_call_time += s.max_call_time
38
+ #self.call_count += s.call_count - do not add call count because we are stacking these times on top of each other
39
+ self.sum_of_squares += s.sum_of_squares if s.sum_of_squares
40
+ self.begin_time = s.begin_time if s.begin_time.to_f < begin_time.to_f || begin_time.to_f == 0.0
41
+ self.end_time = s.end_time if s.end_time.to_f > end_time.to_f
42
+ end
43
+
44
+ self
45
+ end
46
+
47
+ def merge!(other_stats)
48
+ Array(other_stats).each do |s|
49
+ self.total_call_time += s.total_call_time
50
+ self.total_exclusive_time += s.total_exclusive_time
51
+ self.min_call_time = s.min_call_time if (s.min_call_time < min_call_time && s.call_count > 0) || call_count == 0
52
+ self.max_call_time = s.max_call_time if s.max_call_time > max_call_time
53
+ self.call_count += s.call_count
54
+ self.sum_of_squares += s.sum_of_squares if s.sum_of_squares
55
+ self.begin_time = s.begin_time if s.begin_time.to_f < begin_time.to_f || begin_time.to_f == 0.0
56
+ self.end_time = s.end_time if s.end_time.to_f > end_time.to_f
57
+ end
58
+
59
+ self
60
+ end
61
+
62
+ def merge(other_stats)
63
+ stats = self.clone
64
+ stats.merge! other_stats
65
+ end
66
+
67
+ # split into an array of timeslices whose
68
+ # time boundaries start on (begin_time + (n * duration)) and whose
69
+ # end time ends on (begin_time * (n + 1) * duration), except for the
70
+ # first and last elements, whose begin time and end time are the begin
71
+ # and end times of this stats instance, respectively. Yield to caller
72
+ # for the code that creates the actual stats instance
73
+ def split(rollup_begin_time, rollup_period)
74
+ rollup_begin_time = rollup_begin_time.to_f
75
+ rollup_begin_time += ((self.begin_time - rollup_begin_time) / rollup_period).floor * rollup_period
76
+
77
+ current_begin_time = self.begin_time
78
+ current_end_time = rollup_begin_time + rollup_period
79
+
80
+ return [self] if current_end_time >= self.end_time
81
+
82
+ timeslices = []
83
+ while current_end_time < self.end_time do
84
+ ts = yield(current_begin_time, current_end_time)
85
+ if ts
86
+ ts.fraction_of(self)
87
+ timeslices << ts
88
+ end
89
+ current_begin_time = current_end_time
90
+ current_end_time = current_begin_time + rollup_period
91
+ end
92
+
93
+ if self.end_time > current_begin_time
94
+ percentage = rollup_period / self.duration + (self.begin_time - rollup_begin_time) / rollup_period
95
+ ts = yield(current_begin_time, self.end_time)
96
+ if ts
97
+ ts.fraction_of(self)
98
+ timeslices << ts
99
+ end
100
+ end
101
+
102
+ timeslices
103
+ end
104
+
105
+ def is_reset?
106
+ call_count == 0 && total_call_time == 0.0 && total_exclusive_time == 0.0
107
+ end
108
+
109
+ def reset
110
+ self.call_count = 0
111
+ self.total_call_time = 0.0
112
+ self.total_exclusive_time = 0.0
113
+ self.min_call_time = 0.0
114
+ self.max_call_time = 0.0
115
+ self.sum_of_squares = 0.0
116
+ self.begin_time = Time.at(0)
117
+ self.end_time = Time.at(0)
118
+ end
119
+
120
+ def as_percentage_of(other_stats)
121
+ return 0 if other_stats.total_call_time == 0
122
+ return (total_call_time / other_stats.total_call_time) * 100.0
123
+ end
124
+
125
+ # the stat total_call_time is a percent
126
+ def as_percentage
127
+ if call_count.zero?
128
+ 0
129
+ else
130
+ (total_call_time / call_count) * 100.0
131
+ end
132
+ end
133
+
134
+ def duration
135
+ end_time - begin_time
136
+ end
137
+
138
+ def calls_per_minute
139
+ if duration.zero?
140
+ 0
141
+ else
142
+ (call_count / duration.to_f) * 60.0
143
+ end
144
+ end
145
+
146
+ def total_call_time_per_minute
147
+ 60.0 * time_percentage
148
+ end
149
+
150
+ def standard_deviation
151
+ return 0 if call_count < 2 || self.sum_of_squares.nil?
152
+
153
+ # Convert sum of squares into standard deviation based on
154
+ # formula for the standard deviation for the entire population
155
+ x = self.sum_of_squares - (self.call_count * (self.average_value**2))
156
+ return 0 if x <= 0
157
+
158
+ Math.sqrt(x / self.call_count)
159
+ end
160
+
161
+ # returns the time spent in this component as a percentage of the total
162
+ # time window.
163
+ def time_percentage
164
+ return 0 if duration == 0
165
+ total_call_time / duration
166
+ end
167
+
168
+ def exclusive_time_percentage
169
+ return 0 if duration == 0
170
+ total_exclusive_time / duration
171
+ end
172
+
173
+ alias average_value average_call_time
174
+ alias average_response_time average_call_time
175
+ alias requests_per_minute calls_per_minute
176
+
177
+ def to_s
178
+ summary
179
+ end
180
+
181
+ # Summary string to facilitate testing
182
+ def summary
183
+ format = "%m/%d/%y %I:%M%p"
184
+ "[#{Time.at(begin_time).utc.strftime(format)} UTC, #{'%2.3fs' % duration}; #{'%2i' % call_count} calls #{'%4i' % to_ms(average_call_time)} ms]"
185
+ end
186
+
187
+ # round all of the values to n decimal points
188
+ def round!
189
+ self.total_call_time = round_to_3(total_call_time)
190
+ self.total_exclusive_time = round_to_3(total_exclusive_time)
191
+ self.min_call_time = round_to_3(min_call_time)
192
+ self.max_call_time = round_to_3(max_call_time)
193
+ self.sum_of_squares = round_to_3(sum_of_squares)
194
+ self.begin_time = begin_time
195
+ self.end_time = end_time
196
+ end
197
+
198
+ # calculate this set of stats to be a percentage fraction
199
+ # of the provided stats, which has an overlapping time window.
200
+ # used as a key part of the split algorithm
201
+ def fraction_of(s)
202
+ min_end = (end_time < s.end_time ? end_time : s.end_time)
203
+ max_begin = (begin_time > s.begin_time ? begin_time : s.begin_time)
204
+ percentage = (min_end - max_begin) / s.duration
205
+
206
+ self.total_exclusive_time = s.total_exclusive_time * percentage
207
+ self.total_call_time = s.total_call_time * percentage
208
+ self.min_call_time = s.min_call_time
209
+ self.max_call_time = s.max_call_time
210
+ self.call_count = s.call_count * percentage
211
+ self.sum_of_squares = (s.sum_of_squares || 0) * percentage
212
+ end
213
+
214
+ # multiply the total time and rate by the given percentage
215
+ def multiply_by(percentage)
216
+ self.total_call_time = total_call_time * percentage
217
+ self.call_count = call_count * percentage
218
+ self.sum_of_squares = sum_of_squares * percentage
219
+
220
+ self
221
+ end
222
+
223
+
224
+ # returns s,t,f
225
+ def get_apdex
226
+ [@call_count, @total_call_time.to_i, @total_exclusive_time.to_i]
227
+ end
228
+
229
+ def apdex_score
230
+ s, t, f = get_apdex
231
+ (s.to_f + (t.to_f / 2)) / (s+t+f).to_f
232
+ end
233
+
234
+ private
235
+
236
+ def to_ms(number)
237
+ (number*1000).round
238
+ end
239
+
240
+ def round_to_3(val)
241
+ (val * 1000).round / 1000.0
242
+ end
243
+ end
244
+
245
+
246
+ class StatsBase
247
+ include Stats
248
+
249
+ attr_accessor :call_count
250
+ attr_accessor :min_call_time
251
+ attr_accessor :max_call_time
252
+ attr_accessor :total_call_time
253
+ attr_accessor :total_exclusive_time
254
+ attr_accessor :sum_of_squares
255
+
256
+ def initialize
257
+ reset
258
+ end
259
+
260
+ def freeze
261
+ @end_time = Time.now
262
+ super
263
+ end
264
+
265
+ def to_json(*a)
266
+ {'call_count' => call_count,
267
+ 'min_call_time' => min_call_time,
268
+ 'max_call_time' => max_call_time,
269
+ 'total_call_time' => total_call_time,
270
+ 'total_exclusive_time' => total_exclusive_time,
271
+ 'sum_of_squares' => sum_of_squares}.to_json(*a)
272
+ end
273
+
274
+
275
+ # In this class, we explicitly don't track begin and end time here, to save space during
276
+ # cross process serialization via xml. Still the accessor methods must be provided for merge to work.
277
+ def begin_time=(t)
278
+ end
279
+
280
+ def end_time=(t)
281
+ end
282
+
283
+ def begin_time
284
+ 0.0
285
+ end
286
+
287
+ def end_time
288
+ 0.0
289
+ end
290
+ end
291
+
292
+
293
+ class BasicStats < StatsBase
294
+ end
295
+
296
+ class ApdexStats < StatsBase
297
+
298
+ def record_apdex_s
299
+ @call_count += 1
300
+ end
301
+
302
+ def record_apdex_t
303
+ @total_call_time += 1
304
+ end
305
+
306
+ def record_apdex_f
307
+ @total_exclusive_time += 1
308
+ end
309
+ end
310
+
311
+ # Statistics used to track the performance of traced methods
312
+ class MethodTraceStats < StatsBase
313
+
314
+ alias data_point_count call_count
315
+
316
+ # record a single data point into the statistical gatherer. The gatherer
317
+ # will aggregate all data points collected over a specified period and upload
318
+ # its data to the NewRelic server
319
+ def record_data_point(value, exclusive_time = value)
320
+ @call_count += 1
321
+ @total_call_time += value
322
+ @min_call_time = value if value < @min_call_time || @call_count == 1
323
+ @max_call_time = value if value > @max_call_time
324
+ @total_exclusive_time += exclusive_time
325
+
326
+ @sum_of_squares += (value * value)
327
+ self
328
+ end
329
+
330
+ alias trace_call record_data_point
331
+
332
+ def record_multiple_data_points(total_value, count=1)
333
+ return record_data_point(total_value) if count == 1
334
+ @call_count += count
335
+ @total_call_time += total_value
336
+ avg_val = total_value / count
337
+ @min_call_time = avg_val if avg_val < @min_call_time || @call_count == count
338
+ @max_call_time = avg_val if avg_val > @max_call_time
339
+ @total_exclusive_time += total_value
340
+ @sum_of_squares += (avg_val * avg_val) * count
341
+ self
342
+ end
343
+
344
+ def increment_count(value = 1)
345
+ @call_count += value
346
+ end
347
+
348
+ end
349
+
350
+ class ScopedMethodTraceStats < MethodTraceStats
351
+ def initialize(unscoped_stats)
352
+ super()
353
+ @unscoped_stats = unscoped_stats
354
+ end
355
+ def trace_call(call_time, exclusive_time = call_time)
356
+ @unscoped_stats.trace_call call_time, exclusive_time
357
+ super call_time, exclusive_time
358
+ end
359
+ def record_multiple_data_points(total_value, count=1)
360
+ @unscoped_stats.record_multiple_data_points(total_value, count)
361
+ super total_value, count
362
+ end
363
+ def unscoped_stats
364
+ @unscoped_stats
365
+ end
366
+ end
367
+ end
368
+