Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,9 @@ public void forceKeep(byte samplingMechanism) {
private void forceKeepThisSpan(byte samplingMechanism) {
// if the user really wants to keep this trace chunk, we will let them,
// even if the old sampling priority and mechanism have already propagated
if (SAMPLING_PRIORITY_UPDATER.getAndSet(this, PrioritySampling.USER_KEEP)
== PrioritySampling.UNSET) {
propagationTags.updateTraceSamplingPriority(PrioritySampling.USER_KEEP, samplingMechanism);
}
SAMPLING_PRIORITY_UPDATER.set(this, PrioritySampling.USER_KEEP);
// record force keep decision for future distributed trace propagation
propagationTags.forceKeep(samplingMechanism);
}

public void addPropagatedTraceSource(final int value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public interface Factory {
*/
public abstract void updateTraceSamplingPriority(int samplingPriority, int samplingMechanism);

public abstract void forceKeep(int samplingMechanism);

public abstract int getSamplingPriority();

public abstract void updateTraceOrigin(CharSequence origin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,46 @@ static PTags withError(PTagsFactory factory, String error) {
public void updateTraceSamplingPriority(int samplingPriority, int samplingMechanism) {
if (samplingPriority != PrioritySampling.UNSET && canChangeDecisionMaker
|| samplingMechanism == SamplingMechanism.EXTERNAL_OVERRIDE) {
if (this.samplingPriority != samplingPriority) {
// This should invalidate any cached w3c header
clearCachedHeader(W3C);
doUpdateTraceSamplingPriority(samplingPriority, samplingMechanism);
}
}

@Override
public void forceKeep(int samplingMechanism) {
doUpdateTraceSamplingPriority(PrioritySampling.USER_KEEP, samplingMechanism);
}

private void doUpdateTraceSamplingPriority(int samplingPriority, int samplingMechanism) {
if (this.samplingPriority != samplingPriority) {
// This should invalidate any cached w3c header
clearCachedHeader(W3C);
}
this.samplingPriority = samplingPriority;
if (samplingPriority > 0) {
// TODO should try to keep the old sampling mechanism if we override the value?
if (samplingMechanism == SamplingMechanism.EXTERNAL_OVERRIDE) {
// There is no specific value for the EXTERNAL_OVERRIDE, so say that it's the DEFAULT
samplingMechanism = SamplingMechanism.DEFAULT;
}
this.samplingPriority = samplingPriority;
if (samplingPriority > 0) {
// TODO should try to keep the old sampling mechanism if we override the value?
if (samplingMechanism == SamplingMechanism.EXTERNAL_OVERRIDE) {
// There is no specific value for the EXTERNAL_OVERRIDE, so say that it's the DEFAULT
samplingMechanism = SamplingMechanism.DEFAULT;
}
// Protect against possible SamplingMechanism.UNKNOWN (-1) that doesn't comply with the
// format
if (samplingMechanism >= 0) {
TagValue newDM = TagValue.from("-" + samplingMechanism);
if (!newDM.equals(decisionMakerTagValue)) {
// This should invalidate any cached w3c and datadog header
clearCachedHeader(DATADOG);
clearCachedHeader(W3C);
}
decisionMakerTagValue = newDM;
}
} else {
// Drop the decision maker tag
if (decisionMakerTagValue != null) {
// Protect against possible SamplingMechanism.UNKNOWN (-1) that doesn't comply with the
// format
if (samplingMechanism >= 0) {
TagValue newDM = TagValue.from("-" + samplingMechanism);
if (!newDM.equals(decisionMakerTagValue)) {
// This should invalidate any cached w3c and datadog header
clearCachedHeader(DATADOG);
clearCachedHeader(W3C);
}
decisionMakerTagValue = null;
decisionMakerTagValue = newDM;
}
} else {
// Drop the decision maker tag
if (decisionMakerTagValue != null) {
// This should invalidate any cached w3c and datadog header
clearCachedHeader(DATADOG);
clearCachedHeader(W3C);
}
decisionMakerTagValue = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ class DDSpanContextPropagationTagsTest extends DDCoreSpecification {
dd.createTagMap() == tagMap

where:
priority | header | newHeader | tagMap
UNSET | "_dd.p.usr=123" | "_dd.p.dm=-4,_dd.p.usr=123" | ["_dd.p.dm": "-4", "_dd.p.usr": "123"]
// decision has already been made, propagate as-is
SAMPLER_KEEP | "_dd.p.dm=9bf3439f2f-1,_dd.p.usr=123" | "_dd.p.dm=9bf3439f2f-1,_dd.p.usr=123" | ["_dd.p.dm": "9bf3439f2f-1", "_dd.p.usr": "123"]
SAMPLER_KEEP | "_dd.p.usr=123" | "_dd.p.usr=123" | ["_dd.p.usr": "123"]
priority | header | newHeader | tagMap
UNSET | "_dd.p.usr=123" | "_dd.p.dm=-4,_dd.p.usr=123" | ["_dd.p.dm": "-4", "_dd.p.usr": "123"]
SAMPLER_KEEP | "_dd.p.dm=9bf3439f2f-1,_dd.p.usr=123" | "_dd.p.dm=-4,_dd.p.usr=123" | ["_dd.p.dm": "-4", "_dd.p.usr": "123"]
SAMPLER_KEEP | "_dd.p.usr=123" | "_dd.p.dm=-4,_dd.p.usr=123" | ["_dd.p.dm": "-4", "_dd.p.usr": "123"]
}

def "forceKeep trace PropagationTags #priority #header"() {
Expand All @@ -127,10 +126,9 @@ class DDSpanContextPropagationTagsTest extends DDCoreSpecification {
ddRoot.createTagMap() == rootTagMap

where:
priority | header | rootHeader | rootTagMap
UNSET | "_dd.p.usr=123" | "_dd.p.dm=-4,_dd.p.usr=123" | ["_dd.p.dm": "-4", "_dd.p.usr": "123"]
// decision has already been made, propagate as-is
SAMPLER_KEEP | "_dd.p.dm=9bf3439f2f-1,_dd.p.usr=123" | "_dd.p.dm=9bf3439f2f-1,_dd.p.usr=123" | ["_dd.p.dm": "9bf3439f2f-1", "_dd.p.usr": "123"]
SAMPLER_KEEP | "_dd.p.usr=123" | "_dd.p.usr=123" | ["_dd.p.usr": "123"]
priority | header | rootHeader | rootTagMap
UNSET | "_dd.p.usr=123" | "_dd.p.dm=-4,_dd.p.usr=123" | ["_dd.p.dm": "-4", "_dd.p.usr": "123"]
SAMPLER_KEEP | "_dd.p.dm=9bf3439f2f-1,_dd.p.usr=123" | "_dd.p.dm=-4,_dd.p.usr=123" | ["_dd.p.dm": "-4", "_dd.p.usr": "123"]
SAMPLER_KEEP | "_dd.p.usr=123" | "_dd.p.dm=-4,_dd.p.usr=123" | ["_dd.p.dm": "-4", "_dd.p.usr": "123"]
}
}