From 466bf036fdc80b1be518c7cc715f9aeb42c951e1 Mon Sep 17 00:00:00 2001 From: Laurent Lecigne Date: Mon, 5 Jun 2017 18:47:22 +0200 Subject: [PATCH] Timing() time.Duration support --- statsd.go | 5 ++++- statsd_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/statsd.go b/statsd.go index f19204d..4455f42 100644 --- a/statsd.go +++ b/statsd.go @@ -105,6 +105,9 @@ func (c *Client) Timing(bucket string, value interface{}) { if c.skip() { return } + if v, ok := value.(time.Duration); ok { + value = int64(v / time.Millisecond) + } c.conn.metric(c.prefix, bucket, value, "ms", c.rate, c.tags) } @@ -129,7 +132,7 @@ func (c *Client) NewTiming() Timing { // Send sends the time elapsed since the creation of the Timing. func (t Timing) Send(bucket string) { - t.c.Timing(bucket, int(t.Duration()/time.Millisecond)) + t.c.Timing(bucket, t.Duration()) } // Duration returns the time elapsed since the creation of the Timing. diff --git a/statsd_test.go b/statsd_test.go index c97650c..351b65c 100644 --- a/statsd_test.go +++ b/statsd_test.go @@ -43,6 +43,12 @@ func TestTiming(t *testing.T) { }) } +func TestTimingDuration(t *testing.T) { + testOutput(t, "test_key:7|ms", func(c *Client) { + c.Timing(testKey, time.Duration(7*time.Millisecond)) + }) +} + func TestHistogram(t *testing.T) { testOutput(t, "test_key:17|h", func(c *Client) { c.Histogram(testKey, 17)