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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class InstrumentedClass {
// 2. If using the Java Agent (-javaagent;/path/to/agent.jar), do not instantiate the GlobalTracer; the Agent instantiates it for you
Tracer tracer = io.opentracing.util.GlobalTracer.get();

Span span = tracer.buildSpan("operation-name").startActive();
Span span = tracer.buildSpan("operation-name").startActive(true);
span.setTag(DDTags.SERVICE_NAME, "my-new-service");

// The code you're tracing
Expand All @@ -207,7 +207,7 @@ class InstrumentedClass {
void method0() {
Tracer tracer = io.opentracing.util.GlobalTracer.get();

try (ActiveSpan span = tracer.buildSpan("operation-name").startActive()) {
try (ActiveSpan span = tracer.buildSpan("operation-name").startActive(true)) {
span.setTag(DDTags.SERVICE_NAME, "my-new-service");
Thread.sleep(1000);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.agent.integration

import io.opentracing.ActiveSpan
import io.opentracing.Scope
import io.opentracing.SpanContext
import io.opentracing.propagation.Format
import io.opentracing.propagation.TextMap
Expand Down Expand Up @@ -53,12 +53,12 @@ class TestHttpServer {
final SpanContext extractedContext =
GlobalTracer.get()
.extract(Format.Builtin.HTTP_HEADERS, new RatpackResponseAdapter(context))
ActiveSpan span =
Scope scope =
GlobalTracer.get()
.buildSpan("test-http-server")
.asChildOf(extractedContext)
.startActive()
span.deactivate()
.startActive(true)
scope.close()
}

response.status(200).send(msg)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.agent.integration.httpclient

import datadog.opentracing.DDBaseSpan
import datadog.opentracing.DDSpan
import datadog.opentracing.DDTracer
import datadog.trace.agent.integration.TestHttpServer
import datadog.trace.agent.test.TestUtils
Expand Down Expand Up @@ -53,18 +53,18 @@ class ApacheHttpClientTest extends Specification {
expect:
// one trace on the server, one trace on the client
writer.size() == 2
final List<DDBaseSpan<?>> serverTrace = writer.get(0)
final List<DDSpan> serverTrace = writer.get(0)
serverTrace.size() == 1

final List<DDBaseSpan<?>> clientTrace = writer.get(1)
final List<DDSpan> clientTrace = writer.get(1)
clientTrace.size() == 3
clientTrace.get(0).getOperationName() == "someTrace"
// our instrumentation makes 2 spans for apache-httpclient
final DDBaseSpan<?> localSpan = clientTrace.get(1)
final DDSpan localSpan = clientTrace.get(1)
localSpan.getTags()[Tags.COMPONENT.getKey()] == "apache-httpclient"
localSpan.getOperationName() == "GET"

final DDBaseSpan<?> clientSpan = clientTrace.get(2)
final DDSpan clientSpan = clientTrace.get(2)
clientSpan.getOperationName() == "GET"
clientSpan.getTags()[Tags.HTTP_METHOD.getKey()] == "GET"
clientSpan.getTags()[Tags.HTTP_STATUS.getKey()] == 200
Expand Down Expand Up @@ -100,15 +100,15 @@ class ApacheHttpClientTest extends Specification {
expect:
// only one trace (client).
writer.size() == 1
final List<DDBaseSpan<?>> clientTrace = writer.get(0)
final List<DDSpan> clientTrace = writer.get(0)
clientTrace.size() == 3
clientTrace.get(0).getOperationName() == "someTrace"
// our instrumentation makes 2 spans for apache-httpclient
final DDBaseSpan<?> localSpan = clientTrace.get(1)
final DDSpan localSpan = clientTrace.get(1)
localSpan.getTags()[Tags.COMPONENT.getKey()] == "apache-httpclient"
localSpan.getOperationName() == "GET"

final DDBaseSpan<?> clientSpan = clientTrace.get(2)
final DDSpan clientSpan = clientTrace.get(2)
clientSpan.getOperationName() == "GET"
clientSpan.getTags()[Tags.HTTP_METHOD.getKey()] == "GET"
clientSpan.getTags()[Tags.HTTP_STATUS.getKey()] == 200
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.agent.integration.servlet

import datadog.opentracing.DDBaseSpan
import datadog.opentracing.DDSpan
import datadog.opentracing.DDTracer
import datadog.trace.common.writer.ListWriter
import io.opentracing.util.GlobalTracer
Expand Down Expand Up @@ -43,7 +43,7 @@ class JettyServletTest extends Specification {

ListWriter writer = new ListWriter() {
@Override
void write(final List<DDBaseSpan<?>> trace) {
void write(final List<DDSpan> trace) {
add(trace)
JettyServletTest.latch.countDown()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TomcatServletTest extends Specification {
}
appContext = tomcatServer.addWebapp("", applicationDir.getAbsolutePath())
// Speed up startup by disabling jar scanning:
appContext.getJarScanner().setJarScanFilter(new JarScanFilter(){
appContext.getJarScanner().setJarScanFilter(new JarScanFilter() {
@Override
boolean check(JarScanType jarScanType, String jarName) {
return false
Expand Down Expand Up @@ -147,9 +147,9 @@ class TomcatServletTest extends Specification {
span.context().tags.size() == 11

where:
path | expectedResponse
path | expectedResponse
//"async" | "Hello Async" // FIXME: I can't seem get the async error handler to trigger
"sync" | "Hello Sync"
"sync" | "Hello Sync"
}

private static int randomOpenPort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import datadog.opentracing.DDBaseSpan;
import datadog.opentracing.DDSpan;
import datadog.opentracing.DDTracer;
import datadog.opentracing.decorators.ErrorFlag;
import datadog.trace.agent.test.SayTracedHello;
Expand Down Expand Up @@ -73,7 +73,7 @@ public void testExceptionExit() {
final StringWriter errorString = new StringWriter();
error.printStackTrace(new PrintWriter(errorString));

final DDBaseSpan<?> span = writer.firstTrace().get(0);
final DDSpan span = writer.firstTrace().get(0);
assertThat(span.getOperationName()).isEqualTo("ERROR");
assertThat(span.getTags().get("error")).isEqualTo(true);
assertThat(span.getTags().get("error.msg")).isEqualTo(error.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.mongodb.async.client.MongoClient;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoDatabase;
import datadog.opentracing.DDBaseSpan;
import datadog.opentracing.DDSpan;
import datadog.opentracing.DDTracer;
import datadog.trace.agent.test.TestUtils;
import datadog.trace.common.writer.ListWriter;
Expand Down Expand Up @@ -103,7 +103,7 @@ public void onResult(final Long result, final Throwable t) {

final String createCollectionQuery =
"{ \"create\" : \"asyncCollection\", \"autoIndexId\" : \"?\", \"capped\" : \"?\" }";
final DDBaseSpan<?> trace0 = writer.get(0).get(0);
final DDSpan trace0 = writer.get(0).get(0);
Assert.assertEquals("mongo.query", trace0.getOperationName());
Assert.assertEquals(createCollectionQuery, trace0.getResourceName());
Assert.assertEquals("mongodb", trace0.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import datadog.opentracing.DDBaseSpan;
import datadog.opentracing.DDSpan;
import datadog.opentracing.DDTracer;
import datadog.trace.agent.test.TestUtils;
import datadog.trace.common.writer.ListWriter;
Expand Down Expand Up @@ -100,7 +100,7 @@ public void insertOperation() throws UnknownHostException {

final String createCollectionQuery =
"{ \"create\" : \"testCollection\", \"autoIndexId\" : \"?\", \"capped\" : \"?\" }";
final DDBaseSpan<?> trace0 = writer.get(0).get(0);
final DDSpan trace0 = writer.get(0).get(0);
Assert.assertEquals("mongo.query", trace0.getOperationName());
Assert.assertEquals(createCollectionQuery, trace0.getResourceName());
Assert.assertEquals("mongodb", trace0.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ public class SayTracedHello {

@Trace
public static String sayHello() {
new StringTag(DDTags.SERVICE_NAME).set(GlobalTracer.get().activeSpan(), "test");
new StringTag(DDTags.SERVICE_NAME)
.set(GlobalTracer.get().scopeManager().active().span(), "test");
return "hello!";
}

@Trace(operationName = "SAY_HA")
public static String sayHA() {
new StringTag(DDTags.SERVICE_NAME).set(GlobalTracer.get().activeSpan(), "test");
new StringTag(DDTags.SPAN_TYPE).set(GlobalTracer.get().activeSpan(), "DB");
new StringTag(DDTags.SERVICE_NAME)
.set(GlobalTracer.get().scopeManager().active().span(), "test");
new StringTag(DDTags.SPAN_TYPE).set(GlobalTracer.get().scopeManager().active().span(), "DB");
return "HA!!";
}

@Trace(operationName = "NEW_TRACE")
public static String sayHELLOsayHA() {
new StringTag(DDTags.SERVICE_NAME).set(GlobalTracer.get().activeSpan(), "test2");
new StringTag(DDTags.SERVICE_NAME)
.set(GlobalTracer.get().scopeManager().active().span(), "test2");
return sayHello() + sayHA();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.trace.instrumentation.apachehttpclient;

import io.opentracing.ActiveSpan;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
Expand Down Expand Up @@ -48,10 +49,10 @@ public class DDTracingClientExec implements ClientExecChain {
private final Tracer tracer;

public DDTracingClientExec(
ClientExecChain clientExecChain,
RedirectStrategy redirectStrategy,
boolean redirectHandlingDisabled,
Tracer tracer) {
final ClientExecChain clientExecChain,
final RedirectStrategy redirectStrategy,
final boolean redirectHandlingDisabled,
final Tracer tracer) {
this.requestExecutor = clientExecChain;
this.redirectStrategy = redirectStrategy;
this.redirectHandlingDisabled = redirectHandlingDisabled;
Expand All @@ -60,30 +61,30 @@ public DDTracingClientExec(

@Override
public CloseableHttpResponse execute(
HttpRoute route,
HttpRequestWrapper request,
HttpClientContext clientContext,
HttpExecutionAware execAware)
final HttpRoute route,
final HttpRequestWrapper request,
final HttpClientContext clientContext,
final HttpExecutionAware execAware)
throws IOException, HttpException {

ActiveSpan localSpan = clientContext.getAttribute(ACTIVE_SPAN, ActiveSpan.class);
Scope localScope = clientContext.getAttribute(ACTIVE_SPAN, Scope.class);
CloseableHttpResponse response = null;
try {
if (localSpan == null) {
localSpan = createLocalSpan(request, clientContext);
if (localScope == null) {
localScope = createLocalScope(request, clientContext);
}

return (response = createNetworkSpan(localSpan, route, request, clientContext, execAware));
} catch (Exception e) {
localSpan.deactivate();
return (response = createNetworkSpan(localScope, route, request, clientContext, execAware));
} catch (final Exception e) {
localScope.close();
throw e;
} finally {
if (response != null) {
/**
* This exec runs after {@link org.apache.http.impl.execchain.RedirectExec} which loops
* until there is no redirect or reaches max redirect count. {@link RedirectStrategy} is
* used to decide whether localSpan should be finished or not. If there is a redirect
* localSpan is not finished and redirect is logged.
* used to decide whether localScope should be finished or not. If there is a redirect
* localScope is not finished and redirect is logged.
*/
Integer redirectCount = clientContext.getAttribute(REDIRECT_COUNT, Integer.class);
if (!redirectHandlingDisabled
Expand All @@ -92,37 +93,40 @@ public CloseableHttpResponse execute(
&& ++redirectCount < clientContext.getRequestConfig().getMaxRedirects()) {
clientContext.setAttribute(REDIRECT_COUNT, redirectCount);
} else {
localSpan.deactivate();
localScope.close();
}
}
}
}

private ActiveSpan createLocalSpan(HttpRequest httpRequest, HttpClientContext clientContext) {
Tracer.SpanBuilder spanBuilder =
private Scope createLocalScope(
final HttpRequest httpRequest, final HttpClientContext clientContext) {
final Tracer.SpanBuilder spanBuilder =
tracer
.buildSpan(httpRequest.getRequestLine().getMethod())
.withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME);

ActiveSpan localSpan = spanBuilder.startActive();
clientContext.setAttribute(ACTIVE_SPAN, localSpan);
final Scope scope = spanBuilder.startActive(true);
clientContext.setAttribute(ACTIVE_SPAN, scope);
clientContext.setAttribute(REDIRECT_COUNT, 0);
return localSpan;
return scope;
}

private CloseableHttpResponse createNetworkSpan(
ActiveSpan parentSpan,
HttpRoute route,
HttpRequestWrapper request,
HttpClientContext clientContext,
HttpExecutionAware execAware)
final Scope parentScope,
final HttpRoute route,
final HttpRequestWrapper request,
final HttpClientContext clientContext,
final HttpExecutionAware execAware)
throws IOException, HttpException {
ActiveSpan networkSpan =
final Scope networkScope =
tracer
.buildSpan(request.getMethod())
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.asChildOf(parentSpan)
.startActive();
.asChildOf(parentScope.span())
.startActive(true);

final Span networkSpan = networkScope.span();
tracer.inject(
networkSpan.context(), Format.Builtin.HTTP_HEADERS, new HttpHeadersInjectAdapter(request));

Expand All @@ -148,20 +152,20 @@ private CloseableHttpResponse createNetworkSpan(

throw e;
} finally {
networkSpan.deactivate();
networkScope.close();
}
}

public static class HttpHeadersInjectAdapter implements TextMap {

private HttpRequest httpRequest;
private final HttpRequest httpRequest;

public HttpHeadersInjectAdapter(HttpRequest httpRequest) {
public HttpHeadersInjectAdapter(final HttpRequest httpRequest) {
this.httpRequest = httpRequest;
}

@Override
public void put(String key, String value) {
public void put(final String key, final String value) {
httpRequest.addHeader(key, value);
}

Expand Down
2 changes: 1 addition & 1 deletion dd-java-agent/instrumentation/aws-sdk/aws-sdk.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ apply from: "${rootDir}/gradle/java.gradle"

dependencies {
compileOnly group: 'com.amazonaws', name: 'aws-java-sdk-core', version: '1.11.119'
compile('io.opentracing.contrib:opentracing-aws-sdk:0.0.2') {
compile('io.opentracing.contrib:opentracing-aws-sdk:0.0.3-RC1') {
transitive = false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ apply from: "${rootDir}/gradle/java.gradle"

dependencies {
compileOnly group: 'com.datastax.cassandra', name: 'cassandra-driver-core', version: '3.2.0'
compile('io.opentracing.contrib:opentracing-cassandra-driver:0.0.2') {
compile('io.opentracing.contrib:opentracing-cassandra-driver:0.0.3-RC1') {
transitive = false
}

Expand Down
Loading