Rename HTTP & DB decorators + seggregate configurtions of decorators in dd-trace-decorators.yaml

This commit is contained in:
renaudboutet 2017-05-24 13:44:35 +02:00
parent ee9e0626df
commit 3f7d852a65
4 changed files with 92 additions and 118 deletions

View File

@ -18,22 +18,4 @@ sampler:
# AllSampler: all spans are reported to the writer # AllSampler: all spans are reported to the writer
# RateSample: only a portion of 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 # - Param 'rate': the portion of spans to keep
type: AllSampler type: AllSampler
# Decorators are used to add extra information to span
# Could be DBServiceDecorator, MapperDecorator or HTTPServiceDecorator
decorators:
- type: HTTP
componentName: java-web-servlet
- type: HTTP
componentName: java-okhttp
desiredServiceName: http-client
- type: DB
componentName: java-mongo
desiredServiceName: mongo
- type: DB
componentName: java-jdbc
desiredServiceName: jdbc
- type: HTTP
componentName: java-aws-sdk
desiredServiceName: aws-client

View File

@ -33,104 +33,112 @@ import io.opentracing.util.GlobalTracer;
@AutoService(TracerResolver.class) @AutoService(TracerResolver.class)
public class DDTracerResolver extends TracerResolver { public class DDTracerResolver extends TracerResolver {
private final static Logger logger = LoggerFactory.getLogger(DDTracerResolver.class); private final static Logger logger = LoggerFactory.getLogger(DDTracerResolver.class);
public static final String TRACER_CONFIG = "dd-trace.yaml"; public static final String TRACER_CONFIG = "dd-trace.yaml";
private final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); public static final String DECORATORS_CONFIG = "dd-trace-decorators.yaml";
@Override private final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
protected Tracer resolve() {
logger.info("Creating the Datadog tracer");
//Find a resource file named dd-trace.yml @Override
DDTracer tracer = null; protected Tracer resolve() {
try { logger.info("Creating the Datadog tracer");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Enumeration<URL> iter = classLoader.getResources(TRACER_CONFIG);
while (iter.hasMoreElements()) {
TracerConfig config = objectMapper.readValue(iter.nextElement().openStream(), TracerConfig.class);
String defaultServiceName = config.getDefaultServiceName() != null ? config.getDefaultServiceName() : DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME; //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);
//Create writer String defaultServiceName = config.getDefaultServiceName() != null ? config.getDefaultServiceName() : DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME;
Writer writer = DDTracer.UNASSIGNED_WRITER;
if (config.getWriter() != null && config.getWriter().get("type") != null) {
String type = (String) config.getWriter().get("type");
if (type.equals(DDAgentWriter.class.getSimpleName())) {
String host = config.getWriter().get("host") != null ? (String) config.getWriter().get("host") : DDAgentWriter.DEFAULT_HOSTNAME;
Integer port = config.getWriter().get("port") != null ? (Integer) config.getWriter().get("port") : DDAgentWriter.DEFAULT_PORT;
DDApi api = new DDApi(host, port);
writer = new DDAgentWriter(api);
} else if (type.equals(LoggingWritter.class.getSimpleName())) {
writer = new LoggingWritter();
}
}
//Create sampler //Create writer
Sampler rateSampler = DDTracer.UNASSIGNED_SAMPLER; Writer writer = DDTracer.UNASSIGNED_WRITER;
if (config.getSampler() != null && config.getSampler().get("type") != null) { if (config.getWriter() != null && config.getWriter().get("type") != null) {
String type = (String) config.getSampler().get("type"); String type = (String) config.getWriter().get("type");
if (type.equals(AllSampler.class.getSimpleName())) { if (type.equals(DDAgentWriter.class.getSimpleName())) {
rateSampler = new AllSampler(); String host = config.getWriter().get("host") != null ? (String) config.getWriter().get("host") : DDAgentWriter.DEFAULT_HOSTNAME;
} else if (type.equals(RateSampler.class.getSimpleName())) { Integer port = config.getWriter().get("port") != null ? (Integer) config.getWriter().get("port") : DDAgentWriter.DEFAULT_PORT;
rateSampler = new RateSampler((Double) config.getSampler().get("rate")); DDApi api = new DDApi(host, port);
} writer = new DDAgentWriter(api);
} } else if (type.equals(LoggingWritter.class.getSimpleName())) {
writer = new LoggingWritter();
}
}
//Create tracer //Create sampler
tracer = new DDTracer(defaultServiceName, writer, rateSampler); Sampler rateSampler = DDTracer.UNASSIGNED_SAMPLER;
if (config.getSampler() != null && config.getSampler().get("type") != null) {
String type = (String) config.getSampler().get("type");
if (type.equals(AllSampler.class.getSimpleName())) {
rateSampler = new AllSampler();
} else if (type.equals(RateSampler.class.getSimpleName())) {
rateSampler = new RateSampler((Double) config.getSampler().get("rate"));
}
}
//Find decorators //Create tracer
if (config.getDecorators() != null) { tracer = new DDTracer(defaultServiceName, writer, rateSampler);
for (Map<String, Object> map : config.getDecorators()) {
if (map.get("type") != null) {
DDSpanContextDecorator decorator = null;
String componentName = (String) map.get("componentName");
String desiredServiceName = (String) map.get("desiredServiceName");
if (map.get("type").equals(HTTP.class.getSimpleName())) { break; // ONLY the closest resource file is taken into account
decorator = new HTTP(componentName, desiredServiceName); }
tracer.addDecorator(decorator);
} else if (map.get("type").equals(DB.class.getSimpleName())) {
decorator = new DB(componentName, desiredServiceName);
tracer.addDecorator(decorator);
}
}
}
}
break; iter = classLoader.getResources(DECORATORS_CONFIG);
} while (iter.hasMoreElements()) {
} catch (IOException e) { TracerConfig config = objectMapper.readValue(iter.nextElement().openStream(), TracerConfig.class);
logger.error("Could not load tracer configuration file. Loading default tracer.", e); //Find decorators
} if (config.getDecorators() != null) {
for (Map<String, Object> map : config.getDecorators()) {
if (map.get("type") != null) {
DDSpanContextDecorator decorator = null;
String componentName = (String) map.get("componentName");
String desiredServiceName = (String) map.get("desiredServiceName");
if (tracer == null) { if (map.get("type").equals(HTTP.class.getSimpleName())) {
logger.info("No valid configuration file 'dd-trace.yaml' found. Loading default tracer."); decorator = new HTTP(componentName, desiredServiceName);
tracer = new DDTracer(); tracer.addDecorator(decorator);
} } else if (map.get("type").equals(DB.class.getSimpleName())) {
decorator = new DB(componentName, desiredServiceName);
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);
}
return tracer; if (tracer == null) {
} logger.info("No valid configuration file 'dd-trace.yaml' found. Loading default tracer.");
tracer = new DDTracer();
}
@SuppressWarnings("static-access") return tracer;
}
@SuppressWarnings("static-access")
public static Tracer registerTracer() { public static Tracer registerTracer() {
ServiceLoader<TracerResolver> RESOLVERS = ServiceLoader.load(TracerResolver.class); ServiceLoader<TracerResolver> RESOLVERS = ServiceLoader.load(TracerResolver.class);
Tracer tracer = null; Tracer tracer = null;
for (TracerResolver value : RESOLVERS) { for (TracerResolver value : RESOLVERS) {
tracer = value.resolveTracer(); tracer = value.resolveTracer();
if (tracer != null) { if (tracer != null) {
break; break;
} }
} }
if (tracer == null) { if (tracer == null) {
tracer = NoopTracerFactory.create(); tracer = NoopTracerFactory.create();
} }
GlobalTracer.register(tracer); GlobalTracer.register(tracer);
return tracer; return tracer;
} }
} }

View File

@ -18,16 +18,4 @@ sampler:
# AllSampler: all spans are reported to the writer # AllSampler: all spans are reported to the writer
# RateSample: only a portion of 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 # - Param 'rate': the portion of spans to keep
type: AllSampler type: AllSampler
# Decorators are used to add extra information to span
# Could be DBServiceDecorator, MapperDecorator or HTTPServiceDecorator
decorators:
# This span decorator leverages HTTP tags such as the URL requested
- type: HTTP
componentName: http
desiredServiceName: unnamed-java-http
# This span decorator leverages DB tags such as the statement requested
- type: DB
componentName: db
desiredServiceName: unnamed-java-db

View File

@ -5,9 +5,5 @@ writer:
port: 10000 port: 10000
sampler: sampler:
type: AllSampler type: AllSampler
decorators:
- type: HTTP
componentName: hello
desiredServiceName: world