@@ -185,37 +185,46 @@ static PTags withError(PTagsFactory factory, String error) {
185185 public void updateTraceSamplingPriority (int samplingPriority , int samplingMechanism ) {
186186 if (samplingPriority != PrioritySampling .UNSET && canChangeDecisionMaker
187187 || samplingMechanism == SamplingMechanism .EXTERNAL_OVERRIDE ) {
188- if (this .samplingPriority != samplingPriority ) {
189- // This should invalidate any cached w3c header
190- clearCachedHeader (W3C );
188+ doUpdateTraceSamplingPriority (samplingPriority , samplingMechanism );
189+ }
190+ }
191+
192+ @ Override
193+ public void forceKeep (int samplingMechanism ) {
194+ doUpdateTraceSamplingPriority (PrioritySampling .USER_KEEP , samplingMechanism );
195+ }
196+
197+ private void doUpdateTraceSamplingPriority (int samplingPriority , int samplingMechanism ) {
198+ if (this .samplingPriority != samplingPriority ) {
199+ // This should invalidate any cached w3c header
200+ clearCachedHeader (W3C );
201+ }
202+ this .samplingPriority = samplingPriority ;
203+ if (samplingPriority > 0 ) {
204+ // TODO should try to keep the old sampling mechanism if we override the value?
205+ if (samplingMechanism == SamplingMechanism .EXTERNAL_OVERRIDE ) {
206+ // There is no specific value for the EXTERNAL_OVERRIDE, so say that it's the DEFAULT
207+ samplingMechanism = SamplingMechanism .DEFAULT ;
191208 }
192- this .samplingPriority = samplingPriority ;
193- if (samplingPriority > 0 ) {
194- // TODO should try to keep the old sampling mechanism if we override the value?
195- if (samplingMechanism == SamplingMechanism .EXTERNAL_OVERRIDE ) {
196- // There is no specific value for the EXTERNAL_OVERRIDE, so say that it's the DEFAULT
197- samplingMechanism = SamplingMechanism .DEFAULT ;
198- }
199- // Protect against possible SamplingMechanism.UNKNOWN (-1) that doesn't comply with the
200- // format
201- if (samplingMechanism >= 0 ) {
202- TagValue newDM = TagValue .from ("-" + samplingMechanism );
203- if (!newDM .equals (decisionMakerTagValue )) {
204- // This should invalidate any cached w3c and datadog header
205- clearCachedHeader (DATADOG );
206- clearCachedHeader (W3C );
207- }
208- decisionMakerTagValue = newDM ;
209- }
210- } else {
211- // Drop the decision maker tag
212- if (decisionMakerTagValue != null ) {
209+ // Protect against possible SamplingMechanism.UNKNOWN (-1) that doesn't comply with the
210+ // format
211+ if (samplingMechanism >= 0 ) {
212+ TagValue newDM = TagValue .from ("-" + samplingMechanism );
213+ if (!newDM .equals (decisionMakerTagValue )) {
213214 // This should invalidate any cached w3c and datadog header
214215 clearCachedHeader (DATADOG );
215216 clearCachedHeader (W3C );
216217 }
217- decisionMakerTagValue = null ;
218+ decisionMakerTagValue = newDM ;
219+ }
220+ } else {
221+ // Drop the decision maker tag
222+ if (decisionMakerTagValue != null ) {
223+ // This should invalidate any cached w3c and datadog header
224+ clearCachedHeader (DATADOG );
225+ clearCachedHeader (W3C );
218226 }
227+ decisionMakerTagValue = null ;
219228 }
220229 }
221230
0 commit comments