add default rules
MODIFY
dd-trace/src/main/java/com/datadoghq/trace/integration/URLAsResourceName.java
dd-trace/src/main/java/com/datadoghq/trace/integration/URLAsResourceName.java
@@ -9,12 +9,14 @@ import io.opentracing.tag.Tags;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
/** Decorator for servlet contrib */
public class URLAsResourceName extends AbstractDecorator {
+ public static final Config.Rule RULE_QPARAM = new Config.Rule("\\?.*$", "");
+ public static final Config.Rule RULE_DIGIT = new Config.Rule("\\d+", "?");
private List<Config.Rule> patterns = new ArrayList<>();
- private boolean isConfigured = false;
public URLAsResourceName() {
this(DDDecoratorsFactory.CONFIG_PATH);
@@ -31,24 +33,32 @@ public class URLAsResourceName extends AbstractDecorator {
for (final Config.Rule pattern : config.urlResourcePatterns) {
patterns.add(pattern);
}
-
- isConfigured = true;
} catch (final Throwable ex) {
- isConfigured = false;
+ // do nothing
+ } finally {
+ patterns.add(RULE_QPARAM);
+ patterns.add(RULE_DIGIT);
}
}
@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);
+ String path = String.valueOf(value);
+ try {
+ path = new java.net.URL(path).getPath();
} catch (final MalformedURLException e) {
- context.setResourceName(String.valueOf(value));
+ // do nothing
}
+ path = norm(path);
+ final String verb = (String) context.getTags().get(Tags.HTTP_METHOD.getKey());
+ if (verb != null && !verb.isEmpty()) {
+ path = verb + "-" + path;
+ }
+ context.setResourceName(path);
+ } catch (final Throwable e) {
+ // do nothing
}
return true;
}
@@ -57,15 +67,12 @@ public class URLAsResourceName extends AbstractDecorator {
String norm(final String origin) {
// Remove query params and replace integers
- String norm = origin.replaceAll("\\?.*$", "");
- norm = norm.replaceAll("\\d+", "<not-alpha>");
+ String norm = origin;
// Apply custom rules
- if (isConfigured) {
for (final Config.Rule p : patterns) {
norm = norm.replaceAll(p.regex, p.replacement);
}
- }
return norm;
}
@@ -76,7 +83,6 @@ public class URLAsResourceName extends AbstractDecorator {
}
void setPatterns(final List<Config.Rule> patterns) {
- isConfigured = true;
this.patterns = patterns;
}
@@ -91,10 +97,23 @@ public class URLAsResourceName extends AbstractDecorator {
public Rule() {}
- public Rule(String regex, String replacement) {
+ public Rule(final String regex, final String replacement) {
this.regex = regex;
this.replacement = replacement;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Rule rule = (Rule) o;
+ return Objects.equals(regex, rule.regex) && Objects.equals(replacement, rule.replacement);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(regex, replacement);
+ }
}
}
}

MODIFY
dd-trace/src/test/groovy/com/datadoghq/trace/integration/SpanDecoratorTest.groovy
dd-trace/src/test/groovy/com/datadoghq/trace/integration/SpanDecoratorTest.groovy
@@ -123,31 +123,4 @@ class SpanDecoratorTest extends Specification {
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"
- }
}

MODIFY
dd-trace/src/test/groovy/com/datadoghq/trace/integration/URLAsResourceNameTest.groovy
dd-trace/src/test/groovy/com/datadoghq/trace/integration/URLAsResourceNameTest.groovy
@@ -9,15 +9,38 @@ class URLAsResourceNameTest extends Specification {
def patterns = new URLAsResourceName("dd-url-patterns").getPatterns()
expect:
- patterns.size() == 2
+ patterns.size() == 4
+ // 0 and 1 are from the config file
patterns.get(0).regex == ".*"
patterns.get(0).replacement == "foo"
patterns.get(1).regex == "foo"
patterns.get(1).replacement == "bar"
+ // the last and before-last are defaults
+ patterns.get(patterns.size() - 2) == URLAsResourceName.RULE_QPARAM
+ patterns.get(patterns.size() - 1) == URLAsResourceName.RULE_DIGIT
+
}
+ def "remove query params"() {
+
+ setup:
+ def decorator = new URLAsResourceName()
+
+ when:
+ def norm = decorator.norm(input)
+
+ then:
+ norm == output
+
+ where:
+ input << ["http://sdsdfsdfdsfsd?dasdas?das"]
+ output << ["http://sdsdfsdfdsfsd"]
+
+
+ }
+
def "should replace all digits"() {
setup:
@@ -31,7 +54,7 @@ class URLAsResourceNameTest extends Specification {
where:
input << ["/aaaa/1111/bbbb/2222"]
- output << ["/aaaa/<not-alpha>/bbbb/<not-alpha>"]
+ output << ["/aaaa/?/bbbb/?"]
}
@@ -40,7 +63,7 @@ class URLAsResourceNameTest extends Specification {
setup:
def decorator = new URLAsResourceName()
- def r1 = new URLAsResourceName.Config.Rule(/(\\/users\\/)(.*)\\//, /$1:id\//)
+ def r1 = new URLAsResourceName.Config.Rule(/(\\/users\\/)([^\/]*)/, /$1:id/)
decorator.setPatterns(Arrays.asList(r1))
when:
@@ -50,8 +73,8 @@ class URLAsResourceNameTest extends Specification {
norm == output
where:
- input << ["http://www.example.com/users/guillaume/list_repository"]
- output << ["http://www.example.com/users/:id/list_repository"]
+ input << ["http://www.example.com/users/guillaume/list_repository/"]
+ output << ["http://www.example.com/users/:id/list_repository/"]
}

add default rules