-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathextract_test.go
200 lines (176 loc) · 4.83 KB
/
extract_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
package tracing
import (
"reflect"
"testing"
"time"
"github.com/cloudflare/ebpf_exporter/v2/config"
"github.com/cloudflare/ebpf_exporter/v2/decoder"
"github.com/cloudflare/ebpf_exporter/v2/util"
"go.opentelemetry.io/otel/attribute"
)
func TestExtractEmpty(t *testing.T) {
config := config.Span{RingBuf: "some_buf"}
name, timestamp, duration, traceID, parentID, spanID, attributes, err := extractSpan([]string{}, config)
if err != nil {
t.Errorf("Error extracting span from empty labels: %v", err)
}
if name != config.RingBuf {
t.Errorf("Expected name %q to match ringbuf name %q", name, config.RingBuf)
}
timeDiff := time.Since(timestamp)
if timeDiff.Abs().Milliseconds() > 1 {
t.Errorf("Expected time difference to be negligible, got %v", timeDiff)
}
if duration != 0 {
t.Errorf("Expected duration to be zero, got %v", duration)
}
if traceID.IsValid() {
t.Errorf("Expected empty traceID, got %q", traceID.String())
}
if parentID.IsValid() {
t.Errorf("Expected empty parent spanID, got %q", parentID.String())
}
if spanID.IsValid() {
t.Errorf("Expected empty spanID, got %q", spanID.String())
}
if len(attributes) > 0 {
t.Errorf("Expected no attributes, got %#v", attributes)
}
}
func TestExtractFilled(t *testing.T) {
byteOrder := util.GetHostByteOrder()
// ktime 1 minute into the future
ktimeBuf := make([]byte, 8)
byteOrder.PutUint64(ktimeBuf, ktime()+60*1000000000)
cases := []struct {
in []byte
config config.Span
name string
timestamp time.Time
duration time.Duration
traceID string
parentID string
spanID string
attributes []attribute.KeyValue
}{
{
in: append(
append(
[]byte{
0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xed, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // trace_id
0xfe, 0xed, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // parent_span_id
0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // span_id
},
ktimeBuf..., // span_monotonic_timestamp_ns
),
[]byte{
0xff, 0xab, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, // span_duration_ns
0x68, 0x61, 0x68, 0x61, // lol
}...,
),
config: config.Span{
Name: "potato",
Labels: []config.Label{
{
Name: "trace_id",
Size: 16,
Decoders: []config.Decoder{
{
Name: "hex",
},
},
},
{
Name: "parent_span_id",
Size: 8,
Decoders: []config.Decoder{
{
Name: "hex",
},
},
},
{
Name: "span_id",
Size: 8,
Decoders: []config.Decoder{
{
Name: "hex",
},
},
},
{
Name: "span_monotonic_timestamp_ns",
Size: 8,
Decoders: []config.Decoder{
{
Name: "uint",
},
},
},
{
Name: "span_duration_ns",
Size: 8,
Decoders: []config.Decoder{
{
Name: "uint",
},
},
},
{
Name: "lol",
Size: 4,
Decoders: []config.Decoder{
{
Name: "string",
},
},
},
},
},
name: "potato",
timestamp: time.Now().Add(time.Minute),
traceID: "beef000000000000feed000000000000",
parentID: "feed000000000000",
spanID: "beef000000000000",
duration: time.Duration(4041727),
attributes: []attribute.KeyValue{attribute.String("lol", "haha")},
},
}
decoders, err := decoder.NewSet()
if err != nil {
t.Fatalf("Error creating decoders set: %v", err)
}
for i, c := range cases {
labels, err := extractLabels(c.in, decoders, c.config)
if err != nil {
t.Errorf("Error extracting labels from %#v: %v", c.in, err)
continue
}
name, timestamp, duration, traceID, parentID, spanID, attributes, err := extractSpan(labels, c.config)
if err != nil {
t.Errorf("Error extracting span from labels for %d: %v", i, err)
}
if name != c.name {
t.Errorf("Expected name %q, got %q for %d", c.name, name, i)
}
timeDiff := timestamp.Sub(c.timestamp)
if timeDiff.Abs().Milliseconds() > 1 {
t.Errorf("Expected negligible time difference, got %q (timestamp %q expected, got %q) for %d", timeDiff.String(), c.timestamp.Format(time.RFC3339Nano), timestamp.Format(time.RFC3339Nano), i)
}
if duration != c.duration {
t.Errorf("Expected duration %dns, got %dns for %d / %v", c.duration.Nanoseconds(), duration.Nanoseconds(), i, labels)
}
if traceID.String() != c.traceID {
t.Errorf("Expected traceID %q, got %q for %d", c.traceID, traceID.String(), i)
}
if parentID.String() != c.parentID {
t.Errorf("Expected parent spanID %q, got %q for %d", c.parentID, parentID.String(), i)
}
if spanID.String() != c.spanID {
t.Errorf("Expected spanID %q, got %q for %d", c.spanID, spanID.String(), i)
}
if !reflect.DeepEqual(attributes, c.attributes) {
t.Errorf("Expected attributes %#v, got %#v for %d", c.attributes, attributes, i)
}
}
}