Add configuration file by system property
MODIFY
dd-java-agent/README.md
dd-java-agent/README.md
@@ -49,9 +49,10 @@ That's it! If you did this properly the agent was executed at pre-main, had dete
## Configuration
Configuration is done through a default `dd-trace.yaml` file as a resource in the classpath.
+You can also override it by adding the file path as a system property when launching the JVM: `-Ddd.trace.configurationFile`.
```yaml
-# Service name used if none is provided in the app
+# Main service name for the app
defaultServiceName: java-app
# The writer to use.
@@ -82,8 +83,6 @@ enableCustomTracing: true
# disabledInstrumentations: ["apache http", "mongo", "jetty", "tomcat", ...]
```
-If you want to change it, you must create it in your project.
-
## Instrumented frameworks
When attached to an application the `dd-java-agent` automatically instruments the following set of frameworks & servers:

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
@@ -58,7 +58,7 @@ public class TraceAnnotationsManager {
public static void initialize(Retransformer trans) throws Exception {
transformer = trans;
//Load configuration
- AgentTracerConfig agentTracerConfig = FactoryUtils.loadConfigFromResource(DDTracerFactory.CONFIG_PATH, AgentTracerConfig.class);
+ AgentTracerConfig agentTracerConfig = FactoryUtils.loadConfigFromFilePropertyOrResource(DDTracerFactory.SYSTEM_PROPERTY_CONFIG_PATH,DDTracerFactory.CONFIG_PATH, AgentTracerConfig.class);
List<String> loadedScripts = loadRules(ClassLoader.getSystemClassLoader());

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerFactory.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerFactory.java
@@ -22,6 +22,7 @@ public class DDTracerFactory {
private final static Logger logger = LoggerFactory.getLogger(DDTracerFactory.class);
+ public static final String SYSTEM_PROPERTY_CONFIG_PATH = "dd.trace.configurationFile";
public static final String CONFIG_PATH = "dd-trace.yaml";
private static final String DD_AGENT_WRITER_TYPE = DDAgentWriter.class.getSimpleName();
@@ -87,8 +88,8 @@ public class DDTracerFactory {
}
- public static DDTracer createFromResources() {
- TracerConfig tracerConfig = FactoryUtils.loadConfigFromResource(CONFIG_PATH, TracerConfig.class);
+ public static DDTracer createFromConfigurationFile() {
+ TracerConfig tracerConfig = FactoryUtils.loadConfigFromFilePropertyOrResource(SYSTEM_PROPERTY_CONFIG_PATH,CONFIG_PATH, TracerConfig.class);
DDTracer tracer = null;
if (tracerConfig == null) {

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerResolver.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerResolver.java
@@ -28,7 +28,7 @@ public class DDTracerResolver extends TracerResolver {
//Find a resource file named dd-trace.yml
DDTracer tracer = null;
//Create tracer from resource files
- tracer = DDTracerFactory.createFromResources();
+ tracer = DDTracerFactory.createFromConfigurationFile();
//Create decorators from resource files
List<DDSpanContextDecorator> decorators = DDDecoratorsFactory.createFromResources();

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/FactoryUtils.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/FactoryUtils.java
package com.datadoghq.trace.resolver;
+import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
@@ -15,6 +16,20 @@ public class FactoryUtils {
private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
+ public static <A> A loadConfigFromFilePropertyOrResource(String systemProperty, String resourceName, Class<A> targetClass){
+ String filePath = System.getProperty(systemProperty);
+ if(filePath!=null){
+ try {
+ return objectMapper.readValue(new File(filePath), targetClass);
+ } catch (Exception e) {
+ logger.error("Cannot read provided configuration file "+ filePath +". Using the default one.", e);
+ }
+ }
+
+ return loadConfigFromResource(resourceName,targetClass);
+
+ }
+
public static <A> A loadConfigFromResource(String resourceName, Class<A> targetClass){
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
A config = null;

Add configuration file by system property