Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions statsd/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func (n *NoOpClient) Set(name string, value string, tags []string, rate float64)
return nil
}

// Time executes the passed function and returns nil
func (n *NoOpClient) Time(name string, tags []string, rate float64, fn func()) error {
fn()
return nil
}

// Timing does nothing and returns nil
func (n *NoOpClient) Timing(name string, value time.Duration, tags []string, rate float64) error {
return nil
Expand Down
1 change: 1 addition & 0 deletions statsd/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestNoOpClient(t *testing.T) {
a.Nil(c.Decr("asd", tags, 56.0))
a.Nil(c.Incr("asd", tags, 56.0))
a.Nil(c.Set("asd", "asd", tags, 56.0))
a.Nil(c.Time("asd", tags, 56.0, func() {}))
a.Nil(c.Timing("asd", time.Second, tags, 56.0))
a.Nil(c.TimeInMilliseconds("asd", 1234.5, tags, 56.0))
a.Nil(c.Event(nil))
Expand Down
11 changes: 11 additions & 0 deletions statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ type ClientInterface interface {
// Set counts the number of unique elements in a group.
Set(name string, value string, tags []string, rate float64) error

// Time measures and sends timing information
Time(name string, tags []string, rate float64, fn func()) error

// Timing sends timing information, it is an alias for TimeInMilliseconds
Timing(name string, value time.Duration, tags []string, rate float64) error

Expand Down Expand Up @@ -544,6 +547,14 @@ func (c *Client) Set(name string, value string, tags []string, rate float64) err
return c.send(metric{metricType: set, name: name, svalue: value, tags: tags, rate: rate})
}

// Time measures and sends timing information
func (c *Client) Time(name string, tags []string, rate float64, fn func()) error {
start := time.Now()
fn()
duration := time.Since(start)
return c.Timing(name, duration, tags, rate)
}

// Timing sends timing information, it is an alias for TimeInMilliseconds
func (c *Client) Timing(name string, value time.Duration, tags []string, rate float64) error {
return c.TimeInMilliseconds(name, value.Seconds()*1000, tags, rate)
Expand Down
12 changes: 11 additions & 1 deletion statsd/statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func testTelemetry(t *testing.T, client *Client, expectedTelemetryTags []string)
client.Decr("Decr", nil, 1)
client.Incr("Incr", nil, 1)
client.Set("Set", "value", nil, 1)
client.Time("Time", nil, 1, func() {})
client.Timing("Timing", 21, nil, 1)
client.TimeInMilliseconds("TimeInMilliseconds", 21, nil, 1)
client.SimpleEvent("hello", "world")
Expand All @@ -54,7 +55,7 @@ func testTelemetry(t *testing.T, client *Client, expectedTelemetryTags []string)
metrics := client.flushTelemetry()

expectedMetricsName := map[string]int64{
"datadog.dogstatsd.client.metrics": 9,
"datadog.dogstatsd.client.metrics": 10,
"datadog.dogstatsd.client.events": 1,
"datadog.dogstatsd.client.service_checks": 1,
"datadog.dogstatsd.client.metric_dropped_on_receive": 0,
Expand Down Expand Up @@ -179,3 +180,12 @@ func TestCloneWithExtraOptions(t *testing.T) {
assert.Equal(t, cloneClient.addrOption, addr)
assert.Len(t, cloneClient.options, 3)
}

func testTime(t *testing.T, client *Client, expectedTelemetryTags []string) {
executed := 0
fn := func() {
executed = executed + 1
}
client.Time("Time", nil, 1, fn)
assert.Equal(t, executed, 1)
}