| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package batchprocessor |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 12 |
"go.opentelemetry.io/collector/pdata/testdata" |
| 13 |
) |
| 14 |
|
| 15 |
func TestSplitTraces_noop(t *testing.T) { |
| 16 |
td := testdata.GenerateTraces(20) |
| 17 |
splitSize := 40 |
| 18 |
split := splitTraces(splitSize, td) |
| 19 |
assert.Equal(t, td, split) |
| 20 |
|
| 21 |
i := 0 |
| 22 |
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().RemoveIf(func(ptrace.Span) bool { |
| 23 |
i++ |
| 24 |
return i > 5 |
| 25 |
}) |
| 26 |
assert.Equal(t, td, split) |
| 27 |
} |
| 28 |
|
| 29 |
func TestSplitTraces(t *testing.T) { |
| 30 |
td := testdata.GenerateTraces(20) |
| 31 |
spans := td.ResourceSpans().At(0).ScopeSpans().At(0).Spans() |
| 32 |
for i := 0; i < spans.Len(); i++ { |
| 33 |
spans.At(i).SetName(getTestSpanName(0, i)) |
| 34 |
} |
| 35 |
cp := ptrace.NewTraces() |
| 36 |
cpSpans := cp.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans() |
| 37 |
cpSpans.EnsureCapacity(5) |
| 38 |
td.ResourceSpans().At(0).Resource().CopyTo( |
| 39 |
cp.ResourceSpans().At(0).Resource()) |
| 40 |
td.ResourceSpans().At(0).ScopeSpans().At(0).Scope().CopyTo( |
| 41 |
cp.ResourceSpans().At(0).ScopeSpans().At(0).Scope()) |
| 42 |
spans.At(0).CopyTo(cpSpans.AppendEmpty()) |
| 43 |
spans.At(1).CopyTo(cpSpans.AppendEmpty()) |
| 44 |
spans.At(2).CopyTo(cpSpans.AppendEmpty()) |
| 45 |
spans.At(3).CopyTo(cpSpans.AppendEmpty()) |
| 46 |
spans.At(4).CopyTo(cpSpans.AppendEmpty()) |
| 47 |
|
| 48 |
splitSize := 5 |
| 49 |
split := splitTraces(splitSize, td) |
| 50 |
assert.Equal(t, splitSize, split.SpanCount()) |
| 51 |
assert.Equal(t, cp, split) |
| 52 |
assert.Equal(t, 15, td.SpanCount()) |
| 53 |
assert.Equal(t, "test-span-0-0", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name()) |
| 54 |
assert.Equal(t, "test-span-0-4", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(4).Name()) |
| 55 |
|
| 56 |
split = splitTraces(splitSize, td) |
| 57 |
assert.Equal(t, 10, td.SpanCount()) |
| 58 |
assert.Equal(t, "test-span-0-5", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name()) |
| 59 |
assert.Equal(t, "test-span-0-9", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(4).Name()) |
| 60 |
|
| 61 |
split = splitTraces(splitSize, td) |
| 62 |
assert.Equal(t, 5, td.SpanCount()) |
| 63 |
assert.Equal(t, "test-span-0-10", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name()) |
| 64 |
assert.Equal(t, "test-span-0-14", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(4).Name()) |
| 65 |
|
| 66 |
split = splitTraces(splitSize, td) |
| 67 |
assert.Equal(t, 5, td.SpanCount()) |
| 68 |
assert.Equal(t, "test-span-0-15", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name()) |
| 69 |
assert.Equal(t, "test-span-0-19", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(4).Name()) |
| 70 |
} |
| 71 |
|
| 72 |
func TestSplitTracesMultipleResourceSpans(t *testing.T) { |
| 73 |
td := testdata.GenerateTraces(20) |
| 74 |
spans := td.ResourceSpans().At(0).ScopeSpans().At(0).Spans() |
| 75 |
for i := 0; i < spans.Len(); i++ { |
| 76 |
spans.At(i).SetName(getTestSpanName(0, i)) |
| 77 |
} |
| 78 |
// add second index to resource spans |
| 79 |
testdata.GenerateTraces(20). |
| 80 |
ResourceSpans().At(0).CopyTo(td.ResourceSpans().AppendEmpty()) |
| 81 |
spans = td.ResourceSpans().At(1).ScopeSpans().At(0).Spans() |
| 82 |
for i := 0; i < spans.Len(); i++ { |
| 83 |
spans.At(i).SetName(getTestSpanName(1, i)) |
| 84 |
} |
| 85 |
|
| 86 |
splitSize := 5 |
| 87 |
split := splitTraces(splitSize, td) |
| 88 |
assert.Equal(t, splitSize, split.SpanCount()) |
| 89 |
assert.Equal(t, 35, td.SpanCount()) |
| 90 |
assert.Equal(t, "test-span-0-0", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name()) |
| 91 |
assert.Equal(t, "test-span-0-4", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(4).Name()) |
| 92 |
} |
| 93 |
|
| 94 |
func TestSplitTracesMultipleResourceSpans_SplitSizeGreaterThanSpanSize(t *testing.T) { |
| 95 |
td := testdata.GenerateTraces(20) |
| 96 |
spans := td.ResourceSpans().At(0).ScopeSpans().At(0).Spans() |
| 97 |
for i := 0; i < spans.Len(); i++ { |
| 98 |
spans.At(i).SetName(getTestSpanName(0, i)) |
| 99 |
} |
| 100 |
// add second index to resource spans |
| 101 |
testdata.GenerateTraces(20). |
| 102 |
ResourceSpans().At(0).CopyTo(td.ResourceSpans().AppendEmpty()) |
| 103 |
spans = td.ResourceSpans().At(1).ScopeSpans().At(0).Spans() |
| 104 |
for i := 0; i < spans.Len(); i++ { |
| 105 |
spans.At(i).SetName(getTestSpanName(1, i)) |
| 106 |
} |
| 107 |
|
| 108 |
splitSize := 25 |
| 109 |
split := splitTraces(splitSize, td) |
| 110 |
assert.Equal(t, splitSize, split.SpanCount()) |
| 111 |
assert.Equal(t, 40-splitSize, td.SpanCount()) |
| 112 |
assert.Equal(t, 1, td.ResourceSpans().Len()) |
| 113 |
assert.Equal(t, "test-span-0-0", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name()) |
| 114 |
assert.Equal(t, "test-span-0-19", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(19).Name()) |
| 115 |
assert.Equal(t, "test-span-1-0", split.ResourceSpans().At(1).ScopeSpans().At(0).Spans().At(0).Name()) |
| 116 |
assert.Equal(t, "test-span-1-4", split.ResourceSpans().At(1).ScopeSpans().At(0).Spans().At(4).Name()) |
| 117 |
} |
| 118 |
|
| 119 |
func TestSplitTracesMultipleILS(t *testing.T) { |
| 120 |
td := testdata.GenerateTraces(20) |
| 121 |
spans := td.ResourceSpans().At(0).ScopeSpans().At(0).Spans() |
| 122 |
for i := 0; i < spans.Len(); i++ { |
| 123 |
spans.At(i).SetName(getTestSpanName(0, i)) |
| 124 |
} |
| 125 |
// add second index to ILS |
| 126 |
td.ResourceSpans().At(0).ScopeSpans().At(0). |
| 127 |
CopyTo(td.ResourceSpans().At(0).ScopeSpans().AppendEmpty()) |
| 128 |
spans = td.ResourceSpans().At(0).ScopeSpans().At(1).Spans() |
| 129 |
for i := 0; i < spans.Len(); i++ { |
| 130 |
spans.At(i).SetName(getTestSpanName(1, i)) |
| 131 |
} |
| 132 |
|
| 133 |
// add third index to ILS |
| 134 |
td.ResourceSpans().At(0).ScopeSpans().At(0). |
| 135 |
CopyTo(td.ResourceSpans().At(0).ScopeSpans().AppendEmpty()) |
| 136 |
spans = td.ResourceSpans().At(0).ScopeSpans().At(2).Spans() |
| 137 |
for i := 0; i < spans.Len(); i++ { |
| 138 |
spans.At(i).SetName(getTestSpanName(2, i)) |
| 139 |
} |
| 140 |
|
| 141 |
splitSize := 40 |
| 142 |
split := splitTraces(splitSize, td) |
| 143 |
assert.Equal(t, splitSize, split.SpanCount()) |
| 144 |
assert.Equal(t, 20, td.SpanCount()) |
| 145 |
assert.Equal(t, "test-span-0-0", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name()) |
| 146 |
assert.Equal(t, "test-span-0-4", split.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(4).Name()) |
| 147 |
} |