Skip to content

[processor/tailsampling] Deprecate invert sample decisions #39833

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -10,6 +10,7 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
)
Expand Down Expand Up @@ -61,9 +62,10 @@ func TestBooleanTagFilterInverted(t *testing.T) {
resAttr["example"] = 8

cases := []struct {
Desc string
Trace *TraceData
Decision Decision
Desc string
Trace *TraceData
Decision Decision
DisableInvertSample bool
}{
{
Desc: "non-matching span attribute",
Expand All @@ -80,10 +82,30 @@ func TestBooleanTagFilterInverted(t *testing.T) {
Trace: newTraceBoolAttrs(empty, "example", true),
Decision: InvertNotSampled,
},
{
Desc: "span attribute with non matching boolean value with DisableInvertSample",
Trace: newTraceBoolAttrs(empty, "example", false),
Decision: Sampled,
DisableInvertSample: true,
},
{
Desc: "span attribute with matching boolean value with DisableInvertSample",
Trace: newTraceBoolAttrs(empty, "example", true),
Decision: NotSampled,
DisableInvertSample: true,
},
}

for _, c := range cases {
t.Run(c.Desc, func(t *testing.T) {
if c.DisableInvertSample {
err := featuregate.GlobalRegistry().Set("processor.tailsamplingprocessor.disableinvertsample", true)
assert.NoError(t, err)
defer func() {
err := featuregate.GlobalRegistry().Set("processor.tailsamplingprocessor.disableinvertsample", false)
assert.NoError(t, err)
}()
}
u, _ := uuid.NewRandom()
decision, err := filter.Evaluate(context.Background(), pcommon.TraceID(u), c.Trace)
assert.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package sampling // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/sampling"

import "go.opentelemetry.io/collector/featuregate"

var disableInvertSample = featuregate.GlobalRegistry().MustRegister(
"processor.tailsamplingprocessor.disableinvertsample",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, sampling policy 'invert_match' will result in a SAMPLED or NOT SAMPLED decision instead of INVERT SAMPLED or INVERT NOT SAMPLED."),
)

func IsInvertSampleDisabled() bool {
return disableInvertSample.IsEnabled()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
)
Expand Down Expand Up @@ -97,9 +98,10 @@ func TestNumericTagFilterInverted(t *testing.T) {
resAttr["example"] = 8

cases := []struct {
Desc string
Trace *TraceData
Decision Decision
Desc string
Trace *TraceData
Decision Decision
DisableInvertSample bool
}{
{
Desc: "nonmatching span attribute",
Expand Down Expand Up @@ -146,10 +148,30 @@ func TestNumericTagFilterInverted(t *testing.T) {
Trace: newTraceIntAttrs(map[string]any{"example": math.MaxInt32 + 1}, "non_matching", math.MaxInt32+1),
Decision: InvertSampled,
},
{
Desc: "nonmatching span attribute with DisableInvertSample",
Trace: newTraceIntAttrs(empty, "non_matching", math.MinInt32),
Decision: Sampled,
DisableInvertSample: true,
},
{
Desc: "span attribute at the lower limit with DisableInvertSample",
Trace: newTraceIntAttrs(empty, "example", math.MinInt32),
Decision: NotSampled,
DisableInvertSample: true,
},
}

for _, c := range cases {
t.Run(c.Desc, func(t *testing.T) {
if c.DisableInvertSample {
err := featuregate.GlobalRegistry().Set("processor.tailsamplingprocessor.disableinvertsample", true)
assert.NoError(t, err)
defer func() {
err := featuregate.GlobalRegistry().Set("processor.tailsamplingprocessor.disableinvertsample", false)
assert.NoError(t, err)
}()
}
u, _ := uuid.NewRandom()
decision, err := filter.Evaluate(context.Background(), pcommon.TraceID(u), c.Trace)
assert.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
)
Expand All @@ -24,10 +25,11 @@ type TestStringAttributeCfg struct {

func TestStringTagFilter(t *testing.T) {
cases := []struct {
Desc string
Trace *TraceData
filterCfg *TestStringAttributeCfg
Decision Decision
Desc string
Trace *TraceData
filterCfg *TestStringAttributeCfg
Decision Decision
DisableInvertSample bool
}{
{
Desc: "nonmatching node attribute key",
Expand Down Expand Up @@ -197,10 +199,32 @@ func TestStringTagFilter(t *testing.T) {
filterCfg: &TestStringAttributeCfg{Key: "example", Values: []string{}, EnabledRegexMatching: true, InvertMatch: true},
Decision: InvertSampled,
},
{
Desc: "invert matching node attribute key with DisableInvertSample",
Trace: newTraceStringAttrs(map[string]any{"example": "value"}, "", ""),
filterCfg: &TestStringAttributeCfg{Key: "example", Values: []string{"value"}, EnabledRegexMatching: false, CacheMaxSize: defaultCacheSize, InvertMatch: true},
Decision: NotSampled,
DisableInvertSample: true,
},
{
Desc: "invert nonmatching node attribute key with DisableInvertSample",
Trace: newTraceStringAttrs(map[string]any{"non_matching": "value"}, "", ""),
filterCfg: &TestStringAttributeCfg{Key: "example", Values: []string{"value"}, EnabledRegexMatching: false, CacheMaxSize: defaultCacheSize, InvertMatch: true},
Decision: Sampled,
DisableInvertSample: true,
},
}

for _, c := range cases {
t.Run(c.Desc, func(t *testing.T) {
if c.DisableInvertSample {
err := featuregate.GlobalRegistry().Set("processor.tailsamplingprocessor.disableinvertsample", true)
assert.NoError(t, err)
defer func() {
err := featuregate.GlobalRegistry().Set("processor.tailsamplingprocessor.disableinvertsample", false)
assert.NoError(t, err)
}()
}
filter := NewStringAttributeFilter(componenttest.NewNopTelemetrySettings(), c.filterCfg.Key, c.filterCfg.Values, c.filterCfg.EnabledRegexMatching, c.filterCfg.CacheMaxSize, c.filterCfg.InvertMatch)
decision, err := filter.Evaluate(context.Background(), pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}), c.Trace)
assert.NoError(t, err)
Expand Down
41 changes: 19 additions & 22 deletions processor/tailsamplingprocessor/internal/sampling/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func hasResourceOrSpanWithCondition(
return Sampled
}

if hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSampleSpan) {
if hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSampleSpan, false) {
return Sampled
}
}
Expand All @@ -37,18 +37,30 @@ func invertHasResourceOrSpanWithCondition(
shouldSampleResource func(resource pcommon.Resource) bool,
shouldSampleSpan func(span ptrace.Span) bool,
) Decision {
isd := IsInvertSampleDisabled()

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

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

if !invertHasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSampleSpan) {
if !hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSampleSpan, true) {
if isd {
return NotSampled
}
return InvertNotSampled
}
}

if isd {
return Sampled
}
return InvertSampled
}

Expand All @@ -57,41 +69,26 @@ func hasSpanWithCondition(td ptrace.Traces, shouldSample func(span ptrace.Span)
for i := 0; i < td.ResourceSpans().Len(); i++ {
rs := td.ResourceSpans().At(i)

if hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSample) {
if hasInstrumentationLibrarySpanWithCondition(rs.ScopeSpans(), shouldSample, false) {
return Sampled
}
}
return NotSampled
}

func hasInstrumentationLibrarySpanWithCondition(ilss ptrace.ScopeSpansSlice, check func(span ptrace.Span) bool) bool {
for i := 0; i < ilss.Len(); i++ {
ils := ilss.At(i)

for j := 0; j < ils.Spans().Len(); j++ {
span := ils.Spans().At(j)

if check(span) {
return true
}
}
}
return false
}

func invertHasInstrumentationLibrarySpanWithCondition(ilss ptrace.ScopeSpansSlice, check func(span ptrace.Span) bool) bool {
func hasInstrumentationLibrarySpanWithCondition(ilss ptrace.ScopeSpansSlice, check func(span ptrace.Span) bool, invert bool) bool {
for i := 0; i < ilss.Len(); i++ {
ils := ilss.At(i)

for j := 0; j < ils.Spans().Len(); j++ {
span := ils.Spans().At(j)

if !check(span) {
return false
if r := check(span); r != invert {
return r
}
}
}
return true
return invert
}

func SetAttrOnScopeSpans(data *TraceData, attrName string, attrKey string) {
Expand Down
Loading