@@ -10,6 +10,7 @@ import (
1010// DataDogClient is a dogstatsd metrics client implementation.
1111type 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 {
3748func (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.
4961func (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.
6476func (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.
7890func (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.
8395func (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}
0 commit comments