-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tailsampler: Combine batches of spans into a single batch #1864
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,10 @@ | |
package tailsamplingprocessor | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"sort" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
@@ -331,8 +333,103 @@ func TestSamplingPolicyDecisionNotSampled(t *testing.T) { | |
require.Equal(t, 2, mpe.LateArrivingSpansCount, "policy was not notified of the late span") | ||
} | ||
|
||
func TestMultipleBatchesAreCombinedIntoOne(t *testing.T) { | ||
const maxSize = 100 | ||
const decisionWaitSeconds = 1 | ||
// For this test explicitly control the timer calls and batcher, and set a mock | ||
// sampling policy evaluator. | ||
msp := new(exportertest.SinkTraceExporter) | ||
mpe := &mockPolicyEvaluator{} | ||
mtt := &manualTTicker{} | ||
tsp := &tailSamplingSpanProcessor{ | ||
ctx: context.Background(), | ||
nextConsumer: msp, | ||
maxNumTraces: maxSize, | ||
logger: zap.NewNop(), | ||
decisionBatcher: newSyncIDBatcher(decisionWaitSeconds), | ||
policies: []*Policy{{Name: "mock-policy", Evaluator: mpe, ctx: context.TODO()}}, | ||
deleteChan: make(chan traceKey, maxSize), | ||
policyTicker: mtt, | ||
} | ||
|
||
mpe.NextDecision = sampling.Sampled | ||
|
||
traceIds, batches := generateIdsAndBatches(3) | ||
for _, batch := range batches { | ||
require.NoError(t, tsp.ConsumeTraces(context.Background(), batch)) | ||
} | ||
|
||
tsp.samplingPolicyOnTick() | ||
tsp.samplingPolicyOnTick() | ||
|
||
require.EqualValues(t, 3, len(msp.AllTraces()), "There should be three batches, one for each trace") | ||
|
||
expectedSpanIds := make(map[int][]pdata.SpanID) | ||
expectedSpanIds[0] = []pdata.SpanID{ | ||
pdata.NewSpanID(tracetranslator.UInt64ToByteSpanID(uint64(1))), | ||
} | ||
expectedSpanIds[1] = []pdata.SpanID{ | ||
pdata.NewSpanID(tracetranslator.UInt64ToByteSpanID(uint64(2))), | ||
pdata.NewSpanID(tracetranslator.UInt64ToByteSpanID(uint64(3))), | ||
} | ||
expectedSpanIds[2] = []pdata.SpanID{ | ||
pdata.NewSpanID(tracetranslator.UInt64ToByteSpanID(uint64(4))), | ||
pdata.NewSpanID(tracetranslator.UInt64ToByteSpanID(uint64(5))), | ||
pdata.NewSpanID(tracetranslator.UInt64ToByteSpanID(uint64(6))), | ||
} | ||
|
||
receivedTraces := msp.AllTraces() | ||
for i, traceID := range traceIds { | ||
trace := findTrace(receivedTraces, traceID) | ||
require.NotNil(t, trace, "Trace was not received. TraceId %s", traceID.HexString()) | ||
require.EqualValues(t, i+1, trace.SpanCount(), "The trace should have all of its spans in a single batch") | ||
|
||
expected := expectedSpanIds[i] | ||
got := collectSpanIds(trace) | ||
|
||
// might have received out of order, sort for comparison | ||
sort.Slice(got, func(i, j int) bool { | ||
a, _ := tracetranslator.BytesToInt64SpanID(got[i]) | ||
b, _ := tracetranslator.BytesToInt64SpanID(got[j]) | ||
return a < b | ||
}) | ||
|
||
require.EqualValues(t, expected, got) | ||
} | ||
} | ||
|
||
func collectSpanIds(trace *pdata.Traces) []pdata.SpanID { | ||
spanIDs := make([]pdata.SpanID, 0) | ||
|
||
for i := 0; i < trace.ResourceSpans().Len(); i++ { | ||
ilss := trace.ResourceSpans().At(i).InstrumentationLibrarySpans() | ||
|
||
for j := 0; j < ilss.Len(); j++ { | ||
ils := ilss.At(j) | ||
|
||
for k := 0; k < ils.Spans().Len(); k++ { | ||
span := ils.Spans().At(k) | ||
spanIDs = append(spanIDs, span.SpanID()) | ||
} | ||
} | ||
} | ||
|
||
return spanIDs | ||
} | ||
|
||
func findTrace(a []pdata.Traces, traceID pdata.TraceID) *pdata.Traces { | ||
for _, batch := range a { | ||
id := batch.ResourceSpans().At(0).InstrumentationLibrarySpans().At(0).Spans().At(0).TraceID() | ||
if bytes.Equal(traceID.Bytes(), id.Bytes()) { | ||
return &batch | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func generateIdsAndBatches(numIds int) ([]pdata.TraceID, []pdata.Traces) { | ||
traceIds := make([]pdata.TraceID, numIds) | ||
spanID := 0 | ||
var tds []pdata.Traces | ||
for i := 0; i < numIds; i++ { | ||
traceIds[i] = tracetranslator.UInt64ToTraceID(1, uint64(i+1)) | ||
|
@@ -341,7 +438,9 @@ func generateIdsAndBatches(numIds int) ([]pdata.TraceID, []pdata.Traces) { | |
td := testdata.GenerateTraceDataOneSpan() | ||
span := td.ResourceSpans().At(0).InstrumentationLibrarySpans().At(0).Spans().At(0) | ||
span.SetTraceID(traceIds[i]) | ||
span.SetSpanID(tracetranslator.UInt64ToByteSpanID(uint64(i + 1))) | ||
|
||
spanID++ | ||
span.SetSpanID(tracetranslator.UInt64ToByteSpanID(uint64(spanID))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. previously this was creating duplicate span ids, now its a monotonic sequence. the other tests don't rely on the spanid it seems |
||
tds = append(tds, td) | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps not for this PR, but in case an error happens, would be nice to have it logged, even if at debug level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah we can do this in a separate pr