-
Notifications
You must be signed in to change notification settings - Fork 6
/
datadog_test.go
172 lines (132 loc) · 3.16 KB
/
datadog_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package datadog
import (
"bufio"
"bytes"
"net"
"testing"
"time"
"github.com/bmizerany/assert"
)
func init() {
go func() {
a, err := net.ResolveUDPAddr("udp", ":5000")
check(err)
c, err := net.ListenUDP("udp", a)
check(err)
buf := make([]byte, 1024)
for {
_, _, err := c.ReadFromUDP(buf)
check(err)
}
}()
}
func TestWrites(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.Gauge("memory", 512)
client.Incr("count")
client.Decr("count")
client.Flush()
assert.Equal(t, "\n"+buf.String(), `
memory:512|g
count:1|c
count:-1|c`)
}
func TestWritesWithTags(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.Gauge("memory", 512, "tag:a")
client.Incr("count", "tag:b")
client.Decr("count", "tag:c")
client.Flush()
assert.Equal(t, "\n"+buf.String(), `
memory:512|g|#tag:a
count:1|c|#tag:b
count:-1|c|#tag:c`)
}
func TestWritesGlobalTags(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.SetTags("global:tag")
client.Gauge("memory", 512, "tag:a")
client.Incr("count", "tag:b")
client.Decr("count", "tag:c")
client.Flush()
assert.Equal(t, "\n"+buf.String(), `
memory:512|g|#global:tag,tag:a
count:1|c|#global:tag,tag:b
count:-1|c|#global:tag,tag:c`)
}
func TestPrefix(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.SetPrefix("myprogram")
client.Gauge("memory", 512)
client.Incr("count")
client.Decr("count")
client.Flush()
assert.Equal(t, "\n"+buf.String(), `
myprogram.memory:512|g
myprogram.count:1|c
myprogram.count:-1|c`)
}
func TestHistogram(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.Histogram("size", 512)
client.Flush()
assert.Equal(t, buf.String(), `size:512|h`)
}
func TestHistogramFloat(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.Histogram("size", 0.5)
client.Flush()
assert.Equal(t, buf.String(), `size:0.5|h`)
}
func TestDuration(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.Duration("duration", time.Millisecond*500)
client.Flush()
assert.Equal(t, buf.String(), `duration:0.5|h`)
}
func TestUnique(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.Unique("users", "68b260d7c0")
client.Flush()
assert.Equal(t, buf.String(), `users:68b260d7c0|s`)
}
func TestRate(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.rand = func() float64 { return .4 }
client.SetPrefix("program")
client.SetTags("env:stage")
client.Increment("count", 1, 0.5)
client.Flush()
assert.Equal(t, buf.String(), `program.count:1|c|@0.5|#env:stage`)
}
func TestRateIgnored(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.rand = func() float64 { return .6 }
client.Increment("count", 1, 0.5)
client.Flush()
assert.Equal(t, buf.String(), ``)
}
func TestFlushWrite(t *testing.T) {
buf := new(bytes.Buffer)
client := New(buf)
client.buf = bufio.NewWriterSize(buf, 20)
client.SetPrefix("program")
client.SetTags("env:foo")
client.Incr("counter-a")
assert.Equal(t, buf.String(), `program.counter-a:1|c|#env:foo`)
}
func TestDialClose(t *testing.T) {
c, err := Dial(":5000")
assert.Equal(t, err, nil)
assert.Equal(t, c.Close(), nil)
}