forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator_test.go
125 lines (114 loc) · 3.28 KB
/
translator_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package datadogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver"
import (
"bytes"
"io"
"net/http"
"testing"
"github.com/DataDog/datadog-agent/pkg/trace/pb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
vmsgp "github.com/vmihailenco/msgpack/v4"
)
var data = [2]interface{}{
0: []string{
0: "baggage",
1: "item",
2: "elasticsearch.version",
3: "7.0",
4: "my-name",
5: "X",
6: "my-service",
7: "my-resource",
8: "_dd.sampling_rate_whatever",
9: "value whatever",
10: "sql",
11: "service.name",
},
1: [][][12]interface{}{
{
{
6,
4,
7,
uint64(12345678901234561234),
uint64(2),
uint64(3),
int64(123),
int64(456),
1,
map[interface{}]interface{}{
8: 9,
0: 1,
2: 3,
11: 6,
},
map[interface{}]float64{
5: 1.2,
},
10,
},
},
},
}
func TestTracePayloadV05Unmarshalling(t *testing.T) {
var traces pb.Traces
payload, err := vmsgp.Marshal(&data)
assert.NoError(t, err)
require.NoError(t, traces.UnmarshalMsgDictionary(payload), "Must not error when marshaling content")
req, _ := http.NewRequest(http.MethodPost, "/v0.5/traces", io.NopCloser(bytes.NewReader(payload)))
translated := toTraces(&pb.TracerPayload{
LanguageName: req.Header.Get("Datadog-Meta-Lang"),
LanguageVersion: req.Header.Get("Datadog-Meta-Lang-Version"),
TracerVersion: req.Header.Get("Datadog-Meta-Tracer-Version"),
Chunks: traceChunksFromTraces(traces),
}, req)
assert.Equal(t, 1, translated.SpanCount(), "Span Count wrong")
span := translated.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
assert.NotNil(t, span)
assert.Equal(t, 4, span.Attributes().Len(), "missing tags")
value, exists := span.Attributes().Get("service.name")
assert.True(t, exists, "service.name missing")
assert.Equal(t, "my-service", value.AsString(), "service.name tag value incorrect")
assert.Equal(t, "my-name", span.Name())
}
func TestTracePayloadV07Unmarshalling(t *testing.T) {
var traces pb.Traces
payload, err := vmsgp.Marshal(&data)
assert.NoError(t, err)
if err2 := traces.UnmarshalMsgDictionary(payload); err2 != nil {
t.Fatal(err2)
}
apiPayload := pb.TracerPayload{
LanguageName: "1",
LanguageVersion: "1",
Chunks: traceChunksFromTraces(traces),
TracerVersion: "1",
}
var reqBytes []byte
bytez, _ := apiPayload.MarshalMsg(reqBytes)
req, _ := http.NewRequest(http.MethodPost, "/v0.7/traces", io.NopCloser(bytes.NewReader(bytez)))
translated, _ := handlePayload(req)
span := translated.GetChunks()[0].GetSpans()[0]
assert.NotNil(t, span)
assert.Equal(t, 4, len(span.GetMeta()), "missing tags")
value, exists := span.GetMeta()["service.name"]
assert.True(t, exists, "service.name missing")
assert.Equal(t, "my-service", value, "service.name tag value incorrect")
assert.Equal(t, "my-name", span.GetName())
}
func BenchmarkTranslatorv05(b *testing.B) {
b.StartTimer()
for n := 0; n < b.N; n++ {
TestTracePayloadV05Unmarshalling(&testing.T{})
}
b.StopTimer()
}
func BenchmarkTranslatorv07(b *testing.B) {
b.StartTimer()
for n := 0; n < b.N; n++ {
TestTracePayloadV07Unmarshalling(&testing.T{})
}
b.StopTimer()
}