forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traces.go
453 lines (381 loc) · 14.7 KB
/
traces.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ptracetest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/ptracetest"
import (
"fmt"
"reflect"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal"
)
// CompareTraces compares each part of two given Traces and returns
// an error if they don't match. The error describes what didn't match.
func CompareTraces(expected, actual ptrace.Traces, options ...CompareTracesOption) error {
exp, act := ptrace.NewTraces(), ptrace.NewTraces()
expected.CopyTo(exp)
actual.CopyTo(act)
for _, option := range options {
option.applyOnTraces(exp, act)
}
expectedSpans, actualSpans := exp.ResourceSpans(), act.ResourceSpans()
if expectedSpans.Len() != actualSpans.Len() {
return fmt.Errorf("number of resources doesn't match expected: %d, actual: %d",
expectedSpans.Len(),
actualSpans.Len())
}
numResources := expectedSpans.Len()
// Keep track of matching resources so that each can only be matched once
matchingResources := make(map[ptrace.ResourceSpans]ptrace.ResourceSpans, numResources)
var errs error
var outOfOrderErrs error
for e := 0; e < numResources; e++ {
er := expectedSpans.At(e)
var foundMatch bool
for a := 0; a < numResources; a++ {
ar := actualSpans.At(a)
if _, ok := matchingResources[ar]; ok {
continue
}
if reflect.DeepEqual(er.Resource().Attributes().AsRaw(), ar.Resource().Attributes().AsRaw()) {
foundMatch = true
matchingResources[ar] = er
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`resources are out of order: resource "%v" expected at index %d, found at index %d`,
er.Resource().Attributes().AsRaw(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected resource: %v", er.Resource().Attributes().AsRaw()))
}
}
for i := 0; i < numResources; i++ {
if _, ok := matchingResources[actualSpans.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected resource: %v", actualSpans.At(i).Resource().Attributes().AsRaw()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for ar, er := range matchingResources {
errPrefix := fmt.Sprintf(`resource "%v"`, er.Resource().Attributes().AsRaw())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareResourceSpans(er, ar)))
}
return errs
}
// CompareResourceSpans compares each part of two given ResourceSpans and returns
// an error if they don't match. The error describes what didn't match.
func CompareResourceSpans(expected, actual ptrace.ResourceSpans) error {
errs := multierr.Combine(
internal.CompareResource(expected.Resource(), actual.Resource()),
internal.CompareSchemaURL(expected.SchemaUrl(), actual.SchemaUrl()),
)
if expected.ScopeSpans().Len() != actual.ScopeSpans().Len() {
errs = multierr.Append(errs, fmt.Errorf("number of scopes doesn't match expected: %d, actual: %d",
expected.ScopeSpans().Len(), actual.ScopeSpans().Len()))
return errs
}
numScopeSpans := expected.ScopeSpans().Len()
// Keep track of matching scope logs so that each record can only be matched once
matchingScopeSpans := make(map[ptrace.ScopeSpans]ptrace.ScopeSpans, numScopeSpans)
var outOfOrderErrs error
for e := 0; e < numScopeSpans; e++ {
es := expected.ScopeSpans().At(e)
var foundMatch bool
for a := 0; a < numScopeSpans; a++ {
as := actual.ScopeSpans().At(a)
if _, ok := matchingScopeSpans[as]; ok {
continue
}
if es.Scope().Name() == as.Scope().Name() {
foundMatch = true
matchingScopeSpans[as] = es
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf("scopes are out of order: scope %s expected at index %d, found at index %d",
es.Scope().Name(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected scope: %s", es.Scope().Name()))
}
}
for i := 0; i < numScopeSpans; i++ {
if _, ok := matchingScopeSpans[actual.ScopeSpans().At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected scope: %s", actual.ScopeSpans().At(i).Scope().Name()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for i := 0; i < expected.ScopeSpans().Len(); i++ {
errPrefix := fmt.Sprintf(`scope "%s"`, expected.ScopeSpans().At(i).Scope().Name())
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix,
CompareScopeSpans(expected.ScopeSpans().At(i), actual.ScopeSpans().At(i))))
}
return errs
}
// CompareScopeSpans compares each part of two given SpanSlices and returns
// an error if they don't match. The error describes what didn't match.
func CompareScopeSpans(expected, actual ptrace.ScopeSpans) error {
errs := multierr.Combine(
internal.CompareInstrumentationScope(expected.Scope(), actual.Scope()),
internal.CompareSchemaURL(expected.SchemaUrl(), actual.SchemaUrl()),
)
if expected.Spans().Len() != actual.Spans().Len() {
errs = multierr.Append(errs, fmt.Errorf("number of spans doesn't match expected: %d, actual: %d",
expected.Spans().Len(), actual.Spans().Len()))
return errs
}
numSpans := expected.Spans().Len()
// Keep track of matching spans so that each span can only be matched once
matchingSpans := make(map[ptrace.Span]ptrace.Span, numSpans)
var outOfOrderErrs error
for e := 0; e < numSpans; e++ {
es := expected.Spans().At(e)
var foundMatch bool
for a := 0; a < numSpans; a++ {
as := actual.Spans().At(a)
if _, ok := matchingSpans[as]; ok {
continue
}
if es.Name() == as.Name() {
foundMatch = true
matchingSpans[as] = es
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`spans are out of order: span "%s" expected at index %d, found at index %d`,
es.Name(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected span: %s", es.Name()))
}
}
for i := 0; i < numSpans; i++ {
if _, ok := matchingSpans[actual.Spans().At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected span: %s", actual.Spans().At(i).Name()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for as, es := range matchingSpans {
errs = multierr.Append(errs, internal.AddErrPrefix(fmt.Sprintf(`span "%s"`, es.Name()), CompareSpan(es, as)))
}
return errs
}
// CompareSpan compares each part of two given Span and returns
// an error if they don't match. The error describes what didn't match.
func CompareSpan(expected, actual ptrace.Span) error {
errs := multierr.Combine(
internal.CompareAttributes(expected.Attributes(), actual.Attributes()),
internal.CompareDroppedAttributesCount(expected.DroppedAttributesCount(), actual.DroppedAttributesCount()),
)
if expected.TraceID() != actual.TraceID() {
errs = multierr.Append(errs, fmt.Errorf("trace ID doesn't match expected: %s, actual: %s",
expected.TraceID(), actual.TraceID()))
}
if expected.SpanID() != actual.SpanID() {
errs = multierr.Append(errs, fmt.Errorf("span ID doesn't match expected: %s, actual: %s",
expected.SpanID(), actual.SpanID()))
}
if expected.TraceState().AsRaw() != actual.TraceState().AsRaw() {
errs = multierr.Append(errs, fmt.Errorf("trace state doesn't match expected: %s, actual: %s",
expected.TraceState().AsRaw(), actual.TraceState().AsRaw()))
}
if expected.ParentSpanID() != actual.ParentSpanID() {
errs = multierr.Append(errs, fmt.Errorf("parent span ID doesn't match expected: %s, actual: %s",
expected.ParentSpanID(), actual.ParentSpanID()))
}
if expected.Name() != actual.Name() {
errs = multierr.Append(errs, fmt.Errorf("name doesn't match expected: %s, actual: %s", expected.Name(),
actual.Name()))
}
if expected.Kind() != actual.Kind() {
errs = multierr.Append(errs, fmt.Errorf("kind doesn't match expected: %d, actual: %d", expected.Kind(),
actual.Kind()))
}
if expected.StartTimestamp() != actual.StartTimestamp() {
errs = multierr.Append(errs, fmt.Errorf("start timestamp doesn't match expected: %d, actual: %d",
expected.StartTimestamp(), actual.StartTimestamp()))
}
if expected.EndTimestamp() != actual.EndTimestamp() {
errs = multierr.Append(errs, fmt.Errorf("end timestamp doesn't match expected: %d, actual: %d",
expected.EndTimestamp(), actual.EndTimestamp()))
}
errs = multierr.Append(errs, compareSpanEventSlice(expected.Events(), actual.Events()))
if expected.DroppedEventsCount() != actual.DroppedEventsCount() {
errs = multierr.Append(errs, fmt.Errorf("dropped events count doesn't match expected: %d, actual: %d",
expected.DroppedEventsCount(), actual.DroppedEventsCount()))
}
errs = multierr.Append(errs, compareSpanLinkSlice(expected.Links(), actual.Links()))
if expected.DroppedLinksCount() != actual.DroppedLinksCount() {
errs = multierr.Append(errs, fmt.Errorf("dropped links count doesn't match expected: %d, actual: %d",
expected.DroppedLinksCount(), actual.DroppedLinksCount()))
}
if expected.Status().Code() != actual.Status().Code() {
errs = multierr.Append(errs, fmt.Errorf("status code doesn't match expected: %v, actual: %v",
expected.Status().Code(), actual.Status().Code()))
}
if expected.Status().Message() != actual.Status().Message() {
errs = multierr.Append(errs, fmt.Errorf("status message doesn't match expected: %v, actual: %v",
expected.Status().Message(), actual.Status().Message()))
}
return errs
}
// compareSpanEventSlice compares each part of two given SpanEventSlice and returns
// an error if they don't match. The error describes what didn't match.
func compareSpanEventSlice(expected, actual ptrace.SpanEventSlice) (errs error) {
if expected.Len() != actual.Len() {
errs = multierr.Append(errs, fmt.Errorf("number of events doesn't match expected: %d, actual: %d",
expected.Len(), actual.Len()))
return errs
}
numSpanEvents := expected.Len()
// Keep track of matching span events so that each span event can only be matched once
matchingSpanEvents := make(map[ptrace.SpanEvent]ptrace.SpanEvent, numSpanEvents)
var outOfOrderErrs error
for e := 0; e < numSpanEvents; e++ {
ee := expected.At(e)
var foundMatch bool
for a := 0; a < numSpanEvents; a++ {
ae := actual.At(a)
if _, ok := matchingSpanEvents[ae]; ok {
continue
}
if ee.Name() == ae.Name() {
foundMatch = true
matchingSpanEvents[ae] = ee
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`span events are out of order: span event "%s" expected at index %d, found at index %d`,
ee.Name(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected span event: %s", ee.Name()))
}
}
for i := 0; i < numSpanEvents; i++ {
if _, ok := matchingSpanEvents[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected span event: %s", actual.At(i).Name()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for ae, ee := range matchingSpanEvents {
errs = multierr.Append(errs, internal.AddErrPrefix(fmt.Sprintf(`span event "%s"`, ee.Name()), CompareSpanEvent(ee, ae)))
}
return errs
}
// CompareSpanEvent compares each part of two given SpanEvent and returns
// an error if they don't match. The error describes what didn't match.
func CompareSpanEvent(expected, actual ptrace.SpanEvent) error {
errs := multierr.Combine(
internal.CompareAttributes(expected.Attributes(), actual.Attributes()),
internal.CompareDroppedAttributesCount(expected.DroppedAttributesCount(), actual.DroppedAttributesCount()),
)
if expected.Name() != actual.Name() {
errs = multierr.Append(errs, fmt.Errorf("name doesn't match expected: %s, actual: %s",
expected.Name(), actual.Name()))
}
if expected.Timestamp() != actual.Timestamp() {
errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d",
expected.Timestamp(), actual.Timestamp()))
}
return errs
}
// compareSpanLinkSlice compares each part of two given SpanLinkSlice and returns
// an error if they don't match. The error describes what didn't match.
func compareSpanLinkSlice(expected, actual ptrace.SpanLinkSlice) (errs error) {
if expected.Len() != actual.Len() {
errs = multierr.Append(errs, fmt.Errorf("number of span links doesn't match expected: %d, actual: %d",
expected.Len(), actual.Len()))
return errs
}
numSpanLinks := expected.Len()
// Keep track of matching span links so that each span link can only be matched once
matchingSpanLinks := make(map[ptrace.SpanLink]ptrace.SpanLink, numSpanLinks)
var outOfOrderErrs error
for e := 0; e < numSpanLinks; e++ {
el := expected.At(e)
var foundMatch bool
for a := 0; a < numSpanLinks; a++ {
al := actual.At(a)
if _, ok := matchingSpanLinks[al]; ok {
continue
}
if el.SpanID() == al.SpanID() {
foundMatch = true
matchingSpanLinks[al] = el
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`span links are out of order: span link "%s" expected at index %d, found at index %d`,
el.SpanID(), e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected span link: %s", el.SpanID()))
}
}
for i := 0; i < numSpanLinks; i++ {
if _, ok := matchingSpanLinks[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected span link: %s", actual.At(i).SpanID()))
}
}
if errs != nil {
return errs
}
if outOfOrderErrs != nil {
return outOfOrderErrs
}
for al, el := range matchingSpanLinks {
errs = multierr.Append(errs, internal.AddErrPrefix(fmt.Sprintf(`span link "%s"`, el.SpanID()), CompareSpanLink(el, al)))
}
return errs
}
// CompareSpanLink compares each part of two given SpanLink and returns
// an error if they don't match. The error describes what didn't match.
func CompareSpanLink(expected, actual ptrace.SpanLink) error {
errs := multierr.Combine(
internal.CompareAttributes(expected.Attributes(), actual.Attributes()),
internal.CompareDroppedAttributesCount(expected.DroppedAttributesCount(), actual.DroppedAttributesCount()),
)
if expected.TraceID() != actual.TraceID() {
errs = multierr.Append(errs, fmt.Errorf("trace ID doesn't match expected: %s, actual: %s",
expected.TraceID(), actual.TraceID()))
}
if expected.SpanID() != actual.SpanID() {
errs = multierr.Append(errs, fmt.Errorf("span ID doesn't match expected: %s, actual: %s",
expected.SpanID(), actual.SpanID()))
}
if expected.TraceState().AsRaw() != actual.TraceState().AsRaw() {
errs = multierr.Append(errs, fmt.Errorf("trace state doesn't match expected: %s, actual: %s",
expected.TraceState().AsRaw(), actual.TraceState().AsRaw()))
}
return errs
}