Skip to content
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

[chore] Remove usage of MoveAndAppend in tailsamplingprocessor #14622

Merged
merged 1 commit into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestAndEvaluatorNotSampled(t *testing.T) {
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})

trace := &TraceData{
ReceivedBatches: []ptrace.Traces{traces},
ReceivedBatches: traces,
}
decision, err := and.Evaluate(traceID, trace)
require.NoError(t, err, "Failed to evaluate and policy: %v", err)
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestAndEvaluatorSampled(t *testing.T) {
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})

trace := &TraceData{
ReceivedBatches: []ptrace.Traces{traces},
ReceivedBatches: traces,
}
decision, err := and.Evaluate(traceID, trace)
require.NoError(t, err, "Failed to evaluate and policy: %v", err)
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestAndEvaluatorStringInvertSampled(t *testing.T) {
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})

trace := &TraceData{
ReceivedBatches: []ptrace.Traces{traces},
ReceivedBatches: traces,
}
decision, err := and.Evaluate(traceID, trace)
require.NoError(t, err, "Failed to evaluate and policy: %v", err)
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestAndEvaluatorStringInvertNotSampled(t *testing.T) {
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})

trace := &TraceData{
ReceivedBatches: []ptrace.Traces{traces},
ReceivedBatches: traces,
}
decision, err := and.Evaluate(traceID, trace)
require.NoError(t, err, "Failed to evaluate and policy: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,11 @@ func (f FakeTimeProvider) getCurSecond() int64 {
var traceID = pcommon.TraceID([16]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x96, 0x9A, 0x89, 0x55, 0x57, 0x1A, 0x3F})

func createTrace() *TraceData {
trace := &TraceData{SpanCount: atomic.NewInt64(1)}
trace := &TraceData{SpanCount: atomic.NewInt64(1), ReceivedBatches: ptrace.NewTraces()}
return trace
}

func newTraceID() pcommon.TraceID {
r := [16]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x96, 0x9A, 0x89, 0x55, 0x57, 0x1A, 0x3F}
return pcommon.TraceID(r)
}

func newTraceWithKV(traceID pcommon.TraceID, key string, val int64) *TraceData {
var traceBatches []ptrace.Traces
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
ils := rs.ScopeSpans().AppendEmpty()
Expand All @@ -61,9 +55,8 @@ func newTraceWithKV(traceID pcommon.TraceID, key string, val int64) *TraceData {
))
span.Attributes().PutInt(key, val)

traceBatches = append(traceBatches, traces)
return &TraceData{
ReceivedBatches: traceBatches,
ReceivedBatches: traces,
SpanCount: atomic.NewInt64(1),
}
}
Expand Down Expand Up @@ -112,29 +105,26 @@ func TestCompositeEvaluator_OverflowAlwaysSampled(t *testing.T) {
n2 := NewAlwaysSample(zap.NewNop())
c := NewComposite(zap.NewNop(), 3, []SubPolicyEvalParams{{n1, 1}, {n2, 1}}, timeProvider)

trcID := newTraceID()
trace := newTraceWithKV(trcID, "tag", int64(10))
trace := newTraceWithKV(traceID, "tag", int64(10))

decision, err := c.Evaluate(trcID, trace)
decision, err := c.Evaluate(traceID, trace)
require.NoError(t, err, "Failed to evaluate composite policy: %v", err)

// The first policy is NewNumericAttributeFilter and trace tag matches criteria, so the decision should be Sampled.
expected := Sampled
assert.Equal(t, decision, expected)

trcID = newTraceID()
trace = newTraceWithKV(trcID, "tag", int64(11))
trace = newTraceWithKV(traceID, "tag", int64(11))

decision, err = c.Evaluate(trcID, trace)
decision, err = c.Evaluate(traceID, trace)
require.NoError(t, err, "Failed to evaluate composite policy: %v", err)

// The first policy is NewNumericAttributeFilter and trace tag matches criteria, so the decision should be Sampled.
expected = NotSampled
assert.Equal(t, decision, expected)

trcID = newTraceID()
trace = newTraceWithKV(trcID, "tag", int64(1001))
decision, err = c.Evaluate(trcID, trace)
trace = newTraceWithKV(traceID, "tag", int64(1001))
decision, err = c.Evaluate(traceID, trace)
require.NoError(t, err, "Failed to evaluate composite policy: %v", err)

// The first policy fails as the tag value is higher than the range set where as the second policy is AlwaysSample, so the decision should be Sampled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ type spanWithTimeAndDuration struct {
}

func newTraceWithSpans(spans []spanWithTimeAndDuration) *TraceData {
var traceBatches []ptrace.Traces
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
ils := rs.ScopeSpans().AppendEmpty()
Expand All @@ -100,8 +99,7 @@ func newTraceWithSpans(spans []spanWithTimeAndDuration) *TraceData {
span.SetEndTimestamp(pcommon.NewTimestampFromTime(s.StartTime.Add(s.Duration)))
}

traceBatches = append(traceBatches, traces)
return &TraceData{
ReceivedBatches: traceBatches,
ReceivedBatches: traces,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func TestNumericTagFilter(t *testing.T) {
}

func newTraceIntAttrs(nodeAttrs map[string]interface{}, spanAttrKey string, spanAttrValue int64) *TraceData {
var traceBatches []ptrace.Traces
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
rs.Resource().Attributes().FromRaw(nodeAttrs)
Expand All @@ -85,8 +84,7 @@ func newTraceIntAttrs(nodeAttrs map[string]interface{}, spanAttrKey string, span
span.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
span.Attributes().PutInt(spanAttrKey, spanAttrValue)
traceBatches = append(traceBatches, traces)
return &TraceData{
ReceivedBatches: traceBatches,
ReceivedBatches: traces,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type TraceData struct {
// SpanCount track the number of spans on the trace.
SpanCount *atomic.Int64
// ReceivedBatches stores all the batches received for the trace.
ReceivedBatches []ptrace.Traces
ReceivedBatches ptrace.Traces
}

// Decision gives the status of sampling decision.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,12 @@ func TestEvaluate_NumberSpans(t *testing.T) {
}

func newTraceWithMultipleSpans(numberSpans []int32) *TraceData {
var traceBatches []ptrace.Traces
var totalNumberSpans = int32(0)

// For each trace, going to create the number of spans defined in the array
// For each resource, going to create the number of spans defined in the array
traces := ptrace.NewTraces()
for i := range numberSpans {
// Creates trace
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
ils := rs.ScopeSpans().AppendEmpty()

Expand All @@ -111,12 +110,11 @@ func newTraceWithMultipleSpans(numberSpans []int32) *TraceData {
span.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
}
traceBatches = append(traceBatches, traces)
totalNumberSpans += numberSpans[i]
}

return &TraceData{
ReceivedBatches: traceBatches,
ReceivedBatches: traces,
SpanCount: atomic.NewInt64(int64(totalNumberSpans)),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestStatusCodeSampling(t *testing.T) {
}

trace := &TraceData{
ReceivedBatches: []ptrace.Traces{traces},
ReceivedBatches: traces,
}

statusCodeFilter, err := NewStatusCodeFilter(zap.NewNop(), c.StatusCodesToFilterOn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ func BenchmarkStringTagFilterEvaluateRegex(b *testing.B) {
}

func newTraceStringAttrs(nodeAttrs map[string]interface{}, spanAttrKey string, spanAttrValue string) *TraceData {
var traceBatches []ptrace.Traces
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
rs.Resource().Attributes().FromRaw(nodeAttrs)
Expand All @@ -250,8 +249,7 @@ func newTraceStringAttrs(nodeAttrs map[string]interface{}, spanAttrKey string, s
span.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
span.Attributes().PutString(spanAttrKey, spanAttrValue)
traceBatches = append(traceBatches, traces)
return &TraceData{
ReceivedBatches: traceBatches,
ReceivedBatches: traces,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,14 @@ func TestTraceStateFilter(t *testing.T) {
}

func newTraceState(traceState string) *TraceData {
var traceBatches []ptrace.Traces
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
ils := rs.ScopeSpans().AppendEmpty()
span := ils.Spans().AppendEmpty()
span.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
span.TraceState().FromRaw(traceState)
traceBatches = append(traceBatches, traces)
return &TraceData{
ReceivedBatches: traceBatches,
ReceivedBatches: traces,
}
}
58 changes: 23 additions & 35 deletions processor/tailsamplingprocessor/internal/sampling/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@ import (
// hasResourceOrSpanWithCondition iterates through all the resources and instrumentation library spans until any
// callback returns true.
func hasResourceOrSpanWithCondition(
batches []ptrace.Traces,
td ptrace.Traces,
shouldSampleResource func(resource pcommon.Resource) bool,
shouldSampleSpan func(span ptrace.Span) bool,
) Decision {
for _, batch := range batches {
rspans := batch.ResourceSpans()
for i := 0; i < td.ResourceSpans().Len(); i++ {
rs := td.ResourceSpans().At(i)

for i := 0; i < rspans.Len(); i++ {
rs := rspans.At(i)

resource := rs.Resource()
if shouldSampleResource(resource) {
return Sampled
}
resource := rs.Resource()
if shouldSampleResource(resource) {
return Sampled
}

if hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSampleSpan) {
return Sampled
}
if hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSampleSpan) {
return Sampled
}
}
return NotSampled
Expand All @@ -48,40 +44,32 @@ func hasResourceOrSpanWithCondition(
// invertHasResourceOrSpanWithCondition iterates through all the resources and instrumentation library spans until any
// callback returns false.
func invertHasResourceOrSpanWithCondition(
batches []ptrace.Traces,
td ptrace.Traces,
shouldSampleResource func(resource pcommon.Resource) bool,
shouldSampleSpan func(span ptrace.Span) bool,
) Decision {
for _, batch := range batches {
rspans := batch.ResourceSpans()

for i := 0; i < rspans.Len(); i++ {
rs := rspans.At(i)
for i := 0; i < td.ResourceSpans().Len(); i++ {
rs := td.ResourceSpans().At(i)

resource := rs.Resource()
if !shouldSampleResource(resource) {
return InvertNotSampled
}
resource := rs.Resource()
if !shouldSampleResource(resource) {
return InvertNotSampled
}

if !invertHasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSampleSpan) {
return InvertNotSampled
}
if !invertHasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSampleSpan) {
return InvertNotSampled
}
}
return InvertSampled
}

// hasSpanWithCondition iterates through all the instrumentation library spans until any callback returns true.
func hasSpanWithCondition(batches []ptrace.Traces, shouldSample func(span ptrace.Span) bool) Decision {
for _, batch := range batches {
rspans := batch.ResourceSpans()
func hasSpanWithCondition(td ptrace.Traces, shouldSample func(span ptrace.Span) bool) Decision {
for i := 0; i < td.ResourceSpans().Len(); i++ {
rs := td.ResourceSpans().At(i)

for i := 0; i < rspans.Len(); i++ {
rs := rspans.At(i)

if hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSample) {
return Sampled
}
if hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSample) {
return Sampled
}
}
return NotSampled
Expand Down
Loading