Enable custom tracing in dd-trace.yaml
MODIFY
dd-java-agent/Readme.md
dd-java-agent/Readme.md
@@ -150,7 +150,7 @@ public void myMethod() throws InterruptedException{
</dependency>
```
-- Enable custom tracing by adding this JVM property `-Ddd.enable_custom_tracing`
+- Enable custom tracing by adding in the `dd-trace.yaml` config file `enableCustomTracing: true`
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/pom.xml
dd-java-agent/pom.xml
</excludes>
<forkCount>3</forkCount>
<reuseForks>false</reuseForks>
- <argLine>-Xmx500m -Xms500m -XX:MaxPermSize=256m -Dorg.jboss.byteman.verbose=true -Djavaagent.enableAnnotations
+ <argLine>-Xmx1g -Xms1g -XX:MaxPermSize=512m -Dorg.jboss.byteman.verbose=true
-javaagent:${M2_REPO}/com/datadoghq/dd-java-agent/${project.version}/dd-java-agent-${project.version}.jar</argLine>
<workingDirectory>target/FORK_DIRECTORY_${surefire.forkNumber}</workingDirectory>
</configuration>

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
@@ -14,4 +14,5 @@ public class AgentTracerConfig extends TracerConfig {
public void setEnableCustomTracing(boolean enableCustomTracing) {
this.enableCustomTracing = enableCustomTracing;
}
+
}

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
@@ -17,6 +17,10 @@ import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
+import com.datadoghq.trace.resolver.AgentTracerConfig;
+import com.datadoghq.trace.resolver.DDTracerFactory;
+import com.datadoghq.trace.resolver.FactoryUtils;
+
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
@@ -43,8 +47,11 @@ public class TraceAnnotationsManager extends OpenTracingManager{
OpenTracingManager.initialize(trans);
OpenTracingManager.loadRules(ClassLoader.getSystemClassLoader());
- String value = System.getProperty("dd.enable_custom_tracing","false");
- if("true".equalsIgnoreCase(value)){
+ //Load configuration
+ AgentTracerConfig agentTracerConfig = FactoryUtils.loadConfigFromResource(DDTracerFactory.CONFIG_PATH, AgentTracerConfig.class);
+
+ //Check if annotations are enabled
+ if(agentTracerConfig.isEnableCustomTracing()){
loadRules(ClassLoader.getSystemClassLoader());
}
}

MODIFY
dd-java-agent/src/main/resources/dd-trace.yaml
dd-java-agent/src/main/resources/dd-trace.yaml
@@ -19,3 +19,6 @@ sampler:
# RateSample: only a portion of spans are reported to the writer
# - Param 'rate': the portion of spans to keep
type: AllSampler
+
+# Enable custom tracing (annotations)
+# enableCustomTracing: true
\ No newline at end of file

RENAME
dd-trace/src/test/resources/dd-trace-decorators.yaml
dd-java-agent/src/test/resources/dd-trace-decorators.yaml
RENAME
dd-trace/src/test/resources/dd-trace.yaml
dd-java-agent/src/test/resources/dd-trace.yaml
@@ -5,5 +5,3 @@ writer:
port: 10000
sampler:
type: AllSampler
\ No newline at end of file
-
-
\ No newline at end of file

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDDecoratorsFactory.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDDecoratorsFactory.java
package com.datadoghq.trace.resolver;
-import java.io.IOException;
-import java.net.URL;
import java.util.ArrayList;
-import java.util.Enumeration;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datadoghq.trace.integration.DDSpanContextDecorator;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
/**
* Create DDSpaDecorators from a valid configuration
@@ -24,7 +19,6 @@ public class DDDecoratorsFactory {
public static final String CONFIG_PATH = "dd-trace-decorators.yaml";
- private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
/**
* Create decorators from configuration
@@ -77,22 +71,11 @@ public class DDDecoratorsFactory {
}
public static List<DDSpanContextDecorator> createFromResources(){
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-
List<DDSpanContextDecorator> result = new ArrayList<DDSpanContextDecorator>();
- try{
- Enumeration<URL> iter = classLoader.getResources(CONFIG_PATH);
- while (iter.hasMoreElements()) {
- TracerConfig config = objectMapper.readValue(iter.nextElement().openStream(), TracerConfig.class);
+ TracerConfig config = FactoryUtils.loadConfigFromResource(CONFIG_PATH, TracerConfig.class);
+ if(config!=null){
result = DDDecoratorsFactory.create(config.getDecorators());
-
- break; // ONLY the closest resource file is taken into account
}
- }catch(IOException e){
- logger.error("Could not load decorators configuration file.", e);
- }
-
-
return result;
}
}

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerFactory.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerFactory.java
package com.datadoghq.trace.resolver;
-import java.io.IOException;
-import java.net.URL;
-import java.util.Enumeration;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,8 +11,6 @@ import com.datadoghq.trace.writer.DDAgentWriter;
import com.datadoghq.trace.writer.DDApi;
import com.datadoghq.trace.writer.LoggingWritter;
import com.datadoghq.trace.writer.Writer;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
/**
* Create a tracer from a configuration file
@@ -26,8 +20,6 @@ public class DDTracerFactory {
private final static Logger logger = LoggerFactory.getLogger(DDTracerFactory.class);
public static final String CONFIG_PATH = "dd-trace.yaml";
- private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
-
/**
* Create a tracer from a TracerConfig object
@@ -67,26 +59,19 @@ public class DDTracerFactory {
return new DDTracer(defaultServiceName, writer, rateSampler);
}
- public static DDTracer createFromResources(){
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- DDTracer tracer = null;
- try {
- Enumeration<URL> iter = classLoader.getResources(CONFIG_PATH);
- while (iter.hasMoreElements()) {
- TracerConfig config = objectMapper.readValue(iter.nextElement().openStream(), TracerConfig.class);
- tracer = DDTracerFactory.create(config);
- break; // ONLY the closest resource file is taken into account
- }
- } catch (IOException e) {
- logger.error("Could not load tracer configuration file.", e);
- }
+ public static DDTracer createFromResources(){
+ TracerConfig tracerConfig = FactoryUtils.loadConfigFromResource(CONFIG_PATH, TracerConfig.class);
- if (tracer == null) {
+ DDTracer tracer = null;
+ if (tracerConfig == null) {
logger.info("No valid configuration file {} found. Loading default tracer.",CONFIG_PATH);
tracer = new DDTracer();
+ }else{
+ tracer = DDTracerFactory.create(tracerConfig);
}
+
return tracer;
}

Enable custom tracing in dd-trace.yaml