Skip to content

Commit 4c25a00

Browse files
jpkrohlingcrobert-1
authored andcommitted
[processor/tailsampling] Duplicate policy names should yield an error (open-telemetry#27017)
Fixes open-telemetry#27016 Signed-off-by: Juraci Paixão Kröhling <[email protected]> --------- Signed-off-by: Juraci Paixão Kröhling <[email protected]> Co-authored-by: Curtis Robert <[email protected]>
1 parent 6f27cf1 commit 4c25a00

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'bug_fix'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: processor/tailsampling
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Prevent the tail-sampling processor from accepting duplicate policy names
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [27016]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

processor/tailsamplingprocessor/processor.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ func newTracesProcessor(ctx context.Context, settings component.TelemetrySetting
7272
return nil, component.ErrNilNextConsumer
7373
}
7474

75-
numDecisionBatches := math.Max(1, cfg.DecisionWait.Seconds())
76-
inBatcher, err := idbatcher.New(uint64(numDecisionBatches), cfg.ExpectedNewTracesPerSec, uint64(2*runtime.NumCPU()))
77-
if err != nil {
78-
return nil, err
79-
}
80-
75+
policyNames := map[string]bool{}
8176
policies := make([]*policy, len(cfg.PolicyCfgs))
8277
for i := range cfg.PolicyCfgs {
8378
policyCfg := &cfg.PolicyCfgs[i]
79+
80+
if policyNames[policyCfg.Name] {
81+
return nil, fmt.Errorf("duplicate policy name %q", policyCfg.Name)
82+
}
83+
policyNames[policyCfg.Name] = true
84+
8485
policyCtx, err := tag.New(ctx, tag.Upsert(tagPolicyKey, policyCfg.Name), tag.Upsert(tagSourceFormat, sourceFormat))
8586
if err != nil {
8687
return nil, err
@@ -97,6 +98,14 @@ func newTracesProcessor(ctx context.Context, settings component.TelemetrySetting
9798
policies[i] = p
9899
}
99100

101+
// this will start a goroutine in the background, so we run it only if everything went
102+
// well in creating the policies
103+
numDecisionBatches := math.Max(1, cfg.DecisionWait.Seconds())
104+
inBatcher, err := idbatcher.New(uint64(numDecisionBatches), cfg.ExpectedNewTracesPerSec, uint64(2*runtime.NumCPU()))
105+
if err != nil {
106+
return nil, err
107+
}
108+
100109
tsp := &tailSamplingSpanProcessor{
101110
ctx: ctx,
102111
nextConsumer: nextConsumer,

processor/tailsamplingprocessor/processor_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,29 @@ func TestPolicyLoggerAddsPolicyName(t *testing.T) {
851851
assert.Equal(t, AlwaysSample, logs.All()[0].ContextMap()["policy"])
852852
}
853853

854+
func TestDuplicatePolicyName(t *testing.T) {
855+
// prepare
856+
set := componenttest.NewNopTelemetrySettings()
857+
msp := new(consumertest.TracesSink)
858+
859+
alwaysSample := sharedPolicyCfg{
860+
Name: "always_sample",
861+
Type: AlwaysSample,
862+
}
863+
864+
_, err := newTracesProcessor(context.Background(), set, msp, Config{
865+
DecisionWait: 500 * time.Millisecond,
866+
NumTraces: uint64(50000),
867+
PolicyCfgs: []PolicyCfg{
868+
{sharedPolicyCfg: alwaysSample},
869+
{sharedPolicyCfg: alwaysSample},
870+
},
871+
})
872+
873+
// verify
874+
assert.Equal(t, err, errors.New(`duplicate policy name "always_sample"`))
875+
}
876+
854877
func collectSpanIds(trace ptrace.Traces) []pcommon.SpanID {
855878
var spanIDs []pcommon.SpanID
856879

0 commit comments

Comments
 (0)