Fix headers to match standard DD trace headers
Also prevent NullPointerExceptions in servlet handler if active span somehow missing.
MODIFY
dd-java-agent/integrations/helpers/src/main/java/com/datadoghq/agent/integration/Servlet2Helper.java
dd-java-agent/integrations/helpers/src/main/java/com/datadoghq/agent/integration/Servlet2Helper.java
@@ -71,9 +71,11 @@ public class Servlet2Helper extends OpenTracingHelper {
}
final ActiveSpan span = tracer.activeSpan();
+ if (span != null) {
ServletFilterSpanDecorator.STANDARD_TAGS.onError(req, resp, ex, span);
span.deactivate();
}
+ }
public void onResponse(final HttpServletRequest req, final HttpServletResponse resp) {
if (req.getAttribute(SERVER_SPAN_CONTEXT) == null) {
@@ -82,7 +84,9 @@ public class Servlet2Helper extends OpenTracingHelper {
}
final ActiveSpan span = tracer.activeSpan();
+ if (span != null) {
ServletFilterSpanDecorator.STANDARD_TAGS.onResponse(req, resp, span);
span.deactivate();
}
}
+}

MODIFY
dd-java-agent/integrations/helpers/src/main/java/com/datadoghq/agent/integration/Servlet3Helper.java
dd-java-agent/integrations/helpers/src/main/java/com/datadoghq/agent/integration/Servlet3Helper.java
@@ -33,6 +33,7 @@ public class Servlet3Helper extends Servlet2Helper {
}
final ActiveSpan span = tracer.activeSpan();
+ if (span != null) {
if (req.isAsyncStarted()) {
addAsyncListeners(req, resp, span);
} else {
@@ -40,6 +41,7 @@ public class Servlet3Helper extends Servlet2Helper {
}
span.deactivate();
}
+ }
private void addAsyncListeners(
final HttpServletRequest req, final HttpServletResponse resp, final ActiveSpan span) {

MODIFY
dd-trace-examples/spring-boot-jdbc/src/main/java/com/datadoghq/examples/Application.java
dd-trace-examples/spring-boot-jdbc/src/main/java/com/datadoghq/examples/Application.java
@@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
- public static void main(String[] args) {
+ public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}

MODIFY
dd-trace-examples/spring-boot-jdbc/src/main/resources/application.properties
dd-trace-examples/spring-boot-jdbc/src/main/resources/application.properties
# you must set the following so that OpenTracing traced driver is used
spring.datasource.driver-class-name=io.opentracing.contrib.jdbc.TracingDriver
-spring.datasource.url=jdbc:tracing:h2:mem:spring-test
+spring.datasource.url=jdbc:tracing:h2:mem:spring-test;DB_CLOSE_ON_EXIT=FALSE
# set the logging level
logging.level.root=INFO

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/propagation/HTTPCodec.java
dd-trace/src/main/java/com/datadoghq/trace/propagation/HTTPCodec.java
@@ -14,10 +14,9 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HTTPCodec implements Codec<TextMap> {
- private static final String OT_PREFIX = "ot-tracer-";
private static final String OT_BAGGAGE_PREFIX = "ot-baggage-";
- private static final String TRACE_ID_KEY = OT_PREFIX + "traceid";
- private static final String SPAN_ID_KEY = OT_PREFIX + "spanid";
+ private static final String TRACE_ID_KEY = "x-datadog-trace-id";
+ private static final String SPAN_ID_KEY = "x-datadog-parent-id";
@Override
public void inject(final DDSpanContext context, final TextMap carrier) {

MODIFY
dd-trace/src/test/java/com/datadoghq/trace/propagation/HTTPCodecTest.java
dd-trace/src/test/java/com/datadoghq/trace/propagation/HTTPCodecTest.java
@@ -12,15 +12,14 @@ import org.junit.Test;
/** Created by gpolaert on 6/23/17. */
public class HTTPCodecTest {
- private static final String OT_PREFIX = "ot-tracer-";
private static final String OT_BAGGAGE_PREFIX = "ot-baggage-";
- private static final String TRACE_ID_KEY = OT_PREFIX + "traceid";
- private static final String SPAN_ID_KEY = OT_PREFIX + "spanid";
+ private static final String TRACE_ID_KEY = "x-datadog-trace-id";
+ private static final String SPAN_ID_KEY = "x-datadog-parent-id";
@Test
public void shoudAddHttpHeaders() {
- DDSpanContext mockedContext =
+ final DDSpanContext mockedContext =
new DDSpanContext(
1L,
2L,
@@ -40,9 +39,9 @@ public class HTTPCodecTest {
null,
null);
- Map<String, String> carrier = new HashMap<>();
+ final Map<String, String> carrier = new HashMap<>();
- HTTPCodec codec = new HTTPCodec();
+ final HTTPCodec codec = new HTTPCodec();
codec.inject(mockedContext, new TextMapInjectAdapter(carrier));
assertThat(carrier.get(TRACE_ID_KEY)).isEqualTo("1");
@@ -54,7 +53,7 @@ public class HTTPCodecTest {
@Test
public void shoudReadHttpHeaders() {
- Map<String, String> actual =
+ final Map<String, String> actual =
new HashMap<String, String>() {
{
put(TRACE_ID_KEY, "1");
@@ -64,8 +63,8 @@ public class HTTPCodecTest {
}
};
- HTTPCodec codec = new HTTPCodec();
- DDSpanContext context = codec.extract(new TextMapExtractAdapter(actual));
+ final HTTPCodec codec = new HTTPCodec();
+ final DDSpanContext context = codec.extract(new TextMapExtractAdapter(actual));
assertThat(context.getTraceId()).isEqualTo(1l);
assertThat(context.getSpanId()).isEqualTo(2l);

Fix headers to match standard DD trace headers

Also prevent NullPointerExceptions in servlet handler if active span somehow missing.