Migrate Jax-rs client instrumentation to Decorator
MODIFY
dd-java-agent/instrumentation/akka-http-10.0/src/test/groovy/AkkaHttpClientInstrumentationTest.groovy
dd-java-agent/instrumentation/akka-http-10.0/src/test/groovy/AkkaHttpClientInstrumentationTest.groovy
@@ -20,13 +20,13 @@ import java.util.concurrent.CompletionStage
import java.util.concurrent.ExecutionException
import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer
+import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
import static datadog.trace.agent.test.utils.TraceUtils.withConfigOverride
class AkkaHttpClientInstrumentationTest extends AgentTestRunner {
private static final String MESSAGE = "an\nmultiline\nhttp\nresponse"
private static final long TIMEOUT = 10000L
- private static final int UNUSED_PORT = 61 // this port should always be closed
@AutoCleanup
@Shared
@@ -109,7 +109,7 @@ class AkkaHttpClientInstrumentationTest extends AgentTestRunner {
def "error request trace"() {
setup:
- def url = new URL("http://localhost:$UNUSED_PORT/test")
+ def url = new URL("http://localhost:$UNUSABLE_PORT/test")
HttpRequest request = HttpRequest.create(url.toString())
CompletionStage<HttpResponse> responseFuture =
@@ -139,7 +139,7 @@ class AkkaHttpClientInstrumentationTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
"$DDTags.SPAN_TYPE" DDSpanTypes.HTTP_CLIENT
"$Tags.PEER_HOSTNAME.key" server.address.host
- "$Tags.PEER_PORT.key" UNUSED_PORT
+ "$Tags.PEER_PORT.key" UNUSABLE_PORT
"$Tags.COMPONENT.key" "akka-http-client"
"$Tags.ERROR.key" true
errorTags(StreamTcpException, { it.contains("Tcp command") })
@@ -241,7 +241,7 @@ class AkkaHttpClientInstrumentationTest extends AgentTestRunner {
def "error request pool trace"() {
setup:
// Use port number that really should be closed
- def url = new URL("http://localhost:$UNUSED_PORT/test")
+ def url = new URL("http://localhost:$UNUSABLE_PORT/test")
def response = withConfigOverride(Config.HTTP_CLIENT_HOST_SPLIT_BY_DOMAIN, "$renameService") {
Source
@@ -272,7 +272,7 @@ class AkkaHttpClientInstrumentationTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
"$DDTags.SPAN_TYPE" DDSpanTypes.HTTP_CLIENT
"$Tags.PEER_HOSTNAME.key" server.address.host
- "$Tags.PEER_PORT.key" UNUSED_PORT
+ "$Tags.PEER_PORT.key" UNUSABLE_PORT
"$Tags.COMPONENT.key" "akka-http-client"
"$Tags.ERROR.key" true
errorTags(StreamTcpException, { it.contains("Tcp command") })

MODIFY
dd-java-agent/instrumentation/http-url-connection/src/test/groovy/UrlConnectionTest.groovy
dd-java-agent/instrumentation/http-url-connection/src/test/groovy/UrlConnectionTest.groovy
@@ -6,14 +6,13 @@ import datadog.trace.instrumentation.http_url_connection.UrlInstrumentation
import io.opentracing.tag.Tags
import io.opentracing.util.GlobalTracer
+import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
import static datadog.trace.agent.test.utils.TraceUtils.withConfigOverride
import static datadog.trace.instrumentation.http_url_connection.HttpUrlConnectionInstrumentation.HttpUrlState.OPERATION_NAME
class UrlConnectionTest extends AgentTestRunner {
- private static final int UNUSED_PORT = 61 // this port should always be closed
-
def "trace request with connection failure #scheme"() {
when:
withConfigOverride(Config.HTTP_CLIENT_HOST_SPLIT_BY_DOMAIN, "$renameService") {
@@ -53,7 +52,7 @@ class UrlConnectionTest extends AgentTestRunner {
"$Tags.HTTP_URL.key" "$url"
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.PEER_HOSTNAME.key" "localhost"
- "$Tags.PEER_PORT.key" UNUSED_PORT
+ "$Tags.PEER_PORT.key" UNUSABLE_PORT
errorTags ConnectException, String
defaultTags()
}
@@ -66,7 +65,7 @@ class UrlConnectionTest extends AgentTestRunner {
"http" | true
"https" | false
- url = new URI("$scheme://localhost:$UNUSED_PORT").toURL()
+ url = new URI("$scheme://localhost:$UNUSABLE_PORT").toURL()
}
def "trace request with connection failure to a local file with broken url path"() {

MODIFY
dd-java-agent/instrumentation/jax-rs-client/src/main/java/datadog/trace/instrumentation/jaxrs/ClientTracingFilter.java
dd-java-agent/instrumentation/jax-rs-client/src/main/java/datadog/trace/instrumentation/jaxrs/ClientTracingFilter.java
package datadog.trace.instrumentation.jaxrs;
-import datadog.trace.api.DDSpanTypes;
+import static datadog.trace.instrumentation.jaxrs.JaxRsClientDecorator.DECORATE;
+
import datadog.trace.api.DDTags;
import io.opentracing.Span;
import io.opentracing.propagation.Format;
-import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import javax.annotation.Priority;
import javax.ws.rs.Priorities;
@@ -25,13 +25,10 @@ public class ClientTracingFilter implements ClientRequestFilter, ClientResponseF
final Span span =
GlobalTracer.get()
.buildSpan("jax-rs.client.call")
- .withTag(Tags.COMPONENT.getKey(), "jax-rs.client")
- .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
- .withTag(Tags.HTTP_METHOD.getKey(), requestContext.getMethod())
- .withTag(Tags.HTTP_URL.getKey(), requestContext.getUri().toString())
- .withTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_CLIENT)
.withTag(DDTags.RESOURCE_NAME, requestContext.getMethod() + " jax-rs.client.call")
.start();
+ DECORATE.afterStart(span);
+ DECORATE.onRequest(span, requestContext);
log.debug("{} - client span started", span);
@@ -50,8 +47,8 @@ public class ClientTracingFilter implements ClientRequestFilter, ClientResponseF
final Object spanObj = requestContext.getProperty(SPAN_PROPERTY_NAME);
if (spanObj instanceof Span) {
final Span span = (Span) spanObj;
- Tags.HTTP_STATUS.set(span, responseContext.getStatus());
-
+ DECORATE.onResponse(span, responseContext);
+ DECORATE.beforeFinish(span);
span.finish();
log.debug("{} - client spanObj finished", spanObj);
}

ADD
dd-java-agent/instrumentation/jax-rs-client/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsClientDecorator.java
+package datadog.trace.instrumentation.jaxrs;
+
+import datadog.trace.agent.decorator.HttpClientDecorator;
+import javax.ws.rs.client.ClientRequestContext;
+import javax.ws.rs.client.ClientResponseContext;
+
+public class JaxRsClientDecorator
+ extends HttpClientDecorator<ClientRequestContext, ClientResponseContext> {
+ public static final JaxRsClientDecorator DECORATE = new JaxRsClientDecorator();
+
+ @Override
+ protected String[] instrumentationNames() {
+ return new String[] {"jax-rs", "jaxrs", "jax-rs-client"};
+ }
+
+ @Override
+ protected String component() {
+ return "jax-rs.client";
+ }
+
+ @Override
+ protected String method(final ClientRequestContext httpRequest) {
+ return httpRequest.getMethod();
+ }
+
+ @Override
+ protected String url(final ClientRequestContext httpRequest) {
+ return httpRequest.getUri().toString();
+ }
+
+ @Override
+ protected String hostname(final ClientRequestContext httpRequest) {
+ return httpRequest.getUri().getHost();
+ }
+
+ @Override
+ protected Integer port(final ClientRequestContext httpRequest) {
+ return httpRequest.getUri().getPort();
+ }
+
+ @Override
+ protected Integer status(final ClientResponseContext httpResponse) {
+ return httpResponse.getStatus();
+ }
+}

MODIFY
dd-java-agent/instrumentation/jax-rs-client/src/test/groovy/JaxRsClientTest.groovy
dd-java-agent/instrumentation/jax-rs-client/src/test/groovy/JaxRsClientTest.groovy
import datadog.trace.agent.test.AgentTestRunner
-import datadog.trace.agent.test.utils.PortUtils
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
import io.opentracing.tag.Tags
@@ -19,12 +18,10 @@ import javax.ws.rs.core.Response
import java.util.concurrent.ExecutionException
import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer
+import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
class JaxRsClientTest extends AgentTestRunner {
- @Shared
- def emptyPort = PortUtils.randomOpenPort()
-
@AutoCleanup
@Shared
def server = httpServer {
@@ -61,12 +58,13 @@ class JaxRsClientTest extends AgentTestRunner {
parent()
errored false
tags {
-
"$Tags.COMPONENT.key" "jax-rs.client"
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
"$Tags.HTTP_URL.key" "$server.address/ping"
+ "$Tags.PEER_HOSTNAME.key" "localhost"
+ "$Tags.PEER_PORT.key" server.address.port
"$DDTags.SPAN_TYPE" DDSpanTypes.HTTP_CLIENT
defaultTags()
}
@@ -90,7 +88,7 @@ class JaxRsClientTest extends AgentTestRunner {
def "#lib connection failure creates errored span"() {
when:
Client client = builder.build()
- WebTarget service = client.target("http://localhost:$emptyPort/ping")
+ WebTarget service = client.target("http://localhost:$UNUSABLE_PORT/ping")
if (async) {
AsyncInvoker request = service.request(MediaType.TEXT_PLAIN).async()
request.get().get()
@@ -115,7 +113,9 @@ class JaxRsClientTest extends AgentTestRunner {
"$Tags.COMPONENT.key" "jax-rs.client"
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
"$Tags.HTTP_METHOD.key" "GET"
- "$Tags.HTTP_URL.key" "http://localhost:$emptyPort/ping"
+ "$Tags.HTTP_URL.key" "http://localhost:$UNUSABLE_PORT/ping"
+ "$Tags.PEER_HOSTNAME.key" "localhost"
+ "$Tags.PEER_PORT.key" UNUSABLE_PORT
"$DDTags.SPAN_TYPE" DDSpanTypes.HTTP_CLIENT
errorTags ProcessingException, String
defaultTags()

MODIFY
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/utils/PortUtils.java
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/utils/PortUtils.java
@@ -5,6 +5,8 @@ import java.net.ServerSocket;
public class PortUtils {
+ public static int UNUSABLE_PORT = 61;
+
/** Open up a random, reusable port. */
public static int randomOpenPort() {
final ServerSocket socket;

Migrate Jax-rs client instrumentation to Decorator