forked from stripe/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflusher_test.go
191 lines (157 loc) · 5.32 KB
/
flusher_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package veneur
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
lightstep "github.com/lightstep/lightstep-tracer-go"
"github.com/stretchr/testify/assert"
"github.com/stripe/veneur/samplers"
)
func TestServerTags(t *testing.T) {
metrics := []samplers.DDMetric{{
Name: "foo.bar.baz",
Value: [1][2]float64{{float64(time.Now().Unix()), float64(1.0)}},
Tags: []string{"gorch:frobble", "x:e"},
MetricType: "rate",
Interval: 10,
}}
finalizeMetrics("somehostname", []string{"a:b", "c:d"}, metrics)
assert.Equal(t, "somehostname", metrics[0].Hostname, "Metric hostname uses argument")
assert.Contains(t, metrics[0].Tags, "a:b", "Tags should contain server tags")
}
func TestHostMagicTag(t *testing.T) {
metrics := []samplers.DDMetric{{
Name: "foo.bar.baz",
Value: [1][2]float64{{float64(time.Now().Unix()), float64(1.0)}},
Tags: []string{"gorch:frobble", "host:abc123", "x:e"},
MetricType: "rate",
Interval: 10,
}}
finalizeMetrics("badhostname", []string{"a:b", "c:d"}, metrics)
assert.Equal(t, "abc123", metrics[0].Hostname, "Metric hostname should be from tag")
assert.NotContains(t, metrics[0].Tags, "host:abc123", "Host tag should be removed")
assert.Contains(t, metrics[0].Tags, "x:e", "Last tag is still around")
}
func TestDeviceMagicTag(t *testing.T) {
metrics := []samplers.DDMetric{{
Name: "foo.bar.baz",
Value: [1][2]float64{{float64(time.Now().Unix()), float64(1.0)}},
Tags: []string{"gorch:frobble", "device:abc123", "x:e"},
MetricType: "rate",
Interval: 10,
}}
finalizeMetrics("badhostname", []string{"a:b", "c:d"}, metrics)
assert.Equal(t, "abc123", metrics[0].DeviceName, "Metric devicename should be from tag")
assert.NotContains(t, metrics[0].Tags, "device:abc123", "Host tag should be removed")
assert.Contains(t, metrics[0].Tags, "x:e", "Last tag is still around")
}
func TestFlushTracesDatadog(t *testing.T) {
type TestCase struct {
Name string
ProtobufFile string
JSONFile string
}
cases := []TestCase{
{
Name: "Success",
ProtobufFile: filepath.Join("fixtures", "protobuf", "trace.pb"),
JSONFile: filepath.Join("fixtures", "tracing_agent", "spans", "trace.pb.json"),
},
{
Name: "Critical",
ProtobufFile: filepath.Join("fixtures", "protobuf", "trace_critical.pb"),
JSONFile: filepath.Join("fixtures", "tracing_agent", "spans", "trace_critical.pb.json"),
},
}
for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
pb, err := os.Open(tc.ProtobufFile)
assert.NoError(t, err)
defer pb.Close()
js, err := os.Open(tc.JSONFile)
assert.NoError(t, err)
defer js.Close()
testFlushTraceDatadog(t, pb, js)
})
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%s_%s", tc.Name, "lightstep"), func(t *testing.T) {
pb, err := os.Open(tc.ProtobufFile)
assert.NoError(t, err)
defer pb.Close()
js, err := os.Open(tc.JSONFile)
assert.NoError(t, err)
defer js.Close()
testFlushTraceLightstep(t, pb, js)
})
}
}
func testFlushTraceDatadog(t *testing.T, protobuf, jsn io.Reader) {
var expected []*DatadogTraceSpan
err := json.NewDecoder(jsn).Decode(&expected)
assert.NoError(t, err)
remoteResponseChan := make(chan []*DatadogTraceSpan, 1)
remoteServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var actual []*DatadogTraceSpan
err = json.NewDecoder(r.Body).Decode(&actual)
assert.NoError(t, err)
w.WriteHeader(http.StatusAccepted)
remoteResponseChan <- actual
}))
defer remoteServer.Close()
config := globalConfig()
config.DatadogTraceAPIAddress = remoteServer.URL
server := setupVeneurServer(t, config, nil)
defer server.Shutdown()
server.tracerSinks = append(server.tracerSinks, tracerSink{
name: "Datadog",
tracer: nil,
flush: flushSpansDatadog,
})
assert.Equal(t, server.DDTraceAddress, config.DatadogTraceAPIAddress)
packet, err := ioutil.ReadAll(protobuf)
assert.NoError(t, err)
server.HandleTracePacket(packet)
server.Flush()
// wait for remoteServer to process the POST
select {
case actual := <-remoteResponseChan:
assert.Equal(t, expected, actual)
// all is safe
break
case <-time.After(10 * time.Second):
assert.Fail(t, "Global server did not complete all responses before test terminated!")
}
}
// testFlushTraceLightstep tests that the Lightstep sink can be initialized correctly
// and that the flushSpansLightstep function executes without error.
// We can't actually test the functionality end-to-end because the lightstep
// implementation doesn't expose itself for mocking.
func testFlushTraceLightstep(t *testing.T, protobuf, jsn io.Reader) {
config := globalConfig()
// this can be anything as long as it's not empty
config.DatadogTraceAPIAddress = "http://example.org"
server := setupVeneurServer(t, config, nil)
defer server.Shutdown()
lightstepTracer := lightstep.NewTracer(lightstep.Options{
AccessToken: "TestAccessToken",
})
server.tracerSinks = append(server.tracerSinks, tracerSink{
name: "Lightstep",
tracer: lightstepTracer,
flush: flushSpansLightstep,
})
assert.Equal(t, server.DDTraceAddress, config.DatadogTraceAPIAddress)
packet, err := ioutil.ReadAll(protobuf)
assert.NoError(t, err)
server.HandleTracePacket(packet)
assert.NoError(t, err)
server.Flush()
}