adding url patterns WIP
MODIFY
dd-trace/src/main/java/com/datadoghq/trace/integration/URLAsResourceName.java
dd-trace/src/main/java/com/datadoghq/trace/integration/URLAsResourceName.java
@@ -2,16 +2,40 @@ package com.datadoghq.trace.integration;
import com.datadoghq.trace.DDSpanContext;
import com.datadoghq.trace.DDTags;
+import com.datadoghq.trace.resolver.DDDecoratorsFactory;
+import com.datadoghq.trace.resolver.FactoryUtils;
+import com.datadoghq.trace.resolver.TracerConfig;
import io.opentracing.tag.Tags;
import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.List;
/** Decorator for servlet contrib */
public class URLAsResourceName extends AbstractDecorator {
+ private List<Config.Rule> patterns = new ArrayList<>();
+ private boolean isConfigured = false;
+
public URLAsResourceName() {
+ this(DDDecoratorsFactory.CONFIG_PATH);
+ }
+
+ public URLAsResourceName(final String configPath) {
+
super();
this.setMatchingTag(Tags.HTTP_URL.getKey());
this.setSetTag(DDTags.RESOURCE_NAME);
+
+ try {
+ final Config config = FactoryUtils.loadConfigFromResource(configPath, Config.class);
+ for (final Config.Rule pattern : config.urlResourcePatterns) {
+ patterns.add(pattern);
+ }
+
+ isConfigured = true;
+ } catch (final Throwable ex) {
+ isConfigured = false;
+ }
}
@Override
@@ -28,4 +52,49 @@ public class URLAsResourceName extends AbstractDecorator {
}
return true;
}
+
+ // Method to normalise the url string
+ String norm(final String origin) {
+
+ // Remove query params and replace integers
+ String norm = origin.replaceAll("\\?.*$", "");
+ norm = norm.replaceAll("\\d+", "<not-alpha>");
+
+ // Apply custom rules
+ if (isConfigured) {
+ for (final Config.Rule p : patterns) {
+ norm = norm.replaceAll(p.regex, p.replacement);
+ }
+ }
+
+ return norm;
+ }
+
+ // for tests
+ List<Config.Rule> getPatterns() {
+ return patterns;
+ }
+
+ void setPatterns(final List<Config.Rule> patterns) {
+ isConfigured = true;
+ this.patterns = patterns;
+ }
+
+ public static class Config extends TracerConfig {
+
+ public List<Rule> urlResourcePatterns;
+
+ public static class Rule {
+
+ public String regex;
+ public String replacement;
+
+ public Rule() {}
+
+ public Rule(String regex, String replacement) {
+ this.regex = regex;
+ this.replacement = replacement;
+ }
+ }
+ }
}

ADD
dd-trace/src/test/groovy/com/datadoghq/trace/integration/URLAsResourceNameTest.groovy
+package com.datadoghq.trace.integration
+
+import spock.lang.Specification
+
+class URLAsResourceNameTest extends Specification {
+
+ def "load the config from the yaml files"() {
+ setup:
+ def patterns = new URLAsResourceName("dd-url-patterns").getPatterns()
+
+ expect:
+ patterns.size() == 2
+ patterns.get(0).regex == ".*"
+ patterns.get(0).replacement == "foo"
+ patterns.get(1).regex == "foo"
+ patterns.get(1).replacement == "bar"
+
+ }
+
+
+ def "should replace all digits"() {
+
+ setup:
+ def decorator = new URLAsResourceName()
+
+ when:
+ def norm = decorator.norm(input)
+
+ then:
+ norm == output
+
+ where:
+ input << ["/aaaa/1111/bbbb/2222"]
+ output << ["/aaaa/<not-alpha>/bbbb/<not-alpha>"]
+
+
+ }
+
+ def "norm should apply custom rules"() {
+
+ setup:
+ def decorator = new URLAsResourceName()
+ def r1 = new URLAsResourceName.Config.Rule(/(\\/users\\/)(.*)\\//, /$1:id\//)
+ decorator.setPatterns(Arrays.asList(r1))
+
+ when:
+ def norm = decorator.norm(input)
+
+ then:
+ norm == output
+
+ where:
+ input << ["http://www.example.com/users/guillaume/list_repository"]
+ output << ["http://www.example.com/users/:id/list_repository"]
+
+
+ }
+}

ADD
dd-trace/src/test/resources/dd-url-patterns.yaml
+decorators:
+ - type: HTTPComponent
+ matchingValue: hello
+ setValue: world
+ - type: HTTPComponent
+ matchingValue: foo
+ setValue: bar
+ - type: URLAsResourceName
+
+# Configuration of the URLAsResourceName
+urlResourcePatterns:
+ - regex: ".*"
+ replacement: "foo"
+ - regex: "foo"
+ replacement: "bar"

adding url patterns WIP