Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add #counter method for plugins.

  • Loading branch information...
commit 9e6647539fd30c41c791b03320c902990ebd0287 1 parent 9eb0f82
Eric Lindvall eric authored

Showing 1 changed file with 46 additions and 0 deletions. Show diff stats Hide diff stats

  1. +46 0 lib/scout/plugin.rb
46 lib/scout/plugin.rb
@@ -156,6 +156,52 @@ def remember(*args)
156 156 end
157 157
158 158 #
  159 + # Usage:
  160 + #
  161 + # counter(:rkbps, stats['rsect'] / 2, :per => :second)
  162 + # counter(:rpm, request_counter, :per => :minute)
  163 + # counter(:swap_ins, vmstat['pswpin'], :per => :second, :round => true)
  164 + #
  165 + def counter(name, value, options = {}, &block)
  166 + current_time = Time.now
  167 +
  168 + if data = memory("_counter_#{name}")
  169 + last_time, last_value = data[:time], data[:value]
  170 + elapsed_seconds = current_time - last_time
  171 +
  172 + # We won't log it if the value has wrapped or enough time hasn't
  173 + # elapsed
  174 + if value >= last_value && elapsed_seconds >= 1
  175 + if block
  176 + result = block.call(last_value, value)
  177 + else
  178 + result = value - last_value
  179 + end
  180 +
  181 + case options[:per]
  182 + when :second, 'second'
  183 + result = result / elapsed_seconds.to_f
  184 + when :minute, 'minute'
  185 + result = result / elapsed_seconds.to_f / 60.0
  186 + else
  187 + raise "Unknown option for ':per': #{options[:per].inspect}"
  188 + end
  189 +
  190 + if options[:round]
  191 + # Backward compatibility
  192 + options[:round] = 1 if options[:round] == true
  193 +
  194 + result = (result * (10 ** options[:round])).round / (10 ** options[:round]).to_f
  195 + end
  196 +
  197 + report(name => result)
  198 + end
  199 + end
  200 +
  201 + remember("_counter_#{name}" => { :time => current_time, :value => value })
  202 + end
  203 +
  204 + #
159 205 # Old plugins will work because they override this method. New plugins can
160 206 # now leave this method in place, add a build_report() method instead, and
161 207 # use the new helper methods to build up content inside which will

0 comments on commit 9e66475

Please sign in to comment.
Something went wrong with that request. Please try again.