-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtrace_receiver_test.go
498 lines (420 loc) · 14.3 KB
/
trace_receiver_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package zipkinreceiver
import (
"bytes"
"compress/gzip"
"compress/zlib"
"context"
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
zipkin2 "github.com/jaegertracing/jaeger/model/converter/thrift/zipkin"
"github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/receiver/receivertest"
conventions "go.opentelemetry.io/collector/semconv/v1.12.0"
)
const (
zipkinV2Single = "../../pkg/translator/zipkin/zipkinv2/testdata/zipkin_v2_single.json"
zipkinV2NoTimestamp = "../../pkg/translator/zipkin/zipkinv2/testdata/zipkin_v2_notimestamp.json"
zipkinV1SingleBatch = "../../pkg/translator/zipkin/zipkinv1/testdata/zipkin_v1_single_batch.json"
)
func TestNew(t *testing.T) {
type args struct {
address string
nextConsumer consumer.Traces
}
tests := []struct {
name string
args args
wantErr error
}{
{
name: "happy path",
args: args{
nextConsumer: consumertest.NewNop(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: tt.args.address,
},
}
got, err := newReceiver(cfg, tt.args.nextConsumer, receivertest.NewNopSettings())
require.Equal(t, tt.wantErr, err)
if tt.wantErr == nil {
require.NotNil(t, got)
} else {
require.Nil(t, got)
}
})
}
}
func TestZipkinReceiverPortAlreadyInUse(t *testing.T) {
l, err := net.Listen("tcp", "localhost:")
require.NoError(t, err, "failed to open a port: %v", err)
defer l.Close()
_, portStr, err := net.SplitHostPort(l.Addr().String())
require.NoError(t, err, "failed to split listener address: %v", err)
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: "localhost:" + portStr,
},
}
traceReceiver, err := newReceiver(cfg, consumertest.NewNop(), receivertest.NewNopSettings())
require.NoError(t, err, "Failed to create receiver: %v", err)
err = traceReceiver.Start(context.Background(), componenttest.NewNopHost())
require.Error(t, err)
}
func TestConvertSpansToTraceSpans_json(t *testing.T) {
// Using Adrian Cole's sample at https://gist.github.com/adriancole/e8823c19dfed64e2eb71
blob, err := os.ReadFile("./testdata/sample1.json")
require.NoError(t, err, "Failed to read sample JSON file: %v", err)
zi := newTestZipkinReceiver()
reqs, err := zi.v2ToTraceSpans(blob, nil)
require.NoError(t, err, "Failed to parse convert Zipkin spans in JSON to Trace spans: %v", err)
require.Equal(t, 1, reqs.ResourceSpans().Len(), "Expecting only one request since all spans share same node/localEndpoint: %v", reqs.ResourceSpans().Len())
req := reqs.ResourceSpans().At(0)
sn, _ := req.Resource().Attributes().Get(conventions.AttributeServiceName)
assert.Equal(t, "frontend", sn.Str())
// Expecting 9 non-nil spans
require.Equal(t, 9, reqs.SpanCount(), "Incorrect non-nil spans count")
}
func TestStartTraceReception(t *testing.T) {
tests := []struct {
name string
host component.Host
wantErr bool
}{
{
name: "nil_host",
wantErr: true,
},
{
name: "valid_host",
host: componenttest.NewNopHost(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sink := new(consumertest.TracesSink)
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: "localhost:0",
},
}
zr, err := newReceiver(cfg, sink, receivertest.NewNopSettings())
require.NoError(t, err)
require.NotNil(t, zr)
err = zr.Start(context.Background(), tt.host)
assert.Equal(t, tt.wantErr, err != nil)
if !tt.wantErr {
require.NoError(t, zr.Shutdown(context.Background()))
}
})
}
}
func TestReceiverContentTypes(t *testing.T) {
tests := []struct {
endpoint string
content string
encoding string
bodyFn func() ([]byte, error)
}{
{
endpoint: "/api/v1/spans",
content: "application/json",
encoding: "gzip",
bodyFn: func() ([]byte, error) {
return os.ReadFile(zipkinV1SingleBatch)
},
},
{
endpoint: "/api/v1/spans",
content: "application/x-thrift",
encoding: "gzip",
bodyFn: func() ([]byte, error) {
return thriftExample(), nil
},
},
{
endpoint: "/api/v2/spans",
content: "application/json",
encoding: "gzip",
bodyFn: func() ([]byte, error) {
return os.ReadFile(zipkinV2Single)
},
},
{
endpoint: "/api/v2/spans",
content: "application/json",
encoding: "zlib",
bodyFn: func() ([]byte, error) {
return os.ReadFile(zipkinV2Single)
},
},
{
endpoint: "/api/v2/spans",
content: "application/json",
encoding: "",
bodyFn: func() ([]byte, error) {
return os.ReadFile(zipkinV2Single)
},
},
}
for _, test := range tests {
name := fmt.Sprintf("%v %v %v", test.endpoint, test.content, test.encoding)
t.Run(name, func(t *testing.T) {
body, err := test.bodyFn()
require.NoError(t, err, "Failed to generate test body: %v", err)
var requestBody *bytes.Buffer
switch test.encoding {
case "":
requestBody = bytes.NewBuffer(body)
case "zlib":
requestBody, err = compressZlib(body)
case "gzip":
requestBody, err = compressGzip(body)
}
require.NoError(t, err)
r := httptest.NewRequest(http.MethodPost, test.endpoint, requestBody)
r.Header.Add("content-type", test.content)
r.Header.Add("content-encoding", test.encoding)
next := new(consumertest.TracesSink)
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: "",
},
}
zr, err := newReceiver(cfg, next, receivertest.NewNopSettings())
require.NoError(t, err)
req := httptest.NewRecorder()
zr.ServeHTTP(req, r)
require.Equal(t, 202, req.Code)
assert.Eventually(t, func() bool {
allTraces := next.AllTraces()
return len(allTraces) != 0
}, 2*time.Second, 10*time.Millisecond)
})
}
}
func TestReceiverInvalidContentType(t *testing.T) {
body := `{ invalid json `
r := httptest.NewRequest(http.MethodPost, "/api/v2/spans",
bytes.NewBuffer([]byte(body)))
r.Header.Add("content-type", "application/json")
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: "",
},
}
zr, err := newReceiver(cfg, consumertest.NewNop(), receivertest.NewNopSettings())
require.NoError(t, err)
req := httptest.NewRecorder()
zr.ServeHTTP(req, r)
require.Equal(t, 400, req.Code)
require.Equal(t, "invalid character 'i' looking for beginning of object key string\n", req.Body.String())
}
func TestReceiverConsumerError(t *testing.T) {
body, err := os.ReadFile(zipkinV2Single)
require.NoError(t, err)
r := httptest.NewRequest(http.MethodPost, "/api/v2/spans", bytes.NewBuffer(body))
r.Header.Add("content-type", "application/json")
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: "localhost:9411",
},
}
zr, err := newReceiver(cfg, consumertest.NewErr(errors.New("consumer error")), receivertest.NewNopSettings())
require.NoError(t, err)
req := httptest.NewRecorder()
zr.ServeHTTP(req, r)
require.Equal(t, 500, req.Code)
require.Equal(t, "\"Internal Server Error\"", req.Body.String())
}
func TestReceiverConsumerPermanentError(t *testing.T) {
body, err := os.ReadFile(zipkinV2Single)
require.NoError(t, err)
r := httptest.NewRequest(http.MethodPost, "/api/v2/spans", bytes.NewBuffer(body))
r.Header.Add("content-type", "application/json")
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: "localhost:9411",
},
}
zr, err := newReceiver(cfg, consumertest.NewErr(consumererror.NewPermanent(errors.New("consumer error"))), receivertest.NewNopSettings())
require.NoError(t, err)
req := httptest.NewRecorder()
zr.ServeHTTP(req, r)
require.Equal(t, 400, req.Code)
require.Equal(t, "\"Bad Request\"", req.Body.String())
}
func thriftExample() []byte {
now := time.Now().Unix()
zSpans := []*zipkincore.Span{
{
TraceID: 1,
Name: "test",
ID: 2,
BinaryAnnotations: []*zipkincore.BinaryAnnotation{
{
Key: "http.path",
Value: []byte("/"),
},
},
Timestamp: &now,
},
}
return zipkin2.SerializeThrift(context.TODO(), zSpans)
}
func compressGzip(body []byte) (*bytes.Buffer, error) {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
_, err := zw.Write(body)
if err != nil {
return nil, err
}
if err := zw.Close(); err != nil {
return nil, err
}
return &buf, nil
}
func compressZlib(body []byte) (*bytes.Buffer, error) {
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
_, err := zw.Write(body)
if err != nil {
return nil, err
}
if err := zw.Close(); err != nil {
return nil, err
}
return &buf, nil
}
func TestConvertSpansToTraceSpans_JSONWithoutSerivceName(t *testing.T) {
blob, err := os.ReadFile("./testdata/sample2.json")
require.NoError(t, err, "Failed to read sample JSON file: %v", err)
zi := newTestZipkinReceiver()
reqs, err := zi.v2ToTraceSpans(blob, nil)
require.NoError(t, err, "Failed to parse convert Zipkin spans in JSON to Trace spans: %v", err)
require.Equal(t, 1, reqs.ResourceSpans().Len(), "Expecting only one request since all spans share same node/localEndpoint: %v", reqs.ResourceSpans().Len())
// Expecting 1 non-nil spans
require.Equal(t, 1, reqs.SpanCount(), "Incorrect non-nil spans count")
}
func TestConvertSpansToTraceSpans_JSONWithSimpleError(t *testing.T) {
blob, err := os.ReadFile("./testdata/sample3.json")
require.NoError(t, err, "Failed to read sample JSON file: %v", err)
zi := newTestZipkinReceiver()
reqs, err := zi.v2ToTraceSpans(blob, nil)
require.NoError(t, err, "Failed to parse convert Zipkin spans in JSON to Trace spans: %v", err)
require.Equal(t, 1, reqs.ResourceSpans().Len(), "Expecting only one request since all spans share same node/localEndpoint: %v", reqs.ResourceSpans().Len())
// Expecting 1 non-nil spans
require.Equal(t, 1, reqs.SpanCount(), "Incorrect non-nil spans count")
require.Equal(t, ptrace.StatusCodeError, reqs.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Status().Code())
_, exists := reqs.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().Get("error")
require.False(t, exists, "Error attribute should be removed when error is simply set to \"true\".")
}
func TestConvertSpansToTraceSpans_JSONWithErrorMessage(t *testing.T) {
blob, err := os.ReadFile("./testdata/sample4.json")
require.NoError(t, err, "Failed to read sample JSON file: %v", err)
zi := newTestZipkinReceiver()
reqs, err := zi.v2ToTraceSpans(blob, nil)
require.NoError(t, err, "Failed to parse convert Zipkin spans in JSON to Trace spans: %v", err)
require.Equal(t, 1, reqs.ResourceSpans().Len(), "Expecting only one request since all spans share same node/localEndpoint: %v", reqs.ResourceSpans().Len())
// Expecting 1 non-nil spans
require.Equal(t, 1, reqs.SpanCount(), "Incorrect non-nil spans count")
require.Equal(t, ptrace.StatusCodeError, reqs.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Status().Code(),
"Error code should be set to the proper error status since the trace had an error tag.")
errorMessage, exists := reqs.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().Get("error")
require.True(t, exists, "Given span should have an error attribute with the received span's error message.")
require.Equal(t, "Non-basic error message", errorMessage.AsString(), "Error message should be retained in the span.")
}
func TestReceiverConvertsStringsToTypes(t *testing.T) {
body, err := os.ReadFile(zipkinV2Single)
require.NoError(t, err, "Failed to read sample JSON file: %v", err)
r := httptest.NewRequest(http.MethodPost, "/api/v2/spans", bytes.NewBuffer(body))
r.Header.Add("content-type", "application/json")
next := new(consumertest.TracesSink)
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: "",
},
ParseStringTags: true,
}
zr, err := newReceiver(cfg, next, receivertest.NewNopSettings())
require.NoError(t, err)
req := httptest.NewRecorder()
zr.ServeHTTP(req, r)
require.Equal(t, 202, req.Code)
require.Eventually(t, func() bool {
allTraces := next.AllTraces()
return len(allTraces) != 0
}, 2*time.Second, 10*time.Millisecond)
td := next.AllTraces()[0]
span := td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
expected := map[string]any{
"cache_hit": true,
"ping_count": int64(25),
"timeout": 12.3,
"clnt/finagle.version": "6.45.0",
"http.path": "/api",
"http.status_code": int64(500),
"net.host.ip": "7::80:807f",
"peer.service": "backend",
"net.peer.ip": "192.168.99.101",
"net.peer.port": int64(9000),
}
assert.EqualValues(t, expected, span.Attributes().AsRaw())
}
func TestFromBytesWithNoTimestamp(t *testing.T) {
noTimestampBytes, err := os.ReadFile(zipkinV2NoTimestamp)
require.NoError(t, err, "Failed to read sample JSON file: %v", err)
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: "",
},
ParseStringTags: true,
}
zi, err := newReceiver(cfg, consumertest.NewNop(), receivertest.NewNopSettings())
require.NoError(t, err)
hdr := make(http.Header)
hdr.Set("Content-Type", "application/json")
// under the hood this calls V2SpansToInternalTraces, the
// method we want to test, and this is a better end to end
// representation of what happens for the notimestamp case.
traces, err := zi.v2ToTraceSpans(noTimestampBytes, hdr)
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
gs := traces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
assert.NotNil(t, gs.StartTimestamp)
assert.NotNil(t, gs.EndTimestamp)
// missing timestamp and duration in the incoming zipkin v2 json
// are handled and converted to unix time zero in the internal span
// format.
fakeStartTimestamp := gs.StartTimestamp().AsTime().UnixNano()
assert.Equal(t, int64(0), fakeStartTimestamp)
fakeEndTimestamp := gs.StartTimestamp().AsTime().UnixNano()
assert.Equal(t, int64(0), fakeEndTimestamp)
wasAbsent, mapContainedKey := gs.Attributes().Get("otel.zipkin.absentField.startTime")
assert.True(t, mapContainedKey)
assert.True(t, wasAbsent.Bool())
}