Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dd-java-agent/Readme.md → dd-java-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ Modern web application frameworks such as Dropwizard or Spring Boot are automati
|Spring JDBC| 4.x | Please check the following [JDBC instrumentation](#jdbc-instrumentation) section |
|Hibernate| 5.x | Please check the following [JDBC instrumentation](#jdbc-instrumentation) section |
| MongoDB | 3.x | Intercepts all the calls from the MongoDB client |
| ElasticSearch | 3.x, 5.x | Intercepts all the calls from the ES client |

#### JDBC instrumentation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.datadoghq.trace.resolver;

import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.datadoghq.trace.DDTracer;
import com.datadoghq.trace.sampling.ASampler;
import com.datadoghq.trace.sampling.AllSampler;
Expand All @@ -16,6 +9,11 @@
import com.datadoghq.trace.writer.DDApi;
import com.datadoghq.trace.writer.LoggingWritter;
import com.datadoghq.trace.writer.Writer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.regex.Pattern;

/**
* Create a tracer from a configuration file
Expand All @@ -26,67 +24,80 @@ public class DDTracerFactory {

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
*
*
* @param config
* @return the corresponding tracer
*/
@SuppressWarnings("unchecked")
public static DDTracer create(TracerConfig config){
public static DDTracer create(TracerConfig config) {
String defaultServiceName = config.getDefaultServiceName() != null ? config.getDefaultServiceName() : DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME;

//Create writer
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 writer;

if (config.getWriter() != null) {
WriterConfig c = config.getWriter();
if (DD_AGENT_WRITER_TYPE.equals(c.getType())) {
writer = new DDAgentWriter(new DDApi(c.getHost(DDAgentWriter.DEFAULT_HOSTNAME), c.getPort(DDAgentWriter.DEFAULT_PORT)));
} else if (LOGGING_WRITER_TYPE.equals(c.getType())) {
writer = new LoggingWritter();
} else {
writer = DDTracer.UNASSIGNED_WRITER;
}
} else {
writer = DDTracer.UNASSIGNED_WRITER;

}

//Create sampler
Sampler sampler = 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())) {
Sampler sampler;

if (config.getSampler() != null) {
if (RATE_SAMPLER_TYPE.equals(config.getSampler().getType())) {
sampler = new RateSampler(config.getSampler().getRate());
} else if (ALL_SAMPLER_TYPE.equals(config.getSampler().getType())) {
sampler = new AllSampler();
} else if (type.equals(RateSampler.class.getSimpleName())) {
sampler = new RateSampler((Double) config.getSampler().get("rate"));
} else {
sampler = DDTracer.UNASSIGNED_SAMPLER;
}

//Add sampled tags
Map<String,String> skipTagsPatterns = (Map<String, String>) config.getSampler().get("skipTagsPatterns");
if(skipTagsPatterns!=null && sampler instanceof ASampler){
ASampler aSampler = (ASampler) sampler;
for(Entry<String,String> entry:skipTagsPatterns.entrySet()){
aSampler.addSkipTagPattern(entry.getKey(), Pattern.compile(entry.getValue()));
}

} else {
sampler = DDTracer.UNASSIGNED_SAMPLER;
}

//Add sampled tags
Map<String, String> skipTagsPatterns = config.getSampler().getSkipTagsPatterns();
if (skipTagsPatterns != null && sampler instanceof ASampler) {
ASampler aSampler = (ASampler) sampler;
for (Map.Entry<String, String> entry : skipTagsPatterns.entrySet()) {
aSampler.addSkipTagPattern(entry.getKey(), Pattern.compile(entry.getValue()));
}
}


//Create tracer
return new DDTracer(defaultServiceName, writer, sampler);

}



public static DDTracer createFromResources(){
public static DDTracer createFromResources() {
TracerConfig tracerConfig = FactoryUtils.loadConfigFromResource(CONFIG_PATH, TracerConfig.class);

DDTracer tracer = 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();
}else{
} else {
tracer = DDTracerFactory.create(tracerConfig);
}

return tracer;
}

Expand Down
103 changes: 92 additions & 11 deletions dd-trace/src/main/java/com/datadoghq/trace/resolver/TracerConfig.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
package com.datadoghq.trace.resolver;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import java.util.List;
import java.util.Map;

/**
* Tracer configuration
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class TracerConfig {

private String defaultServiceName;
private Map<String,Object> writer;
private Map<String,Object> sampler;
private WriterConfig writer;
private SamplerConfig sampler;
private List<DDSpanDecoratorConfig> decorators;



public String getDefaultServiceName() {
return defaultServiceName;
}

public void setDefaultServiceName(String defaultServiceName) {
this.defaultServiceName = defaultServiceName;
}
public Map<String, Object> getWriter() {

public WriterConfig getWriter() {
return writer;
}
public void setWriter(Map<String, Object> writer) {

public void setWriter(WriterConfig writer) {
this.writer = writer;
}
public Map<String, Object> getSampler() {

public SamplerConfig getSampler() {
return sampler;
}
public void setSampler(Map<String, Object> sampler) {

public void setSampler(SamplerConfig sampler) {
this.sampler = sampler;
}

public List<DDSpanDecoratorConfig> getDecorators() {
return decorators;
}

public void setDecorators(List<DDSpanDecoratorConfig> decorators) {
this.decorators = decorators;
}

@Override
public String toString() {
try {
Expand All @@ -52,4 +61,76 @@ public String toString() {
return null;
}
}

}

class SamplerConfig {


private Double rate;
private String type;
private Map<String, String> skipTagsPatterns;

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;
}

public Map<String, String> getSkipTagsPatterns() {
return skipTagsPatterns;
}
}


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;
}
}
Original file line number Diff line number Diff line change
@@ -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);



}

}
24 changes: 24 additions & 0 deletions dd-trace/src/test/resources/dd-trace-1.yaml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions dd-trace/src/test/resources/dd-trace-2.yaml
Original file line number Diff line number Diff line change
@@ -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