[servlet] change the resource name for servlet integration (404 grouping)
MODIFY
dd-java-agent/src/main/resources/dd-trace-decorators.yaml
dd-java-agent/src/main/resources/dd-trace-decorators.yaml
@@ -11,3 +11,5 @@ decorators:
- type: DBTypeDecorator
- type: DBStatementAsResourceName
- type: OperationDecorator
+ - type: Status404Decorator
+ - type: URLAsResourceName

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/integration/AbstractDecorator.java
dd-trace/src/main/java/com/datadoghq/trace/integration/AbstractDecorator.java
@@ -11,7 +11,7 @@ public abstract class AbstractDecorator {
private String matchingTag;
- private String matchingValue;
+ private Object matchingValue;
private String setTag;
@@ -45,11 +45,11 @@ public abstract class AbstractDecorator {
this.matchingTag = tag;
}
- public String getMatchingValue() {
+ public Object getMatchingValue() {
return matchingValue;
}
- public void setMatchingValue(final String value) {
+ public void setMatchingValue(final Object value) {
this.matchingValue = value;
}

ADD
dd-trace/src/main/java/com/datadoghq/trace/integration/Status404Decorator.java
+package com.datadoghq.trace.integration;
+
+import com.datadoghq.trace.DDTags;
+import io.opentracing.tag.Tags;
+
+/** This span decorator protect against spam on the resource name */
+public class Status404Decorator extends AbstractDecorator {
+
+ public Status404Decorator() {
+ super();
+ this.setMatchingTag(Tags.HTTP_STATUS.getKey());
+ this.setMatchingValue(404);
+ this.setSetTag(DDTags.RESOURCE_NAME);
+ this.setSetValue("404");
+ }
+}

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/integration/URLAsResourceName.java
dd-trace/src/main/java/com/datadoghq/trace/integration/URLAsResourceName.java
@@ -5,6 +5,7 @@ import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags;
import java.net.MalformedURLException;
+/** Decorator for servlet contrib */
public class URLAsResourceName extends AbstractDecorator {
public URLAsResourceName() {
@@ -16,12 +17,15 @@ public class URLAsResourceName extends AbstractDecorator {
@Override
public boolean afterSetTag(final DDSpanContext context, final String tag, final Object value) {
//Assign resource name
+ if (context.getTags().containsKey(Tags.COMPONENT.getKey())
+ && "java-web-servlet".equals(context.getTags().get(Tags.COMPONENT.getKey()))) {
try {
final String path = new java.net.URL(String.valueOf(value)).getPath();
context.setResourceName(path);
} catch (final MalformedURLException e) {
context.setResourceName(String.valueOf(value));
}
+ }
return true;
}
}

MODIFY
dd-trace/src/test/groovy/com/datadoghq/trace/integration/SpanDecoratorTest.groovy
dd-trace/src/test/groovy/com/datadoghq/trace/integration/SpanDecoratorTest.groovy
@@ -109,4 +109,45 @@ class SpanDecoratorTest extends Specification {
where:
something = "fake-query"
}
+
+ def "set 404 as a resource on a 404 issue"() {
+ setup:
+ def tracer = new DDTracer()
+ def span = SpanFactory.newSpanOf(tracer)
+ tracer.addDecorator(new Status404Decorator())
+
+ when:
+ Tags.HTTP_STATUS.set(span, 404)
+
+ then:
+ span.getResourceName() == "404"
+
+ }
+
+ def "set url as a resource only for the servlet integration"() {
+ setup:
+ def tracer = new DDTracer()
+ def span = SpanFactory.newSpanOf(tracer)
+ tracer.addDecorator(new URLAsResourceName())
+
+ when:
+ span.setResourceName("change-me")
+ Tags.COMPONENT.set(span, "java-web-servlet")
+ Tags.HTTP_URL.set(span, something)
+
+ then:
+ span.getResourceName() == something
+
+ when:
+ span.setResourceName("change-me")
+ Tags.COMPONENT.set(span, "other-contrib")
+ Tags.HTTP_URL.set(span, something)
+
+ then:
+ span.getResourceName() == "change-me"
+
+
+ where:
+ something = "fake"
+ }
}

[servlet] change the resource name for servlet integration (404 grouping)