Skip to content

Commit 314888a

Browse files
authored
[processor/probabilisticsampler] Fix logs sampling priority mechanism (#38470)
#### Description Fixes #38468. #### Testing Adds testing. #### Documentation ✔️
1 parent 4306fcb commit 314888a

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

.chloggen/logsprioritysampler.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
change_type: bug_fix
2+
component: probabilisticsampler
3+
note: Logs priority sampling behavior applies only when the priority attribute is present.
4+
issues: [38468]
5+
subtext:
6+
change_logs: [user]

processor/probabilisticsamplerprocessor/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ attribute is not configurable, and is called `sampling.priority`.
112112

113113
In logs pipelines, when the priority attribute has value 0, the
114114
configured probability will by modified to 0%, and the item will not
115-
pass the sampler. Otherwise, the logs sampling priority attribute is
115+
pass the sampler. Otherwise, the logs sampling priority attribute is
116116
interpreted as a percentage, with values >= 100 equal to 100%
117-
sampling. The logs sampling priority attribute is configured via
118-
`sampling_priority`.
117+
sampling. The logs sampling priority attribute is configured via
118+
`sampling_priority`. If the logs sampling priority attribute is not
119+
set on a log record, the default sampling percentage applies.
119120

120121
## Mode Selection
121122

processor/probabilisticsamplerprocessor/logsprocessor.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,16 @@ func (lsp *logsProcessor) priorityFunc(logRec plog.LogRecord, rnd randomnessName
240240
// Note: in logs, unlike traces, the sampling priority
241241
// attribute is interpreted as a request to be sampled.
242242
if lsp.samplingPriority != "" {
243-
priorityThreshold := lsp.logRecordToPriorityThreshold(logRec)
244-
245-
if priorityThreshold == sampling.NeverSampleThreshold {
246-
threshold = priorityThreshold
247-
rnd = newSamplingPriorityMethod(rnd.randomness()) // override policy name
248-
} else if sampling.ThresholdLessThan(priorityThreshold, threshold) {
243+
priorityThreshold, has := lsp.logRecordToPriorityThreshold(logRec)
244+
if has {
249245
threshold = priorityThreshold
250246
rnd = newSamplingPriorityMethod(rnd.randomness()) // override policy name
251247
}
252248
}
253249
return rnd, threshold
254250
}
255251

256-
func (lsp *logsProcessor) logRecordToPriorityThreshold(logRec plog.LogRecord) sampling.Threshold {
252+
func (lsp *logsProcessor) logRecordToPriorityThreshold(logRec plog.LogRecord) (sampling.Threshold, bool) {
257253
if localPriority, ok := logRec.Attributes().Get(lsp.samplingPriority); ok {
258254
// Potentially raise the sampling probability to minProb
259255
minProb := 0.0
@@ -266,9 +262,9 @@ func (lsp *logsProcessor) logRecordToPriorityThreshold(logRec plog.LogRecord) sa
266262
if minProb != 0 {
267263
if th, err := sampling.ProbabilityToThresholdWithPrecision(minProb, lsp.precision); err == nil {
268264
// The record has supplied a valid alternative sampling probability
269-
return th
265+
return th, true
270266
}
271267
}
272268
}
273-
return sampling.NeverSampleThreshold
269+
return sampling.NeverSampleThreshold, false
274270
}

processor/probabilisticsamplerprocessor/logsprocessor_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,64 @@ func TestLogsSampling(t *testing.T) {
151151
received: 29, // probabilistic... doesn't yield the same results as foo
152152
},
153153
{
154-
name: "sampling_priority",
154+
name: "sampling_priority_0_record",
155155
cfg: &Config{
156156
SamplingPercentage: 0,
157157
SamplingPriority: "priority",
158+
AttributeSource: recordAttributeSource,
159+
FromAttribute: "rando",
158160
},
159161
received: 25,
160162
},
163+
{
164+
name: "sampling_priority_50_record",
165+
cfg: &Config{
166+
SamplingPercentage: 50,
167+
SamplingPriority: "priority",
168+
AttributeSource: recordAttributeSource,
169+
FromAttribute: "rando",
170+
},
171+
// Expected value is (0.5*75)+25 == 62.5
172+
received: 64,
173+
},
174+
{
175+
name: "sampling_priority_100_record",
176+
cfg: &Config{
177+
SamplingPercentage: 100,
178+
SamplingPriority: "priority",
179+
AttributeSource: recordAttributeSource,
180+
FromAttribute: "rando",
181+
},
182+
received: 100,
183+
},
184+
{
185+
name: "sampling_priority_0_traceid",
186+
cfg: &Config{
187+
SamplingPercentage: 0,
188+
SamplingPriority: "priority",
189+
AttributeSource: traceIDAttributeSource,
190+
},
191+
received: 25,
192+
},
193+
{
194+
name: "sampling_priority_50_traceid",
195+
cfg: &Config{
196+
SamplingPercentage: 50,
197+
SamplingPriority: "priority",
198+
AttributeSource: traceIDAttributeSource,
199+
},
200+
// Expected value is (0.5*75)+25 == 62.5
201+
received: 57,
202+
},
203+
{
204+
name: "sampling_priority_100_traceid",
205+
cfg: &Config{
206+
SamplingPercentage: 100,
207+
SamplingPriority: "priority",
208+
AttributeSource: traceIDAttributeSource,
209+
},
210+
received: 100,
211+
},
161212
{
162213
name: "sampling_priority with sampling field",
163214
cfg: &Config{
@@ -185,6 +236,7 @@ func TestLogsSampling(t *testing.T) {
185236
// encodes historical behavior, we leave it as-is.
186237
traceID := [16]byte{0, 0, 0, 0, 0, 0, 0, 0, ib, ib, ib, ib, ib, ib, ib, ib}
187238
record.SetTraceID(traceID)
239+
record.Attributes().PutInt("rando", int64(computeHash(traceID[:], 123)))
188240
// set half of records with a foo (bytes) and a bar (string) attribute
189241
if i%2 == 0 {
190242
b := record.Attributes().PutEmptyBytes("foo")

processor/probabilisticsamplerprocessor/sampler_mode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ type hashingSampler struct {
236236
// Logs only: name of attribute to obtain randomness
237237
logsRandomnessSourceAttribute string
238238

239-
// Logs only: name of attribute to obtain randomness
239+
// Logs only: whether traceID is being used
240240
logsTraceIDEnabled bool
241241
}
242242

0 commit comments

Comments
 (0)