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

This commit is contained in:
Guillaume Polaert 2017-08-01 19:36:23 +02:00 committed by Emanuele Palazzetti
parent 7430296691
commit 16aba0ac7a
5 changed files with 71 additions and 8 deletions

View File

@ -11,3 +11,5 @@ decorators:
- type: DBTypeDecorator
- type: DBStatementAsResourceName
- type: OperationDecorator
- type: Status404Decorator
- type: URLAsResourceName

View File

@ -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;
}

View File

@ -0,0 +1,16 @@
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");
}
}

View File

@ -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,11 +17,14 @@ public class URLAsResourceName extends AbstractDecorator {
@Override
public boolean afterSetTag(final DDSpanContext context, final String tag, final Object value) {
//Assign resource name
try {
final String path = new java.net.URL(String.valueOf(value)).getPath();
context.setResourceName(path);
} catch (final MalformedURLException e) {
context.setResourceName(String.valueOf(value));
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;
}

View File

@ -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"
}
}