Skip to content

Commit faa7a02

Browse files
Merge branch 'master' into event-tags
2 parents 8c0684d + 5238649 commit faa7a02

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
- Add unreleased items here.
10+
11+
## [1.2.0] - 2018-03-01
12+
913
- Update build to test with Go 1.9, drop support for 1.7 since `dep` now
1014
requires Go 1.8+. Go 1.7 users can still use this library but must manage
1115
their own dependencies.
16+
- Add `WithRate(rate float)` to the DataDog client to limit traffic sent to
17+
the `dogstatsd` daemon. The set rate will be applied to all calls made
18+
with the returned `Client`.
1219
- Automatically assign tags to events.
1320

1421
## [1.1.0] - 2017-08-01

metrics/datadog.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// DataDogClient is a dogstatsd metrics client implementation.
1111
type DataDogClient struct {
1212
client *statsd.Client
13+
rate float64
1314
tagMap map[string]string
1415
}
1516

@@ -29,6 +30,16 @@ func NewDataDogClient(address string, namespace string) *DataDogClient {
2930

3031
return &DataDogClient{
3132
client: c,
33+
rate: 1.0,
34+
}
35+
}
36+
37+
// WithRate clones this client with a new sample rate.
38+
func (c *DataDogClient) WithRate(rate float64) Client {
39+
return &DataDogClient{
40+
client: c.client,
41+
rate: rate,
42+
tagMap: combine(c.tagMap, map[string]string{}),
3243
}
3344
}
3445

@@ -37,6 +48,7 @@ func NewDataDogClient(address string, namespace string) *DataDogClient {
3748
func (c *DataDogClient) WithTags(tags map[string]string) Client {
3849
return &DataDogClient{
3950
client: c.client,
51+
rate: c.rate,
4052
tagMap: combine(c.tagMap, tags),
4153
}
4254
}
@@ -47,7 +59,7 @@ func (c *DataDogClient) tagsList() []string {
4759

4860
// Count adds some integer value to a metric.
4961
func (c *DataDogClient) Count(name string, value int64) {
50-
c.client.Count(name, value, c.tagsList(), 1.0)
62+
c.client.Count(name, value, c.tagsList(), c.rate)
5163
}
5264

5365
// Incr adds one to a metric.
@@ -62,7 +74,7 @@ func (c *DataDogClient) Decr(name string) {
6274

6375
// Gauge sets a numeric value.
6476
func (c *DataDogClient) Gauge(name string, value float64) {
65-
c.client.Gauge(name, value, c.tagsList(), 1.0)
77+
c.client.Gauge(name, value, c.tagsList(), c.rate)
6678
}
6779

6880
// Event tracks an event that may be relevant to other metrics.
@@ -76,10 +88,10 @@ func (c *DataDogClient) Event(e *statsd.Event) {
7688

7789
// Timing tracks a duration.
7890
func (c *DataDogClient) Timing(name string, value time.Duration) {
79-
c.client.Timing(name, value, c.tagsList(), 1)
91+
c.client.Timing(name, value, c.tagsList(), c.rate)
8092
}
8193

8294
// Histogram sets a numeric value while tracking min/max/avg/p95/etc.
8395
func (c *DataDogClient) Histogram(name string, value float64) {
84-
c.client.Histogram(name, value, c.tagsList(), 1.0)
96+
c.client.Histogram(name, value, c.tagsList(), c.rate)
8597
}

metrics/datadog_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"github.com/istreamlabs/go-metrics/metrics"
1010
)
1111

12+
type withRater interface {
13+
WithRate(rate float64) metrics.Client
14+
}
15+
1216
func ExampleDataDogClient() {
1317
datadog := metrics.NewDataDogClient("127.0.0.1:8125", "myprefix")
1418
datadog.WithTags(map[string]string{
@@ -36,6 +40,13 @@ func TestDataDogClient(t *testing.T) {
3640
datadog.Gauge("memory", 1024)
3741
datadog.Histogram("histo", 123)
3842

43+
if rater, ok := datadog.(withRater); ok {
44+
ratedClient := rater.WithRate(0.5)
45+
ratedClient.Incr("rated")
46+
} else {
47+
t.Fatalf("Expected DataDog client to support sample rate")
48+
}
49+
3950
// Test that tag overrides work.
4051
override := datadog.WithTags(map[string]string{
4152
"tag1": "value1",

0 commit comments

Comments
 (0)