Permalink
Browse files

Meter rates return gauges so they can be logged

  • Loading branch information...
twinturbo
twinturbo committed Apr 15, 2012
1 parent 7ab5044 commit a21a3dda81be4657dcd639d04c4d338fa770148e
Showing with 39 additions and 4 deletions.
  1. +22 −1 lib/harness/meter.rb
  2. +17 −3 test/integration/meter_test.rb
  3. 0 test/unit/meter_test.rb
View
@@ -19,7 +19,18 @@ def per_hour
end
def per(rate, base = Time.now)
- redis.zcount(key, base.to_f - rate, base.to_f)
+ gauge = Gauge.new :value => redis.zcount(key, base.to_f - rate, base.to_f)
+
+ if words = rate_in_words(rate)
+ gauge.name = "#{@name} per #{words}"
+ gauge.id = "#{@name}-per-#{words}"
+ else
+ gauge.id = "#{@name} gauge"
+ end
+
+ gauge.time = Time.now
+
+ gauge
end
private
@@ -30,5 +41,15 @@ def key
def redis
Harness.redis
end
+
+ def rate_in_words(rate)
+ if rate < 1.minute
+ "second"
+ elsif rate >= 1.minute && rate < 1.hour
+ "minute"
+ elsif rate >= 1.hour && rate < 1.day
+ "hour"
+ end
+ end
end
end
@@ -5,14 +5,28 @@ def test_counters_can_act_like_gauges
50.times { instrument "event-counter", :counter => true }
meter = Harness::Meter.new 'event-counter'
- assert_equal 50, meter.per_second
- assert_equal 50, meter.per_minute
- assert_equal 50, meter.per_hour
+ assert_equal 50, meter.per_second.value
+ assert_equal 50, meter.per_minute.value
+ assert_equal 50, meter.per_hour.value
end
def tests_raises_an_error_when_no_such_counter
assert_raises Harness::NoCounter do
Harness::Meter.new 'unknown-counter'
end
end
+
+ def test_rates_return_gauges
+ 50.times { instrument "event-counter", :counter => true }
+
+ meter = Harness::Meter.new 'event-counter'
+
+ gauge = meter.per_second
+
+ assert_kind_of Harness::Gauge, gauge
+
+ assert_equal "event-counter-per-second", gauge.id
+ assert_equal "event-counter per second", gauge.name
+ assert_kind_of Time, gauge.time
+ end
end
View
No changes.

0 comments on commit a21a3dd

Please sign in to comment.