@@ -15,19 +15,29 @@ type withRater interface {
1515}
1616
1717func ExampleDataDogClient () {
18+ // Create a new DataDog metrics client.
1819 datadog := metrics .NewDataDogClient ("127.0.0.1:8125" , "myprefix" )
1920 datadog .WithTags (map [string ]string {
2021 "tag" : "value" ,
2122 }).Incr ("requests.count" )
23+
24+ // Create a DataDog metrics client with a custom configured statsd.
25+ client , err := statsd .New ("127.0.0.1:8125" , statsd .WithNamespace ("myprefix" ))
26+ if err != nil {
27+ panic (err )
28+ }
29+ custom := metrics .NewDataDogClient ("" , "" , metrics .WithStatsd (client ))
30+ custom .WithTags (map [string ]string {
31+ "tag" : "value" ,
32+ }).Incr ("requests.count" )
2233}
2334
2435func TestDataDogClient (t * testing.T ) {
2536 // This connects to an address that's probably not running anything. The
2637 // stats essentially go into `/dev/null`. Right now the only thing this
2738 // ensures is that the functions can be called without crashing.
2839 // TODO: In the future, we should use a statsd mock here.
29- var datadog metrics.Client
30- datadog = metrics .NewDataDogClient ("127.0.0.1:8126" , "testing" , metrics .WithoutTelemetry ())
40+ var datadog metrics.Client = metrics .NewDataDogClient ("127.0.0.1:8126" , "testing" , metrics .WithoutTelemetry ())
3141
3242 datadog .Incr ("one" )
3343 datadog .Event (statsd .NewEvent ("title" , "desc" ))
@@ -85,37 +95,67 @@ func TestDataDogClient(t *testing.T) {
8595 datadog .Close ()
8696}
8797
98+ func TestDataDogCustom (t * testing.T ) {
99+ client , err := statsd .New ("127.0.0.1:8125" , statsd .WithNamespace ("myprefix" ))
100+ if err != nil {
101+ panic (err )
102+ }
103+ // This should pass without error even though no address is passed in because
104+ // the client we created above does have an address.
105+ custom := metrics .NewDataDogClient ("" , "" , metrics .WithStatsd (client ))
106+ custom .WithTags (map [string ]string {
107+ "tag" : "value" ,
108+ }).Incr ("requests.count" )
109+ custom .Close ()
110+ }
111+
88112func Benchmark_0Tags_100Emits (b * testing.B ) {
89- benchmarkClient (b , 0 , 100 , false )
113+ benchmarkClient (b , 0 , 100 , true , false , nil )
90114}
91115
92116func BenchmarkTags_5Tags_100Emits (b * testing.B ) {
93- benchmarkClient (b , 5 , 100 , false )
117+ benchmarkClient (b , 5 , 100 , true , false , nil )
94118}
95119
96120func BenchmarkTags_5Tags_100Emits_WithInline (b * testing.B ) {
97- benchmarkClient (b , 5 , 100 , true )
121+ benchmarkClient (b , 5 , 100 , true , true , nil )
98122}
99123
100124func BenchmarkTags_10Tags_1000Emits (b * testing.B ) {
101- benchmarkClient (b , 10 , 1000 , false )
125+ benchmarkClient (b , 10 , 1000 , true , false , nil )
102126}
103127
104128func BenchmarkTags_10Tags_1000Emits_WithInline (b * testing.B ) {
105- benchmarkClient (b , 10 , 1000 , true )
129+ benchmarkClient (b , 10 , 1000 , true , true , nil )
106130}
107131
108132func BenchmarkTags_15Tags_100Emits (b * testing.B ) {
109- benchmarkClient (b , 15 , 100 , false )
133+ benchmarkClient (b , 15 , 100 , true , false , nil )
110134}
111135
112136func BenchmarkTags_15Tags_100Emits_WithInline (b * testing.B ) {
113- benchmarkClient (b , 15 , 100 , true )
137+ benchmarkClient (b , 15 , 100 , true , true , nil )
138+ }
139+
140+ func BenchmarkTags_15Tags_1000Emits_Incr (b * testing.B ) {
141+ benchmarkClient (b , 15 , 1000 , false , false , nil )
142+ }
143+
144+ func BenchmarkTags_15Tags_1000Emits_Incr_WithInline (b * testing.B ) {
145+ benchmarkClient (b , 15 , 1000 , false , true , nil )
146+ }
147+
148+ func BenchmarkTags_15Tags_1000Emits_Incr_NoAggr (b * testing.B ) {
149+ client , _ := statsd .New ("127.0.0.1:8126" , statsd .WithoutClientSideAggregation ())
150+ benchmarkClient (b , 15 , 1000 , false , false , client )
114151}
115152
116- func benchmarkClient (b * testing.B , numTags , numMetrics int , inlineTags bool ) {
117- var datadog metrics.Client
118- datadog = metrics .NewDataDogClient ("127.0.0.1:8126" , "testing" )
153+ func benchmarkClient (b * testing.B , numTags , numMetrics int , histo bool , inlineTags bool , client * statsd.Client ) {
154+ options := []metrics.Option {}
155+ if client != nil {
156+ options = append (options , metrics .WithStatsd (client ))
157+ }
158+ var datadog metrics.Client = metrics .NewDataDogClient ("127.0.0.1:8126" , "testing" , options ... )
119159 defer datadog .Close ()
120160
121161 tags := map [string ]string {}
@@ -128,10 +168,18 @@ func benchmarkClient(b *testing.B, numTags, numMetrics int, inlineTags bool) {
128168 for i := 0 ; i < b .N ; i ++ {
129169 cli := datadog .WithTags (tags )
130170 for m := 0 ; m < numMetrics ; m ++ {
131- if inlineTags {
132- cli .WithTags (map [string ]string {"a" : "b" }).Histogram ("histo" , 123 )
171+ if histo {
172+ if inlineTags {
173+ cli .WithTags (map [string ]string {"a" : "b" }).Histogram ("histo" , 123 )
174+ } else {
175+ cli .Histogram ("histo" , 123 )
176+ }
133177 } else {
134- cli .Histogram ("histo" , 123 )
178+ if inlineTags {
179+ cli .WithTags (map [string ]string {"a" : "b" }).Incr ("incr" )
180+ } else {
181+ cli .Incr ("incr" )
182+ }
135183 }
136184 }
137185 }
0 commit comments