Allow decorators to parse string values on tags.
Since global tags can only be set as String.
MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/decorators/AnalyticsSampleRateDecorator.java
dd-trace-ot/src/main/java/datadog/opentracing/decorators/AnalyticsSampleRateDecorator.java
@@ -13,6 +13,12 @@ public class AnalyticsSampleRateDecorator extends AbstractDecorator {
public boolean shouldSetTag(final DDSpanContext context, final String tag, final Object value) {
if (value instanceof Number) {
context.setMetric(DDTags.ANALYTICS_SAMPLE_RATE, (Number) value);
+ } else if (value instanceof String) {
+ try {
+ context.setMetric(DDTags.ANALYTICS_SAMPLE_RATE, Double.parseDouble((String) value));
+ } catch (final NumberFormatException ex) {
+ // ignore
+ }
}
return false;
}

MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/decorators/ForceManualDropDecorator.java
dd-trace-ot/src/main/java/datadog/opentracing/decorators/ForceManualDropDecorator.java
@@ -18,6 +18,8 @@ public class ForceManualDropDecorator extends AbstractDecorator {
public boolean shouldSetTag(final DDSpanContext context, final String tag, final Object value) {
if (value instanceof Boolean && (boolean) value) {
context.setSamplingPriority(PrioritySampling.USER_DROP);
+ } else if (value instanceof String && Boolean.parseBoolean((String) value)) {
+ context.setSamplingPriority(PrioritySampling.USER_DROP);
}
return false;
}

MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/decorators/ForceManualKeepDecorator.java
dd-trace-ot/src/main/java/datadog/opentracing/decorators/ForceManualKeepDecorator.java
@@ -18,6 +18,8 @@ public class ForceManualKeepDecorator extends AbstractDecorator {
public boolean shouldSetTag(final DDSpanContext context, final String tag, final Object value) {
if (value instanceof Boolean && (boolean) value) {
context.setSamplingPriority(PrioritySampling.USER_KEEP);
+ } else if (value instanceof String && Boolean.parseBoolean((String) value)) {
+ context.setSamplingPriority(PrioritySampling.USER_KEEP);
}
return false;
}

MODIFY
dd-trace-ot/src/test/groovy/datadog/opentracing/decorators/SpanDecoratorTest.groovy
dd-trace-ot/src/test/groovy/datadog/opentracing/decorators/SpanDecoratorTest.groovy
@@ -7,6 +7,7 @@ import datadog.trace.agent.test.utils.ConfigUtils
import datadog.trace.api.Config
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
+import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.common.sampling.AllSampler
import datadog.trace.common.writer.LoggingWriter
import datadog.trace.util.test.DDSpecification
@@ -14,7 +15,7 @@ import io.opentracing.tag.StringTag
import io.opentracing.tag.Tags
import static datadog.trace.api.Config.DEFAULT_SERVICE_NAME
-import static datadog.trace.api.DDTags.EVENT_SAMPLE_RATE
+import static datadog.trace.api.DDTags.ANALYTICS_SAMPLE_RATE
import static java.util.Collections.emptyMap
class SpanDecoratorTest extends DDSpecification {
@@ -229,25 +230,53 @@ class SpanDecoratorTest extends DDSpecification {
span.metrics == [:]
when:
- span.setTag(EVENT_SAMPLE_RATE, rate)
+ span.setTag(ANALYTICS_SAMPLE_RATE, rate)
then:
span.metrics == result
where:
rate | result
- 00 | [(EVENT_SAMPLE_RATE): 0]
- 1 | [(EVENT_SAMPLE_RATE): 1]
- 0f | [(EVENT_SAMPLE_RATE): 0]
- 1f | [(EVENT_SAMPLE_RATE): 1]
- 0.1 | [(EVENT_SAMPLE_RATE): 0.1]
- 1.1 | [(EVENT_SAMPLE_RATE): 1.1]
- -1 | [(EVENT_SAMPLE_RATE): -1]
- 10 | [(EVENT_SAMPLE_RATE): 10]
- "00" | [:]
+ 00 | [(ANALYTICS_SAMPLE_RATE): 0]
+ 1 | [(ANALYTICS_SAMPLE_RATE): 1]
+ 0f | [(ANALYTICS_SAMPLE_RATE): 0]
+ 1f | [(ANALYTICS_SAMPLE_RATE): 1]
+ 0.1 | [(ANALYTICS_SAMPLE_RATE): 0.1]
+ 1.1 | [(ANALYTICS_SAMPLE_RATE): 1.1]
+ -1 | [(ANALYTICS_SAMPLE_RATE): -1]
+ 10 | [(ANALYTICS_SAMPLE_RATE): 10]
+ "00" | [(ANALYTICS_SAMPLE_RATE): 0]
+ "1" | [(ANALYTICS_SAMPLE_RATE): 1]
+ "1.0" | [(ANALYTICS_SAMPLE_RATE): 1]
+ "0" | [(ANALYTICS_SAMPLE_RATE): 0]
+ "0.1" | [(ANALYTICS_SAMPLE_RATE): 0.1]
+ "1.1" | [(ANALYTICS_SAMPLE_RATE): 1.1]
+ "-1" | [(ANALYTICS_SAMPLE_RATE): -1]
"str" | [:]
}
+ def "set priority sampling via tag"() {
+ when:
+ span.setTag(tag, value)
+
+ then:
+ span.samplingPriority == expected
+
+ where:
+ tag | value | expected
+ DDTags.MANUAL_KEEP | true | PrioritySampling.USER_KEEP
+ DDTags.MANUAL_KEEP | false | null
+ DDTags.MANUAL_KEEP | "true" | PrioritySampling.USER_KEEP
+ DDTags.MANUAL_KEEP | "false" | null
+ DDTags.MANUAL_KEEP | "asdf" | null
+
+ DDTags.MANUAL_DROP | true | PrioritySampling.USER_DROP
+ DDTags.MANUAL_DROP | false | null
+ DDTags.MANUAL_DROP | "true" | PrioritySampling.USER_DROP
+ DDTags.MANUAL_DROP | "false" | null
+ DDTags.MANUAL_DROP | "asdf" | null
+ }
+
def "DBStatementAsResource should not interact on Mongo queries"() {
when:
span.setResourceName("not-change-me")

Allow decorators to parse string values on tags.

Since global tags can only be set as String.