Refactor the configuration
This commit is contained in:
parent
b3d7f3afc6
commit
54597d3edf
|
@ -1,8 +1,5 @@
|
||||||
package com.datadoghq.trace.resolver;
|
package com.datadoghq.trace.resolver;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.datadoghq.trace.DDTracer;
|
import com.datadoghq.trace.DDTracer;
|
||||||
import com.datadoghq.trace.sampling.AllSampler;
|
import com.datadoghq.trace.sampling.AllSampler;
|
||||||
import com.datadoghq.trace.sampling.RateSampler;
|
import com.datadoghq.trace.sampling.RateSampler;
|
||||||
|
@ -11,6 +8,8 @@ import com.datadoghq.trace.writer.DDAgentWriter;
|
||||||
import com.datadoghq.trace.writer.DDApi;
|
import com.datadoghq.trace.writer.DDApi;
|
||||||
import com.datadoghq.trace.writer.LoggingWritter;
|
import com.datadoghq.trace.writer.LoggingWritter;
|
||||||
import com.datadoghq.trace.writer.Writer;
|
import com.datadoghq.trace.writer.Writer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a tracer from a configuration file
|
* Create a tracer from a configuration file
|
||||||
|
@ -21,57 +20,69 @@ public class DDTracerFactory {
|
||||||
|
|
||||||
public static final String CONFIG_PATH = "dd-trace.yaml";
|
public static final String CONFIG_PATH = "dd-trace.yaml";
|
||||||
|
|
||||||
|
private static final String DD_AGENT_WRITER_TYPE = DDAgentWriter.class.getSimpleName();
|
||||||
|
private static final String LOGGING_WRITER_TYPE = LoggingWritter.class.getSimpleName();
|
||||||
|
private static final String ALL_SAMPLER_TYPE = AllSampler.class.getSimpleName();
|
||||||
|
private static final String RATE_SAMPLER_TYPE = RateSampler.class.getSimpleName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a tracer from a TracerConfig object
|
* Create a tracer from a TracerConfig object
|
||||||
*
|
*
|
||||||
* @param config
|
* @param config
|
||||||
* @return the corresponding tracer
|
* @return the corresponding tracer
|
||||||
*/
|
*/
|
||||||
public static DDTracer create(TracerConfig config){
|
public static DDTracer create(TracerConfig config) {
|
||||||
String defaultServiceName = config.getDefaultServiceName() != null ? config.getDefaultServiceName() : DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME;
|
String defaultServiceName = config.getDefaultServiceName() != null ? config.getDefaultServiceName() : DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME;
|
||||||
|
|
||||||
//Create writer
|
//Create writer
|
||||||
Writer writer = DDTracer.UNASSIGNED_WRITER;
|
Writer writer;
|
||||||
if (config.getWriter() != null && config.getWriter().get("type") != null) {
|
|
||||||
String type = (String) config.getWriter().get("type");
|
if (config.getWriter() != null) {
|
||||||
if (type.equals(DDAgentWriter.class.getSimpleName())) {
|
WriterConfig c = config.getWriter();
|
||||||
String host = config.getWriter().get("host") != null ? (String) config.getWriter().get("host") : DDAgentWriter.DEFAULT_HOSTNAME;
|
if (DD_AGENT_WRITER_TYPE.equals(c.getType())) {
|
||||||
Integer port = config.getWriter().get("port") != null ? (Integer) config.getWriter().get("port") : DDAgentWriter.DEFAULT_PORT;
|
writer = new DDAgentWriter(new DDApi(c.getHost(DDAgentWriter.DEFAULT_HOSTNAME), c.getPort(DDAgentWriter.DEFAULT_PORT)));
|
||||||
DDApi api = new DDApi(host, port);
|
} else if (LOGGING_WRITER_TYPE.equals(c.getType())) {
|
||||||
writer = new DDAgentWriter(api);
|
|
||||||
} else if (type.equals(LoggingWritter.class.getSimpleName())) {
|
|
||||||
writer = new LoggingWritter();
|
writer = new LoggingWritter();
|
||||||
|
} else {
|
||||||
|
writer = DDTracer.UNASSIGNED_WRITER;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
writer = DDTracer.UNASSIGNED_WRITER;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create sampler
|
//Create sampler
|
||||||
Sampler rateSampler = DDTracer.UNASSIGNED_SAMPLER;
|
Sampler sampler;
|
||||||
if (config.getSampler() != null && config.getSampler().get("type") != null) {
|
|
||||||
String type = (String) config.getSampler().get("type");
|
if (config.getSampler() != null) {
|
||||||
if (type.equals(AllSampler.class.getSimpleName())) {
|
if (RATE_SAMPLER_TYPE.equals(config.getSampler().getType())) {
|
||||||
rateSampler = new AllSampler();
|
sampler = new RateSampler(config.getSampler().getRate());
|
||||||
} else if (type.equals(RateSampler.class.getSimpleName())) {
|
} else if (ALL_SAMPLER_TYPE.equals(config.getSampler().getType())) {
|
||||||
rateSampler = new RateSampler((Double) config.getSampler().get("rate"));
|
sampler = new AllSampler();
|
||||||
|
} else {
|
||||||
|
sampler = DDTracer.UNASSIGNED_SAMPLER;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
sampler = DDTracer.UNASSIGNED_SAMPLER;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create tracer
|
//Create tracer
|
||||||
return new DDTracer(defaultServiceName, writer, rateSampler);
|
return new DDTracer(defaultServiceName, writer, sampler);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static DDTracer createFromResources() {
|
||||||
public static DDTracer createFromResources(){
|
|
||||||
TracerConfig tracerConfig = FactoryUtils.loadConfigFromResource(CONFIG_PATH, TracerConfig.class);
|
TracerConfig tracerConfig = FactoryUtils.loadConfigFromResource(CONFIG_PATH, TracerConfig.class);
|
||||||
|
|
||||||
DDTracer tracer = null;
|
DDTracer tracer = null;
|
||||||
if (tracerConfig == null) {
|
if (tracerConfig == null) {
|
||||||
logger.info("No valid configuration file {} found. Loading default tracer.",CONFIG_PATH);
|
logger.info("No valid configuration file {} found. Loading default tracer.", CONFIG_PATH);
|
||||||
tracer = new DDTracer();
|
tracer = new DDTracer();
|
||||||
}else{
|
} else {
|
||||||
tracer = DDTracerFactory.create(tracerConfig);
|
tracer = DDTracerFactory.create(tracerConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tracer;
|
return tracer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,48 +1,56 @@
|
||||||
package com.datadoghq.trace.resolver;
|
package com.datadoghq.trace.resolver;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tracer configuration
|
* Tracer configuration
|
||||||
*/
|
*/
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class TracerConfig {
|
public class TracerConfig {
|
||||||
|
|
||||||
private String defaultServiceName;
|
private String defaultServiceName;
|
||||||
private Map<String,Object> writer;
|
private WriterConfig writer;
|
||||||
private Map<String,Object> sampler;
|
private SamplerConfig sampler;
|
||||||
private List<DDSpanDecoratorConfig> decorators;
|
private List<DDSpanDecoratorConfig> decorators;
|
||||||
|
|
||||||
|
|
||||||
public String getDefaultServiceName() {
|
public String getDefaultServiceName() {
|
||||||
return defaultServiceName;
|
return defaultServiceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDefaultServiceName(String defaultServiceName) {
|
public void setDefaultServiceName(String defaultServiceName) {
|
||||||
this.defaultServiceName = defaultServiceName;
|
this.defaultServiceName = defaultServiceName;
|
||||||
}
|
}
|
||||||
public Map<String, Object> getWriter() {
|
|
||||||
|
public WriterConfig getWriter() {
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
public void setWriter(Map<String, Object> writer) {
|
|
||||||
|
public void setWriter(WriterConfig writer) {
|
||||||
this.writer = writer;
|
this.writer = writer;
|
||||||
}
|
}
|
||||||
public Map<String, Object> getSampler() {
|
|
||||||
|
public SamplerConfig getSampler() {
|
||||||
return sampler;
|
return sampler;
|
||||||
}
|
}
|
||||||
public void setSampler(Map<String, Object> sampler) {
|
|
||||||
|
public void setSampler(SamplerConfig sampler) {
|
||||||
this.sampler = sampler;
|
this.sampler = sampler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<DDSpanDecoratorConfig> getDecorators() {
|
public List<DDSpanDecoratorConfig> getDecorators() {
|
||||||
return decorators;
|
return decorators;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDecorators(List<DDSpanDecoratorConfig> decorators) {
|
public void setDecorators(List<DDSpanDecoratorConfig> decorators) {
|
||||||
this.decorators = decorators;
|
this.decorators = decorators;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
|
@ -52,4 +60,71 @@ public class TracerConfig {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class SamplerConfig {
|
||||||
|
|
||||||
|
|
||||||
|
private Double rate;
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getRate() {
|
||||||
|
return rate;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setRate(Double rate) {
|
||||||
|
this.rate = rate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class WriterConfig {
|
||||||
|
|
||||||
|
|
||||||
|
private String host;
|
||||||
|
private Integer port;
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(Integer port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost(String defaultHostname) {
|
||||||
|
return host == null ? defaultHostname : host;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Integer getPort(int defaultPort) {
|
||||||
|
return port == null ? defaultPort : port;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.datadoghq.trace.resolver;
|
||||||
|
|
||||||
|
import com.datadoghq.trace.sampling.AllSampler;
|
||||||
|
import com.datadoghq.trace.sampling.RateSampler;
|
||||||
|
import com.datadoghq.trace.writer.DDAgentWriter;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Java6Assertions.assertThat;
|
||||||
|
|
||||||
|
public class DDTracerFactoryTest {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() throws Exception {
|
||||||
|
TracerConfig tracerConfig = FactoryUtils.loadConfigFromResource("dd-trace-1.yaml", TracerConfig.class);
|
||||||
|
|
||||||
|
assertThat(tracerConfig.getWriter()).isNotNull();
|
||||||
|
assertThat(tracerConfig.getSampler()).isNotNull();
|
||||||
|
assertThat(tracerConfig.getDefaultServiceName()).isEqualTo("java-app");
|
||||||
|
assertThat(tracerConfig.getWriter().getHost()).isEqualTo("foo");
|
||||||
|
assertThat(tracerConfig.getWriter().getPort()).isEqualTo(123);
|
||||||
|
assertThat(tracerConfig.getWriter().getType()).isEqualTo(DDAgentWriter.class.getSimpleName());
|
||||||
|
assertThat(tracerConfig.getSampler().getType()).isEqualTo(AllSampler.class.getSimpleName());
|
||||||
|
assertThat(tracerConfig.getSampler().getRate()).isNull();
|
||||||
|
|
||||||
|
|
||||||
|
tracerConfig = FactoryUtils.loadConfigFromResource("dd-trace-2.yaml", TracerConfig.class);
|
||||||
|
assertThat(tracerConfig.getWriter()).isNotNull();
|
||||||
|
assertThat(tracerConfig.getDefaultServiceName()).isEqualTo("java-app");
|
||||||
|
assertThat(tracerConfig.getWriter().getHost("localhost")).isEqualTo("localhost");
|
||||||
|
assertThat(tracerConfig.getWriter().getPort(8126)).isEqualTo(8126);
|
||||||
|
assertThat(tracerConfig.getWriter().getType()).isEqualTo(DDAgentWriter.class.getSimpleName());
|
||||||
|
assertThat(tracerConfig.getSampler().getType()).isEqualTo(RateSampler.class.getSimpleName());
|
||||||
|
assertThat(tracerConfig.getSampler().getRate()).isEqualTo(0.4);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
# Service name used if none is provided in the app
|
||||||
|
defaultServiceName: java-app
|
||||||
|
|
||||||
|
# The writer to use.
|
||||||
|
# Could be: LoggingWritter 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: foo
|
||||||
|
port: 123
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# Enable custom tracing (annotations)
|
||||||
|
# enableCustomTracing: true
|
|
@ -0,0 +1,23 @@
|
||||||
|
# Service name used if none is provided in the app
|
||||||
|
defaultServiceName: java-app
|
||||||
|
|
||||||
|
# The writer to use.
|
||||||
|
# Could be: LoggingWritter 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
|
||||||
|
|
||||||
|
# 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: RateSampler
|
||||||
|
rate: 0.4
|
||||||
|
|
||||||
|
# Enable custom tracing (annotations)
|
||||||
|
# enableCustomTracing: true
|
Loading…
Reference in New Issue