Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Accumulations consumer records the last time it ran, eliminating extr…

…aneous data points.
  • Loading branch information...
commit da23a70a88fd2dc47182b923ba115d5d1825ae2f 1 parent 267b1c6
@dhruvbansal dhruvbansal authored
Showing with 27 additions and 3 deletions.
  1. +27 −3 lib/graphiterb/monitors/accumulations_consumer.rb
View
30 lib/graphiterb/monitors/accumulations_consumer.rb
@@ -12,6 +12,10 @@ class AccumulationsConsumer < Graphiterb::Monitors::PeriodicMonitor
# accumulations are stored (defaults to 'graphiterb')
attr_accessor :namespace
+ # The key used to store the last report time. Will live inside
+ # the namespace.
+ attr_accessor :report_timestamp_key
+
# The Redis namespace used for the accumulators.
attr_accessor :accumulators
@@ -44,18 +48,38 @@ def initialize options={}
@namespace = options[:namespace] || 'graphiterb'
@accumulators = Redis::Namespace.new(namespace, :redis => @redis)
@regexp = options[:regexp] || /.*/
+ @report_timestamp_key = options[:report_timestamp_key] || '_last_report_timestamp_'
super('fake_scope', options) # FIXME shouldn't have to use a fake scope
end
# Uses Redis' +getset+ call to retrieve total accumulated counts
# from Redis and reset them to 0 atomically.
def get_metrics metrics, since
+ # compute the length of the interval between the last time
+ # this monitor ran and now
+ last_report_timestamp = accumulators.get(report_timestamp_key).to_i
+ now = Time.now.to_i
+ interval = now - last_report_timestamp
+
accumulators.keys.each do |target|
- next unless regexp =~ target
- current_count = accumulators.getset(target, 0) rescue 0.0
- rate = current_count.to_f / since.to_f rescue 0.0
+ next unless regexp && regexp =~ target
+ current_count = accumulators.getset(target, 0) rescue 0.0
+
+ # if we don't know when the last time we ran was, we bail,
+ # resetting the accumulated count to 0 in preparation for
+ # the next iteration
+ #
+ # we lost a data point this way (say the monitor went down
+ # for a while and we brought it back up) but we also don't
+ # ruin the scale of the graph...
+ next if last_report_timestamp == 0
+
+ rate = current_count.to_f / interval.to_f rescue 0.0
metrics << [target, rate] # no need to scope as targets are pre-scoped
end
+
+ # store the timestamp for this run for the next
+ accumulators.set(report_timestamp_key, now)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.