Enable custom tracing with annotations over a subset of packages + documentation modified accordingly
MODIFY
dd-java-agent-ittests/pom.xml
dd-java-agent-ittests/pom.xml
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
- <!-- TODO fixit, Due to a Java Heap error, skiping test-->
- <skipTests>false</skipTests>
<includes>
<include>*Test.java</include>
</includes>

MODIFY
dd-java-agent-ittests/src/test/java/io/opentracing/contrib/agent/TraceAnnotationsManagerTest.java
dd-java-agent-ittests/src/test/java/io/opentracing/contrib/agent/TraceAnnotationsManagerTest.java
@@ -12,7 +12,6 @@ public class TraceAnnotationsManagerTest extends AAgentIntegration{
@Before
public void beforeTest() throws Exception {
super.beforeTest();
- TraceAnnotationsManager.loadAnnotationsRules(ClassLoader.getSystemClassLoader());
}
@Test

ADD
dd-java-agent-ittests/src/test/resources/dd-trace.yaml
+# Service name used if none is provided in the app
+defaultServiceName: java-app
+
+# The writer to use.
+# Could be: LoggingWriter or DDAgentWriter (default)
+writer:
+ # LoggingWriter: Spans are logged using the application configuration
+ # DDAgentWriter: Spans are forwarding to a Datadog Agent
+ # - Param 'host': the hostname where the DD Agent running (default: localhost)
+ # - Param 'port': the port to reach the DD Agent (default: 8126)
+ type: DDAgentWriter
+ host: localhost
+ port: 8126
+
+# The sampler to use.
+# Could be: AllSampler (default) or RateSampler
+sampler:
+ # AllSampler: all spans are reported to the writer
+ # RateSample: only a portion of spans are reported to the writer
+ # - Param 'rate': the portion of spans to keep
+ type: AllSampler
+ # Skip some traces if the root span tag values matches some regexp patterns
+ # skipTagsPatterns: {"http.url": ".*/demo/add.*"}
+
+# Enable custom annotation tracing over a selected set of packages
+enableCustomAnnotationTracingOver: ["io.opentracing.contrib.agent"]
+
+# Disable some instrumentations
+# disabledInstrumentations: ["apache http", "mongo", "jetty", "tomcat", ...]
\ No newline at end of file

MODIFY
dd-java-agent/README.md
dd-java-agent/README.md
@@ -77,7 +77,7 @@ sampler:
# skipTagsPatterns: {"http.url": ".*/demo/add.*"}
# Enable custom tracing (Custom annotations for now)
-enableCustomTracing: true
+# enableCustomAnnotationTracingOver: ["io","org","com"]
# Disable some instrumentations
# disabledInstrumentations: ["apache http", "mongo", "jetty", "tomcat", ...]
@@ -171,7 +171,7 @@ public void myMethod() throws InterruptedException{
</dependency>
```
-- Enable custom tracing by adding in the `dd-trace.yaml` config file `enableCustomTracing: true`
+- Enable custom tracing by adding in the `dd-trace.yaml` config file the packages you would like to scan as follow `enableCustomAnnotationTracingOver: ["io","org","com"]`.
If you want to see custom tracing in action please run the [Dropwizard example](https://github.com/DataDog/dd-trace-java/blob/dev/dd-trace-examples/dropwizard-mongo-client/).

MODIFY
dd-java-agent/src/main/java/com/datadoghq/trace/resolver/AgentTracerConfig.java
dd-java-agent/src/main/java/com/datadoghq/trace/resolver/AgentTracerConfig.java
@@ -8,24 +8,24 @@ import java.util.List;
*/
public class AgentTracerConfig extends TracerConfig {
- private boolean enableCustomTracing = false;
-
private List<String> disabledInstrumentations = new ArrayList<String>();
- public List<String> getDisabledInstrumentations() {
- return disabledInstrumentations;
+ private String[] enableCustomAnnotationTracingOver = {"io","com","org"};
+
+ public String[] getEnableCustomAnnotationTracingOver() {
+ return enableCustomAnnotationTracingOver;
}
- public void setDisabledInstrumentations(List<String> uninstallContributions) {
- this.disabledInstrumentations = uninstallContributions;
+ public void setEnableCustomAnnotationTracingOver(String[] enableCustomAnnotationTracingOver) {
+ this.enableCustomAnnotationTracingOver = enableCustomAnnotationTracingOver;
}
- public boolean isEnableCustomTracing() {
- return enableCustomTracing;
+ public List<String> getDisabledInstrumentations() {
+ return disabledInstrumentations;
}
- public void setEnableCustomTracing(boolean enableCustomTracing) {
- this.enableCustomTracing = enableCustomTracing;
+ public void setDisabledInstrumentations(List<String> uninstallContributions) {
+ this.disabledInstrumentations = uninstallContributions;
}
}

MODIFY
dd-java-agent/src/main/java/io/opentracing/contrib/agent/TraceAnnotationsManager.java
dd-java-agent/src/main/java/io/opentracing/contrib/agent/TraceAnnotationsManager.java
@@ -73,8 +73,10 @@ public class TraceAnnotationsManager {
uninstallScripts(loadedScripts,uninstallScripts);
//Check if annotations are enabled
- if(agentTracerConfig != null && agentTracerConfig.isEnableCustomTracing()){
- loadAnnotationsRules(ClassLoader.getSystemClassLoader());
+ if(agentTracerConfig != null
+ && agentTracerConfig.getEnableCustomAnnotationTracingOver()!=null
+ && agentTracerConfig.getEnableCustomAnnotationTracingOver().length>0){
+ loadAnnotationsRules(agentTracerConfig.getEnableCustomAnnotationTracingOver());
}
}
@@ -152,13 +154,16 @@ public class TraceAnnotationsManager {
/**
* Find all the methods annoted with @Trace and inject rules
- *
- * @param classLoader
+ * @param scannedPackages
*/
- public static void loadAnnotationsRules(ClassLoader classLoader) {
+ public static void loadAnnotationsRules(String... scannedPackages) {
+
+ log.info("Looking for annotations over the following packages: "+Arrays.asList(scannedPackages));
+ long cur = System.currentTimeMillis();
+
Reflections reflections = new Reflections(new ConfigurationBuilder()
- .forPackages("/")
- .filterInputsBy(new FilterBuilder().include(".*\\.class"))
+ .forPackages(scannedPackages)
+ .filterInputsBy(new FilterBuilder().includePackage(scannedPackages).include(".*\\.class"))
.setScanners(new MethodAnnotationsScanner()));
Set<Method> methods = reflections.getMethodsAnnotatedWith(Trace.class);
@@ -206,6 +211,8 @@ public class TraceAnnotationsManager {
} catch (Exception e) {
log.log(Level.SEVERE,"Could not install annotation scripts.",e);
}
+
+ log.info("Finished annotation scanning in " + (System.currentTimeMillis() - cur) +" ms. You can accelerate this process by refining the packages you want to scan with `scannedPackages` in the dd-trace.yaml configuration file.");
}
private static RuleScript createRuleScript(

MODIFY
dd-java-agent/src/main/resources/dd-trace.yaml
dd-java-agent/src/main/resources/dd-trace.yaml
@@ -22,8 +22,8 @@ sampler:
# Skip some traces if the root span tag values matches some regexp patterns
# skipTagsPatterns: {"http.url": ".*/demo/add.*"}
-# Enable custom tracing (Custom annotations for now)
-# enableCustomTracing: true
+# Enable custom annotation tracing over a selected set of packages
+# enableCustomAnnotationTracingOver: ["io","org","com"]
# Disable some instrumentations
# disabledInstrumentations: ["apache http", "mongo", "jetty", "tomcat", ...]
\ No newline at end of file

Enable custom tracing with annotations over a subset of packages + documentation modified accordingly