Move DBStatementAsResourceName to DBStatementRule
DELETE
dd-trace-ot/src/main/java/datadog/opentracing/decorators/DBStatementAsResourceName.java
-package datadog.opentracing.decorators;
-
-import datadog.opentracing.DDSpanContext;
-import datadog.trace.api.DDTags;
-import io.opentracing.tag.Tags;
-
-public class DBStatementAsResourceName extends AbstractDecorator {
-
- public DBStatementAsResourceName() {
- super();
- this.setMatchingTag(Tags.DB_STATEMENT.getKey());
- this.setReplacementTag(DDTags.RESOURCE_NAME);
- }
-
- @Override
- public boolean shouldSetTag(final DDSpanContext context, final String tag, final Object value) {
-
- // Special case: Mongo
- // Skip the decorators
- if (context.getTags().containsKey(Tags.COMPONENT.getKey())
- && "java-mongo".equals(context.getTags().get(Tags.COMPONENT.getKey()))) {
- return true;
- }
-
- // Assign service name
- if (!super.shouldSetTag(context, tag, value)) {
- // TODO: remove properly the tag (immutable at this time)
- // the `db.statement` tag must be removed because it will be set
- // by the Datadog Trace Agent as `sql.query`; here we're removing
- // a duplicate that will not be obfuscated with the current Datadog
- // Trace Agent version.
- context.setTag(Tags.DB_STATEMENT.getKey(), null);
- return false;
- }
- return true;
- }
-}

MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/decorators/DDDecoratorsFactory.java
dd-trace-ot/src/main/java/datadog/opentracing/decorators/DDDecoratorsFactory.java
@@ -15,7 +15,6 @@ public class DDDecoratorsFactory {
for (final AbstractDecorator decorator :
Arrays.asList(
- new DBStatementAsResourceName(),
new DBTypeDecorator(),
new ForceManualDropDecorator(),
new ForceManualKeepDecorator(),

MODIFY
dd-trace-ot/src/main/java/datadog/trace/common/processor/TraceProcessor.java
dd-trace-ot/src/main/java/datadog/trace/common/processor/TraceProcessor.java
@@ -2,6 +2,8 @@ package datadog.trace.common.processor;
import datadog.opentracing.DDSpan;
import datadog.trace.api.Config;
+import datadog.trace.common.processor.rule.AnalyticsSampleRateRule;
+import datadog.trace.common.processor.rule.DBStatementRule;
import datadog.trace.common.processor.rule.ErrorRule;
import datadog.trace.common.processor.rule.ResourceNameRule;
import datadog.trace.common.processor.rule.SpanTypeRule;
@@ -19,6 +21,7 @@ public class TraceProcessor {
final Rule[] DEFAULT_RULES =
new Rule[] {
// Rules are applied in order.
+ new DBStatementRule(),
new ResourceNameRule(),
new SpanTypeRule(),
new Status5XXRule(),

ADD
dd-trace-ot/src/main/java/datadog/trace/common/processor/rule/DBStatementRule.java
+package datadog.trace.common.processor.rule;
+
+import datadog.opentracing.DDSpan;
+import datadog.trace.common.processor.TraceProcessor;
+import io.opentracing.tag.Tags;
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Converts db.statement tag to resource name. This is later set to sql.query by the datadog agent
+ * after obfuscation.
+ */
+public class DBStatementRule implements TraceProcessor.Rule {
+ @Override
+ public String[] aliases() {
+ return new String[] {"DBStatementAsResourceName"};
+ }
+
+ @Override
+ public void processSpan(
+ final DDSpan span, final Map<String, Object> tags, final Collection<DDSpan> trace) {
+ if (tags.containsKey(Tags.DB_STATEMENT.getKey())) {
+ // Special case: Mongo
+ // Skip the decorators
+ if (tags.containsKey(Tags.COMPONENT.getKey())
+ && "java-mongo".equals(tags.get(Tags.COMPONENT.getKey()))) {
+ return;
+ }
+
+ final String statement = tags.get(Tags.DB_STATEMENT.getKey()).toString();
+ if (!statement.isEmpty()) {
+ span.setResourceName(statement);
+ }
+ span.setTag(Tags.DB_STATEMENT.getKey(), (String) null); // Remove the tag
+ }
+ }
+}

MODIFY
dd-trace-ot/src/test/groovy/datadog/opentracing/decorators/SpanDecoratorTest.groovy
dd-trace-ot/src/test/groovy/datadog/opentracing/decorators/SpanDecoratorTest.groovy
@@ -333,24 +333,18 @@ class SpanDecoratorTest extends DDSpecification {
def "DBStatementAsResource should not interact on Mongo queries"() {
when:
- span.setResourceName("not-change-me")
- Tags.COMPONENT.set(span, "java-mongo")
- Tags.DB_STATEMENT.set(span, something)
-
- then:
- span.getResourceName() == "not-change-me"
-
-
- when:
- span.setResourceName("change-me")
- Tags.COMPONENT.set(span, "other-contrib")
- Tags.DB_STATEMENT.set(span, something)
+ span.setResourceName("existing")
+ Tags.COMPONENT.set(span, component)
+ Tags.DB_STATEMENT.set(span, statement)
+ span.finish()
then:
- span.getResourceName() == something
+ span.getResourceName() == resource
where:
- something = "fake-query"
+ component | statement | resource
+ "java-mongo" | "some-query" | "existing"
+ "other" | "some-query" | "some-query"
}
def "set error flag when error tag reported"() {

Move DBStatementAsResourceName to DBStatementRule