Created
December 8, 2009 19:33
-
-
Save eric/251915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Would be nice to be part of scout internals | |
| def counter(name, value, options = {}) | |
| current_time = Time.now | |
| if data = memory(name) | |
| last_time, last_value = data[:time], data[:value] | |
| elapsed_seconds = current_time - last_time | |
| # We won't log it if the value has wrapped or enough time hasn't | |
| # elapsed | |
| if value >= last_value && elapsed_seconds >= 1 | |
| result = value - last_value | |
| case options[:per] | |
| when :second, 'second' | |
| result = result / elapsed_seconds.to_f | |
| when :minute, 'minute' | |
| result = result / elapsed_seconds.to_f / 60.0 | |
| end | |
| result = result.to_i if options[:round] | |
| report(name => result) | |
| end | |
| end | |
| remember(name => { :time => current_time, :value => value }) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment