Fixes for ratpack.
MODIFY
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/decorator/HttpServerDecorator.java
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/decorator/HttpServerDecorator.java
@@ -5,10 +5,15 @@ import datadog.trace.api.DDSpanTypes;
import io.opentracing.Span;
import io.opentracing.tag.Tags;
import java.net.URI;
+import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class HttpServerDecorator<REQUEST, CONNECTION, RESPONSE> extends ServerDecorator {
+ // Source: https://www.regextester.com/22
+ private static final Pattern VALID_IPV4_ADDRESS =
+ Pattern.compile(
+ "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
protected abstract String method(REQUEST request);
@@ -68,10 +73,10 @@ public abstract class HttpServerDecorator<REQUEST, CONNECTION, RESPONSE> extends
Tags.PEER_HOSTNAME.set(span, peerHostname(connection));
final String ip = peerHostIP(connection);
if (ip != null) {
- if (ip.contains(":")) {
- Tags.PEER_HOST_IPV6.set(span, ip);
- } else {
+ if (VALID_IPV4_ADDRESS.matcher(ip).matches()) {
Tags.PEER_HOST_IPV4.set(span, ip);
+ } else if (ip.contains(":")) {
+ Tags.PEER_HOST_IPV6.set(span, ip);
}
}
Tags.PEER_PORT.set(span, peerPort(connection));

MODIFY
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/server/NettyHttpServerDecorator.java
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/server/NettyHttpServerDecorator.java
@@ -38,7 +38,7 @@ public class NettyHttpServerDecorator
try {
URI uri = new URI(request.uri());
if ((uri.getHost() == null || uri.getHost().equals("")) && request.headers().contains(HOST)) {
- uri = new URI("http://" + request.headers().get(HOST) + request.getUri());
+ uri = new URI("http://" + request.headers().get(HOST) + request.uri());
}
return new URI(
uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/latestDepTest/groovy/RatpackTest.groovy
dd-java-agent/instrumentation/ratpack-1.4/src/latestDepTest/groovy/RatpackTest.groovy
+import static datadog.trace.agent.test.server.http.TestHttpServer.distributedRequestTrace
+import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer
+import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
+
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.agent.test.utils.OkHttpUtils
import datadog.trace.api.DDSpanTypes
@@ -8,6 +12,8 @@ import io.opentracing.Scope
import io.opentracing.Span
import io.opentracing.tag.Tags
import io.opentracing.util.GlobalTracer
+import java.util.concurrent.CountDownLatch
+import java.util.regex.Pattern
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
@@ -20,13 +26,6 @@ import ratpack.http.client.HttpClient
import ratpack.path.PathBinding
import ratpack.test.exec.ExecHarness
-import java.util.concurrent.CountDownLatch
-import java.util.regex.Pattern
-
-import static datadog.trace.agent.test.server.http.TestHttpServer.distributedRequestTrace
-import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer
-import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
-
class RatpackTest extends AgentTestRunner {
OkHttpClient client = OkHttpUtils.client()
@@ -85,7 +84,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
defaultTags()
}
}
@@ -149,7 +150,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
- "$Tags.HTTP_URL.key" "/a/b/baz"
+ "$Tags.HTTP_URL.key" "${app.address}a/b/baz"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
defaultTags()
}
}
@@ -209,7 +212,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 500
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
errorTags(HandlerException, Pattern.compile("java.lang.ArithmeticException: Division( is)? undefined"))
defaultTags()
}
@@ -274,7 +279,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 500
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
"$Tags.ERROR.key" true
defaultTags()
}
@@ -337,7 +344,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 500
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
"$Tags.ERROR.key" true
defaultTags()
}
@@ -431,7 +440,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
defaultTags()
}
}
@@ -539,7 +550,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 500
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
"$Tags.ERROR.key" true
defaultTags()
}
@@ -642,7 +655,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
defaultTags()
}
}

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/RatpackServerDecorator.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/RatpackServerDecorator.java
package datadog.trace.instrumentation.ratpack;
+import com.google.common.net.HostAndPort;
import datadog.trace.agent.decorator.HttpServerDecorator;
import datadog.trace.api.DDTags;
import io.opentracing.Span;
+import java.net.URI;
+import lombok.extern.slf4j.Slf4j;
import ratpack.handling.Context;
import ratpack.http.Request;
import ratpack.http.Response;
import ratpack.http.Status;
+import ratpack.server.PublicAddress;
-public class RatpackServerDecorator extends HttpServerDecorator<Request, Response> {
+@Slf4j
+public class RatpackServerDecorator extends HttpServerDecorator<Request, Request, Response> {
public static final RatpackServerDecorator DECORATE = new RatpackServerDecorator();
@Override
@@ -27,18 +32,27 @@ public class RatpackServerDecorator extends HttpServerDecorator<Request, Respons
}
@Override
- protected String url(final Request request) {
- return request.getUri();
+ protected URI url(final Request request) {
+ final HostAndPort address = request.getLocalAddress();
+ // This call implicitly uses request via a threadlocal provided by ratpack.
+ final PublicAddress publicAddress =
+ PublicAddress.inferred(address.getPort() == 443 ? "https" : "http");
+ return publicAddress.get(request.getPath());
}
@Override
- protected String hostname(final Request request) {
- return null;
+ protected String peerHostname(final Request request) {
+ return request.getRemoteAddress().getHostText();
}
@Override
- protected Integer port(final Request request) {
- return null;
+ protected String peerHostIP(final Request request) {
+ return request.getRemoteAddress().getHostText();
+ }
+
+ @Override
+ protected Integer peerPort(final Request request) {
+ return request.getRemoteAddress().getPort();
}
@Override

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/TracingHandler.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/TracingHandler.java
@@ -40,6 +40,7 @@ public final class TracingHandler implements Handler {
// Relying on executor instrumentation to assume the netty span is in context as the parent.
final Span ratpackSpan = tracer.buildSpan("ratpack.handler").start();
DECORATE.afterStart(ratpackSpan);
+ DECORATE.onConnection(ratpackSpan, request);
DECORATE.onRequest(ratpackSpan, request);
try (final Scope scope = tracer.scopeManager().activate(ratpackSpan, false)) {

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/test/groovy/RatpackTest.groovy
dd-java-agent/instrumentation/ratpack-1.4/src/test/groovy/RatpackTest.groovy
+import static datadog.trace.agent.test.server.http.TestHttpServer.distributedRequestTrace
+import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer
+import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
+
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.agent.test.utils.OkHttpUtils
import datadog.trace.api.DDSpanTypes
@@ -7,6 +11,8 @@ import io.opentracing.Scope
import io.opentracing.Span
import io.opentracing.tag.Tags
import io.opentracing.util.GlobalTracer
+import java.util.concurrent.CountDownLatch
+import java.util.regex.Pattern
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
@@ -19,13 +25,6 @@ import ratpack.http.client.HttpClient
import ratpack.path.PathBinding
import ratpack.test.exec.ExecHarness
-import java.util.concurrent.CountDownLatch
-import java.util.regex.Pattern
-
-import static datadog.trace.agent.test.server.http.TestHttpServer.distributedRequestTrace
-import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer
-import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
-
class RatpackTest extends AgentTestRunner {
OkHttpClient client = OkHttpUtils.client()
@@ -84,7 +83,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
defaultTags()
}
}
@@ -148,7 +149,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
- "$Tags.HTTP_URL.key" "/a/b/baz"
+ "$Tags.HTTP_URL.key" "${app.address}a/b/baz"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
defaultTags()
}
}
@@ -208,7 +211,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 500
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
errorTags(HandlerException, Pattern.compile("java.lang.ArithmeticException: Division( is)? undefined"))
defaultTags()
}
@@ -273,7 +278,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 500
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
"$Tags.ERROR.key" true
defaultTags()
}
@@ -336,7 +343,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 500
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
"$Tags.ERROR.key" true
defaultTags()
}
@@ -430,7 +439,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
defaultTags()
}
}
@@ -538,7 +549,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 500
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
errorTags(ConnectException, String)
defaultTags()
}
@@ -641,7 +654,9 @@ class RatpackTest extends AgentTestRunner {
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.HTTP_STATUS.key" 200
- "$Tags.HTTP_URL.key" "/"
+ "$Tags.HTTP_URL.key" "$app.address"
+ "$Tags.PEER_HOSTNAME.key" "$app.address.host"
+ "$Tags.PEER_PORT.key" Integer
defaultTags()
}
}

Fixes for ratpack.