@@ -274,6 +274,17 @@ func (c *SpanContext) setSamplingPriority(p int, sampler samplernames.SamplerNam
274274 }
275275}
276276
277+ // forceSetSamplingPriority sets (and forces if the trace is locked) the sampling priority and decision maker (based on `sampler`).
278+ func (c * SpanContext ) forceSetSamplingPriority (p int , sampler samplernames.SamplerName ) {
279+ if c .trace == nil {
280+ c .trace = newTrace ()
281+ }
282+ if c .trace .forceSetSamplingPriority (p , sampler ) {
283+ // the trace's sampling priority or sampler was updated: mark this as updated
284+ c .updated = true
285+ }
286+ }
287+
277288func (c * SpanContext ) SamplingPriority () (p int , ok bool ) {
278289 if c == nil || c .trace == nil {
279290 return 0 , false
@@ -396,6 +407,14 @@ func (t *trace) setSamplingPriority(p int, sampler samplernames.SamplerName) boo
396407 return t .setSamplingPriorityLocked (p , sampler )
397408}
398409
410+ // forceSetSamplingPriority forces the sampling priority and the decision maker
411+ // and returns true if it was modified.
412+ func (t * trace ) forceSetSamplingPriority (p int , sampler samplernames.SamplerName ) bool {
413+ t .mu .Lock ()
414+ defer t .mu .Unlock ()
415+ return t .setSamplingPriorityLockedWithForce (p , sampler , true )
416+ }
417+
399418func (t * trace ) keep () {
400419 atomic .CompareAndSwapUint32 ((* uint32 )(& t .samplingDecision ), uint32 (decisionNone ), uint32 (decisionKeep ))
401420}
@@ -421,8 +440,13 @@ func samplerToDM(sampler samplernames.SamplerName) string {
421440 return "-" + strconv .Itoa (int (sampler ))
422441}
423442
424- func (t * trace ) setSamplingPriorityLocked (p int , sampler samplernames.SamplerName ) bool {
425- if t .locked {
443+ // setSamplingPriority sets the sampling priority and the decision maker
444+ // and returns true if it was modified.
445+ //
446+ // The force parameter is used to bypass the locked sampling decision check
447+ // when setting the sampling priority. This is used to apply a manual keep or drop decision.
448+ func (t * trace ) setSamplingPriorityLockedWithForce (p int , sampler samplernames.SamplerName , force bool ) bool {
449+ if t .locked && ! force {
426450 return false
427451 }
428452
@@ -455,6 +479,10 @@ func (t *trace) setSamplingPriorityLocked(p int, sampler samplernames.SamplerNam
455479 return updatedPriority
456480}
457481
482+ func (t * trace ) setSamplingPriorityLocked (p int , sampler samplernames.SamplerName ) bool {
483+ return t .setSamplingPriorityLockedWithForce (p , sampler , false )
484+ }
485+
458486func (t * trace ) isLocked () bool {
459487 t .mu .RLock ()
460488 defer t .mu .RUnlock ()
0 commit comments