Skip to content

Commit

Permalink
add Time function to report time a passed function takes
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed May 24, 2020
1 parent ee4b28b commit 1b9e703
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
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
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)
}

0 comments on commit 1b9e703

Please sign in to comment.