Moving resolver in agent

This commit is contained in:
renaudboutet 2017-05-29 16:28:32 +02:00
parent aecaca6309
commit 5e957558e3
7 changed files with 150 additions and 101 deletions

View File

@ -0,0 +1,17 @@
package com.datadoghq.trace.resolver;
/**
* Configuration POJO for the agent
*/
public class AgentTracerConfig extends TracerConfig {
private boolean enableCustomTracing = false;
public boolean isEnableCustomTracing() {
return enableCustomTracing;
}
public void setEnableCustomTracing(boolean enableCustomTracing) {
this.enableCustomTracing = enableCustomTracing;
}
}

View File

@ -0,0 +1,62 @@
package com.datadoghq.trace.resolver;
import java.util.List;
import java.util.ServiceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datadoghq.trace.DDTracer;
import com.datadoghq.trace.integration.DDSpanContextDecorator;
import com.google.auto.service.AutoService;
import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;
import io.opentracing.contrib.tracerresolver.TracerResolver;
import io.opentracing.util.GlobalTracer;
@AutoService(TracerResolver.class)
public class DDTracerResolver extends TracerResolver {
private final static Logger logger = LoggerFactory.getLogger(DDTracerResolver.class);
@Override
protected Tracer resolve() {
logger.info("Creating the Datadog tracer");
//Find a resource file named dd-trace.yml
DDTracer tracer = null;
//Create tracer from resource files
tracer = DDTracerFactory.createFromResources();
//Create decorators from resource files
List<DDSpanContextDecorator> decorators = DDDecoratorsFactory.createFromResources();
for(DDSpanContextDecorator decorator : decorators){
tracer.addDecorator(decorator);
}
return tracer;
}
@SuppressWarnings("static-access")
public static Tracer registerTracer() {
ServiceLoader<TracerResolver> RESOLVERS = ServiceLoader.load(TracerResolver.class);
Tracer tracer = null;
for (TracerResolver value : RESOLVERS) {
tracer = value.resolveTracer();
if (tracer != null) {
break;
}
}
if (tracer == null) {
tracer = NoopTracerFactory.create();
}
GlobalTracer.register(tracer);
return tracer;
}
}

View File

@ -1,12 +1,17 @@
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
@ -17,6 +22,10 @@ public class DDDecoratorsFactory {
public static String DECORATORS_PACKAGE = "com.datadoghq.trace.integration.";
public static final String CONFIG_PATH = "dd-trace-decorators.yaml";
private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
/**
* Create decorators from configuration
*
@ -39,7 +48,7 @@ public class DDDecoratorsFactory {
logger.warn("Cannot create decorator as the class {} is not defined. Provided configuration {}",decoratorConfig);
continue;
}
DDSpanContextDecorator decorator = null;
try{
decorator = (DDSpanContextDecorator) decoratorClass.getConstructor().newInstance();
@ -47,7 +56,7 @@ public class DDDecoratorsFactory {
logger.warn("Cannot create decorator as we could not invoke the default constructor. Provided configuration {}",decoratorConfig);
continue;
}
//Fill with config values
if(decoratorConfig.getMatchingTag()!=null){
decorator.setMatchingTag(decoratorConfig.getMatchingTag());
@ -61,9 +70,29 @@ public class DDDecoratorsFactory {
if(decoratorConfig.getSetValue()!=null){
decorator.setSetValue(decoratorConfig.getSetValue());
}
decorators.add(decorator);
}
return decorators;
}
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);
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;
}
}

View File

@ -4,9 +4,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
/**
*
*/
public class DDSpanDecoratorConfig {
private String type;

View File

@ -1,5 +1,12 @@
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;
import com.datadoghq.trace.DDTracer;
import com.datadoghq.trace.sampling.AllSampler;
import com.datadoghq.trace.sampling.RateSampler;
@ -8,12 +15,20 @@ 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
*/
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
*
@ -52,4 +67,27 @@ 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);
}
if (tracer == null) {
logger.info("No valid configuration file {} found. Loading default tracer.",CONFIG_PATH);
tracer = new DDTracer();
}
return tracer;
}
}

View File

@ -1,94 +0,0 @@
package com.datadoghq.trace.resolver;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.ServiceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datadoghq.trace.DDTracer;
import com.datadoghq.trace.integration.DDSpanContextDecorator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.auto.service.AutoService;
import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;
import io.opentracing.contrib.tracerresolver.TracerResolver;
import io.opentracing.util.GlobalTracer;
@AutoService(TracerResolver.class)
public class DDTracerResolver extends TracerResolver {
private final static Logger logger = LoggerFactory.getLogger(DDTracerResolver.class);
public static final String TRACER_CONFIG = "dd-trace.yaml";
public static final String DECORATORS_CONFIG = "dd-trace-decorators.yaml";
private final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
@Override
protected Tracer resolve() {
logger.info("Creating the Datadog tracer");
//Find a resource file named dd-trace.yml
DDTracer tracer = null;
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Enumeration<URL> iter = classLoader.getResources(TRACER_CONFIG);
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
}
iter = classLoader.getResources(DECORATORS_CONFIG);
while (iter.hasMoreElements()) {
TracerConfig config = objectMapper.readValue(iter.nextElement().openStream(), TracerConfig.class);
//Find decorators
if (config.getDecorators() != null) {
for(DDSpanContextDecorator decorator:DDDecoratorsFactory.create(config.getDecorators())){
tracer.addDecorator(decorator);
}
}
break; // ONLY the closest resource file is taken into account
}
} catch (IOException e) {
logger.error("Could not load tracer configuration file. Loading default tracer.", e);
}
if (tracer == null) {
logger.info("No valid configuration file 'dd-trace.yaml' found. Loading default tracer.");
tracer = new DDTracer();
}
return tracer;
}
@SuppressWarnings("static-access")
public static Tracer registerTracer() {
ServiceLoader<TracerResolver> RESOLVERS = ServiceLoader.load(TracerResolver.class);
Tracer tracer = null;
for (TracerResolver value : RESOLVERS) {
tracer = value.resolveTracer();
if (tracer != null) {
break;
}
}
if (tracer == null) {
tracer = NoopTracerFactory.create();
}
GlobalTracer.register(tracer);
return tracer;
}
}