Small bug fixes on core + Apache HTTP Client auto instrumentation on agent
MODIFY
dd-java-agent/dependency-reduced-pom.xml
dd-java-agent/dependency-reduced-pom.xml
</plugins>
</build>
<dependencies>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.5.3</version>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <artifactId>httpcore</artifactId>
+ <groupId>org.apache.httpcomponents</groupId>
+ </exclusion>
+ <exclusion>
+ <artifactId>commons-logging</artifactId>
+ <groupId>commons-logging</groupId>
+ </exclusion>
+ <exclusion>
+ <artifactId>commons-codec</artifactId>
+ <groupId>commons-codec</groupId>
+ </exclusion>
+ </exclusions>
+ </dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>

MODIFY
dd-java-agent/pom.xml
dd-java-agent/pom.xml
</exclusions>
</dependency>
+ <!-- Apache HTTP instrumentation -->
+ <dependency>
+ <groupId>io.opentracing.contrib</groupId>
+ <artifactId>opentracing-apache-httpclient</artifactId>
+ <version>0.0.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.5.3</version>
+ <scope>test</scope>
+ </dependency>
+
<!-- JUnit tests -->
<dependency>
<groupId>io.opentracing</groupId>

MODIFY
dd-java-agent/src/main/resources/otarules.btm
dd-java-agent/src/main/resources/otarules.btm
@@ -15,10 +15,6 @@ ENDRULE
# --------------------------------------------------------
-
-
-
-# --------------------------------------------------------
# Instrument AWS client
RULE AWS SDK Client instrumentation 1
CLASS ^com.amazonaws.client.builder.AwsClientBuilder
@@ -44,3 +40,19 @@ DO
$this.requestHandlers.add(0, requestTracerHandler);
ENDRULE
# --------------------------------------------------------
+
+# Instrument Apache HTTP Client
+RULE Apache HTTP Client instrumentation
+CLASS org.apache.http.impl.client.HttpClientBuilder
+METHOD decorateProtocolExec(org.apache.http.impl.execchain.ClientExecChain)
+HELPER io.opentracing.contrib.agent.OpenTracingHelper
+BIND
+ redirectStrategy: org.apache.http.client.RedirectStrategy = $this.redirectStrategy==null?org.apache.http.impl.client.DefaultRedirectStrategy.INSTANCE:$this.redirectStrategy;
+ decorators: java.util.List = java.util.Collections.singletonList(new io.opentracing.contrib.apache.http.client.ApacheClientSpanDecorator$StandardTags());
+ tracingClientExec:io.opentracing.contrib.apache.http.client.TracingClientExec
+ = new io.opentracing.contrib.apache.http.client.TracingClientExec($1,redirectStrategy,$this.redirectHandlingDisabled,getTracer(),decorators);
+AT ENTRY
+IF TRUE
+DO
+ return tracingClientExec;
+ENDRULE

ADD
dd-java-agent/src/test/java/com/datadoghq/trace/instrument/ApacheHTTPClientTest.java
+package com.datadoghq.trace.instrument;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.opentracing.mock.MockTracer;
+import io.opentracing.util.GlobalTracer;
+
+public class ApacheHTTPClientTest {
+
+ protected MockTracer tracer = new MockTracer();
+
+ @Before
+ public void beforeTest() throws Exception {
+ GlobalTracer.register(tracer);
+ }
+
+ @Test
+ public void test() throws Exception{
+ HttpClient client = HttpClientBuilder.create().build();
+ HttpGet request = new HttpGet("http://apache.org");
+
+ // add request header
+ HttpResponse response = client.execute(request);
+
+ System.out.println("Response Code : "
+ + response.getStatusLine().getStatusCode());
+
+ BufferedReader rd = new BufferedReader(
+ new InputStreamReader(response.getEntity().getContent()));
+
+ StringBuffer result = new StringBuffer();
+ String line = "";
+ while ((line = rd.readLine()) != null) {
+ result.append(line);
+ }
+ System.out.println(result);
+
+ assertThat(tracer.finishedSpans().size()).isEqualTo(2);
+ }
+
+}

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDSpan.java
dd-trace/src/main/java/com/datadoghq/trace/DDSpan.java
@@ -280,12 +280,12 @@ public class DDSpan implements io.opentracing.Span {
@JsonGetter("resource")
public String getResourceName() {
- return context.getResourceName() == null ? context.getOperationName() : context.getResourceName();
+ return context.getResourceName();
}
@JsonGetter("name")
public String getOperationName() {
- return this.context().getOperationName();
+ return context.getOperationName();
}
@JsonIgnore

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
@@ -121,7 +121,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
}
public String getResourceName() {
- return resourceName;
+ return this.resourceName == null || this.resourceName.isEmpty() ? this.operationName : this.resourceName;
}
public boolean getErrorFlag() {

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
@@ -292,6 +292,8 @@ public class DDTracer implements io.opentracing.Tracer {
}
}
+ String operationName = this.operationName != null ? this.operationName : this.resourceName;
+
//this.operationName, this.tags,
// some attributes are inherited from the parent
@@ -300,7 +302,7 @@ public class DDTracer implements io.opentracing.Tracer {
generatedId,
this.parent == null ? 0L : p.getSpanId(),
serviceName,
- this.operationName,
+ operationName,
this.resourceName,
this.parent == null ? new HashMap<String, String>() : p.getBaggageItems(),
errorFlag,

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerResolver.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerResolver.java
@@ -35,7 +35,7 @@ public class DDTracerResolver extends TracerResolver {
private final static Logger logger = LoggerFactory.getLogger(DDTracerResolver.class);
- public static final String TRACER_CONFIG = "src/test/resources/dd-trace.yaml";
+ public static final String TRACER_CONFIG = "dd-trace.yaml";
private final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
@Override

MODIFY
dd-trace/src/test/java/com/datadoghq/trace/DDSpanSerializerTest.java
dd-trace/src/test/java/com/datadoghq/trace/DDSpanSerializerTest.java
@@ -33,7 +33,7 @@ public class DDSpanSerializerTest {
0L,
"service",
"operation",
- "resource",
+ null,
baggage,
false,
"type",
@@ -53,7 +53,7 @@ public class DDSpanSerializerTest {
public void test() throws Exception {
- String expected = "{\"meta\":{\"a-baggage\":\"value\",\"k1\":\"v1\"},\"service\":\"service\",\"error\":0,\"type\":\"type\",\"name\":\"operation\",\"duration\":33000,\"resource\":\"resource\",\"start\":100000,\"span_id\":2,\"parent_id\":0,\"trace_id\":1}";
+ String expected = "{\"meta\":{\"a-baggage\":\"value\",\"k1\":\"v1\"},\"service\":\"service\",\"error\":0,\"type\":\"type\",\"name\":\"operation\",\"duration\":33000,\"resource\":\"operation\",\"start\":100000,\"span_id\":2,\"parent_id\":0,\"trace_id\":1}";
// FIXME At the moment, just compare the string sizes
try {
assertThat(serializer.serialize(span).length()).isEqualTo(expected.length());

Small bug fixes on core + Apache HTTP Client auto instrumentation on agent