Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Time function to report time a passed function takes #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions statsd/noop.go
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
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
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
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)
}