-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New metric types: accumulator and delta
* A little bit more debugging info
- Loading branch information
Showing
10 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Salus | ||
class Accumulator < Metric | ||
STORAGE_DEPTH = 1 | ||
|
||
def push(opts={}, &block) | ||
opts = {} unless opts.is_a?(Hash) | ||
|
||
synchronize do | ||
opts.each do |k, v| | ||
validate(k, v) | ||
@opts[k] = v unless [:value, :ttl, :timestamp].include?(k) | ||
end | ||
|
||
if block_given? | ||
v = begin | ||
yield | ||
rescue Exception => e | ||
log DEBUG, e | ||
nil | ||
end | ||
validate(:value, v) | ||
opts[:value] = v | ||
end | ||
|
||
prev = @values.empty? ? 0 : (@values.last.value || 0) | ||
curr = opts[:value] || 0 | ||
|
||
@values << Value.new(prev+curr, opts[:timestamp] || Time.now.to_f, opts[:ttl] || @opts[:ttl]) | ||
@needs_update = true | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Salus | ||
class Delta < Metric | ||
def initialize(defaults={}) | ||
super(defaults) | ||
option :minimum, Numeric | ||
validate(:minimum, @opts[:minimum]) if @opts.key?(:minimum) | ||
end | ||
|
||
protected | ||
def calc | ||
super | ||
@last_calced_value = nil | ||
|
||
if @values.length < STORAGE_DEPTH | ||
return | ||
elsif @values[0].expired?(@values[1].timestamp) | ||
return | ||
elsif !@values[0].value.is_a?(Numeric) | ||
return | ||
elsif !@values[1].value.is_a?(Numeric) | ||
return | ||
end | ||
|
||
@last_calced_value = begin | ||
dt = (@values[1].timestamp - @values[0].timestamp) | ||
dv = (@values[1].value - @values[0].value) | ||
r = (dt == 0) ? nil : dv | ||
if @opts.key?(:minimum) && !r.nil? && r < @opts[:minimum] | ||
nil | ||
else | ||
r | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Salus | ||
VERSION = "0.1.3" | ||
VERSION = "0.2.0" | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe Salus::Accumulator do | ||
context "without ttl" do | ||
let(:metric) { Salus::Accumulator.new } | ||
|
||
it "accumulates values as pushed" do | ||
metric.push value: 10.0 | ||
expect(metric.value).to eq(10.0) | ||
metric.push value: 20.0 | ||
expect(metric.value).to eq(30.0) | ||
metric.push value: 30.0 | ||
expect(metric.value).to eq(60.0) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe Salus::Delta do | ||
context "without ttl" do | ||
let(:metric) { Salus::Delta.new } | ||
|
||
it "validates types" do | ||
expect{metric.push minimum: "just a test"}.to raise_error(ArgumentError) | ||
end | ||
|
||
it "counts deltas" do | ||
metric.push value: 10, timestamp: 0 | ||
expect(metric.value).to eq(nil) | ||
metric.push value: 20, timestamp: 10 | ||
expect(metric.value).to eq(10) | ||
metric.push value: 100, timestamp: 20 | ||
expect(metric.value).to eq(80) | ||
end | ||
|
||
it "returns nil rate for dt == 0" do | ||
metric.push value: 10, timestamp: 10 | ||
metric.push value: 20, timestamp: 10 | ||
expect(metric.value).to eq(nil) | ||
end | ||
|
||
it "returns nil if one value is nil" do | ||
metric.push value: 10, timestamp: 0 | ||
metric.push value: nil, timestamp: 10 | ||
expect(metric.value).to eq(nil) | ||
end | ||
|
||
it "returns nil if delta is less than minimum" do | ||
metric.push value: 200, timestamp: 0, minimum: 0 | ||
metric.push value: 1000, timestamp: 10 | ||
expect(metric.value).to eq(800) | ||
metric.push value: 1, timestamp: 20 | ||
expect(metric.value).to eq(nil) | ||
end | ||
end | ||
|
||
context "with ttl" do | ||
let(:metric) { Salus::Delta.new } | ||
|
||
it "returns nil if previous value is expired" do | ||
metric.push value: 0, timestamp: 0, ttl: 10 | ||
metric.push value: 5, timestamp: 5, ttl: 10 | ||
expect(metric.value).to eq(5) | ||
metric.push value: 100, timestamp: 100, ttl: 10 | ||
expect(metric.value).to eq(nil) | ||
end | ||
end | ||
end |
This file contains 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