-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/tailsampling] Adds BooleanAttribute PolicyType (#18764)
- Loading branch information
1 parent
57cfee7
commit 94e4025
Showing
8 changed files
with
196 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: processor/tailsampling | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: adds support for a BooleanAttribute PolicyType | ||
|
||
# One or more tracking issues related to the change | ||
issues: [17545] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: enables use of boolean attrbiutes in defining tail sampling policies |
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
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
63 changes: 63 additions & 0 deletions
63
processor/tailsamplingprocessor/internal/sampling/boolean_tag_filter.go
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sampling // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/sampling" | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type booleanAttributeFilter struct { | ||
key string | ||
value bool | ||
logger *zap.Logger | ||
} | ||
|
||
var _ PolicyEvaluator = (*booleanAttributeFilter)(nil) | ||
|
||
// NewBooleanAttributeFilter creates a policy evaluator that samples all traces with | ||
// the given attribute that match the supplied boolean value. | ||
func NewBooleanAttributeFilter(logger *zap.Logger, key string, value bool) PolicyEvaluator { | ||
return &booleanAttributeFilter{ | ||
key: key, | ||
value: value, | ||
logger: logger, | ||
} | ||
} | ||
|
||
// Evaluate looks at the trace data and returns a corresponding SamplingDecision. | ||
func (baf *booleanAttributeFilter) Evaluate(_ pcommon.TraceID, trace *TraceData) (Decision, error) { | ||
trace.Lock() | ||
batches := trace.ReceivedBatches | ||
trace.Unlock() | ||
|
||
return hasResourceOrSpanWithCondition( | ||
batches, | ||
func(resource pcommon.Resource) bool { | ||
if v, ok := resource.Attributes().Get(baf.key); ok { | ||
value := v.Bool() | ||
return value == baf.value | ||
} | ||
return false | ||
}, | ||
func(span ptrace.Span) bool { | ||
if v, ok := span.Attributes().Get(baf.key); ok { | ||
value := v.Bool() | ||
return value == baf.value | ||
} | ||
return false | ||
}), nil | ||
} |
80 changes: 80 additions & 0 deletions
80
processor/tailsamplingprocessor/internal/sampling/boolean_tag_filter_test.go
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sampling | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestBooleanTagFilter(t *testing.T) { | ||
|
||
var empty = map[string]interface{}{} | ||
filter := NewBooleanAttributeFilter(zap.NewNop(), "example", true) | ||
|
||
resAttr := map[string]interface{}{} | ||
resAttr["example"] = 8 | ||
|
||
cases := []struct { | ||
Desc string | ||
Trace *TraceData | ||
Decision Decision | ||
}{ | ||
{ | ||
Desc: "non-matching span attribute", | ||
Trace: newTraceBoolAttrs(empty, "non_matching", true), | ||
Decision: NotSampled, | ||
}, | ||
{ | ||
Desc: "span attribute with unwanted boolean value", | ||
Trace: newTraceBoolAttrs(empty, "example", false), | ||
Decision: NotSampled, | ||
}, | ||
{ | ||
Desc: "span attribute with wanted boolean value", | ||
Trace: newTraceBoolAttrs(empty, "example", true), | ||
Decision: Sampled, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.Desc, func(t *testing.T) { | ||
u, _ := uuid.NewRandom() | ||
decision, err := filter.Evaluate(pcommon.TraceID(u), c.Trace) | ||
assert.NoError(t, err) | ||
assert.Equal(t, decision, c.Decision) | ||
}) | ||
} | ||
} | ||
|
||
func newTraceBoolAttrs(nodeAttrs map[string]interface{}, spanAttrKey string, spanAttrValue bool) *TraceData { | ||
traces := ptrace.NewTraces() | ||
rs := traces.ResourceSpans().AppendEmpty() | ||
//nolint:errcheck | ||
rs.Resource().Attributes().FromRaw(nodeAttrs) | ||
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.Attributes().PutBool(spanAttrKey, spanAttrValue) | ||
return &TraceData{ | ||
ReceivedBatches: traces, | ||
} | ||
} |
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