Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Allow floating point times #10

Merged
merged 3 commits into from
Jul 30, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Send floating point milliseconds in time block
To match what the available back-ends support.
  • Loading branch information
zerowidth committed Jul 30, 2014
commit a3746aac7364d87775babe4d407e7f080fcb446b
2 changes: 1 addition & 1 deletion lib/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def timing(stat, ms, sample_rate=1); send stat, ms, 'ms', sample_rate end
def time(stat, sample_rate=1)
start = Time.now
result = yield
timing(stat, ((Time.now - start) * 1000).round, sample_rate)
timing(stat, (Time.now - start) * 1000, sample_rate)
result
end

Expand Down
13 changes: 11 additions & 2 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def socket; @socket ||= FakeUDPSocket.new end
describe "#time" do
it "should format the message according to the statsd spec" do
@statsd.time('foobar') { sleep(0.001); 'test' }
@statsd.socket.recv.must_equal ['foobar:1|ms']
data = @statsd.socket.recv
key, value, unit = data.first.split(/[:|]/)
key.must_equal "foobar"
value.must_match /^\d\.\d{3}$/
unit.must_equal "ms"
end

it "should return the result of the block" do
Expand All @@ -84,7 +88,12 @@ def socket; @socket ||= FakeUDPSocket.new end

it "should format the message according to the statsd spec" do
result = @statsd.time('foobar', 0.5) { sleep(0.001); 'test' }
@statsd.socket.recv.must_equal ['foobar:1|ms|@0.5']
data = @statsd.socket.recv
key, value, unit, frequency = data.first.split(/[:|]/)
key.must_equal "foobar"
value.must_match /^\d\.\d{3}$/
unit.must_equal "ms"
frequency.must_equal "@0.5"
end
end
end
Expand Down