Skip to content

Commit

Permalink
support floats for timer update values
Browse files Browse the repository at this point in the history
  • Loading branch information
fetep committed Feb 25, 2013
1 parent d1f4da9 commit 984ef65
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/statsdserver/proto/v1.rb
Expand Up @@ -25,15 +25,15 @@ def self.parse_update(update, stats)
if fields[1] == "ms" or fields[1] == "t" # timer update
if fields[0].index(",")
fields[0].split(",").each do |value_str|
value = Integer(value_str) rescue nil
value = Float(value_str) rescue nil
stats.timers[key] << value if value
end
else
value = Integer(fields[0]) rescue nil
value = Float(fields[0]) rescue nil
if value.nil?
raise ParseError, "invalid timer value for #{key}: #{fields[0]}"
end
stats.timers[key] << fields[0].to_i
stats.timers[key] << value
end

elsif fields[1] == "c" # counter update
Expand Down
18 changes: 14 additions & 4 deletions spec/statsdserver/proto/v1_spec.rb
Expand Up @@ -44,21 +44,31 @@
it "should handle timers with type ms" do
update = "test.timer:100|ms"
StatsdServer::Proto::V1.parse_update(update, @stats)
@stats.timers["test.timer"].should eq([100])
@stats.timers["test.timer"].should eq([100.0])

update = "test.timer:200|ms"
StatsdServer::Proto::V1.parse_update(update, @stats)
@stats.timers["test.timer"].should eq([100, 200])
@stats.timers["test.timer"].should eq([100.0, 200.0])
end

it "should handle timers with type t" do
update = "test.timer:100|t"
StatsdServer::Proto::V1.parse_update(update, @stats)
@stats.timers["test.timer"].should eq([100])
@stats.timers["test.timer"].should eq([100.0])

update = "test.timer:200|t"
StatsdServer::Proto::V1.parse_update(update, @stats)
@stats.timers["test.timer"].should eq([100, 200])
@stats.timers["test.timer"].should eq([100.0, 200.0])
end

it "should handle timers that are floats" do
update = "test.timer:3.14159|t"
StatsdServer::Proto::V1.parse_update(update, @stats)
@stats.timers["test.timer"].should eq([3.14159])

update = "test.multitimer:1,2,3.14159|t"
StatsdServer::Proto::V1.parse_update(update, @stats)
@stats.timers["test.multitimer"].should eq([1.0, 2.0, 3.14159])
end

it "should handle timers with invalid updates" do
Expand Down
15 changes: 15 additions & 0 deletions spec/statsdserver_spec.rb
Expand Up @@ -80,5 +80,20 @@
result["stats.timers.test.timer.upper_90"].should eq("9")
result["stats.timers.test.timer.count"].should eq("10")
end

it "should calculate statistics for timers with float values" do
s = StatsdServer.new({}, {}, {})
1.upto(10) { |i| s.stats.timers["test.timer"] << (i.to_f / 2) }
result = {}
s.carbon_update_str.split("\n").each do |line|
metric, value, _unused = line.split(" ")
result[metric] = value
end
result["stats.timers.test.timer.lower"].should eq("0.5")
result["stats.timers.test.timer.mean"].should eq("2.5")
result["stats.timers.test.timer.upper"].should eq("5.0")
result["stats.timers.test.timer.upper_90"].should eq("4.5")
result["stats.timers.test.timer.count"].should eq("10")
end
end # describe carbon_update_str
end

0 comments on commit 984ef65

Please sign in to comment.