Abstract decorator classes for new API
Intended to be implemented for each instrumentation, then called directly by the instrumentation in the appropriate locations.
MODIFY
dd-trace/dd-trace.gradle
dd-trace/dd-trace.gradle
@@ -5,6 +5,7 @@ minimumBranchCoverage = 0.9
minimumInstructionCoverage = 0.9
excludedClassesConverage += [
'datadog.trace.tracer.Tracer.TracerBuilder',
+ 'datadog.trace.decorator.*', // TODO: remove when ready to write tests.
]
dependencies {

ADD
dd-trace/src/main/java/datadog/trace/decorator/BaseDecorator.java
+package datadog.trace.decorator;
+
+import datadog.trace.tracer.Span;
+
+public abstract class BaseDecorator {
+
+ protected abstract String component();
+
+ public Span afterStart(final Span span) {
+ assert span != null;
+ span.setMeta("component", component());
+ return span;
+ }
+
+ public Span beforeFinish(final Span span) {
+ assert span != null;
+ return span;
+ }
+
+ public Span onError(final Span span, final Throwable throwable) {
+ assert span != null;
+ if (throwable != null) {
+ span.setErrored(true);
+ span.attachThrowable(throwable);
+ }
+ return span;
+ }
+}

ADD
dd-trace/src/main/java/datadog/trace/decorator/ClientDecorator.java
+package datadog.trace.decorator;
+
+import datadog.trace.tracer.Span;
+
+public abstract class ClientDecorator extends BaseDecorator {
+
+ protected abstract String service();
+
+ protected abstract String spanType();
+
+ @Override
+ public Span afterStart(final Span span) {
+ assert span != null;
+ if (service() != null) {
+ span.setService(service());
+ }
+ span.setMeta("span.kind", "client");
+ span.setMeta("span.type", spanType());
+ return super.afterStart(span);
+ }
+}

ADD
dd-trace/src/main/java/datadog/trace/decorator/HttpClientDecorator.java
+package datadog.trace.decorator;
+
+import datadog.trace.tracer.Span;
+
+public abstract class HttpClientDecorator<REQUEST, RESPONSE> extends ClientDecorator {
+
+ protected abstract String method(REQUEST request);
+
+ protected abstract String url(REQUEST request);
+
+ protected abstract String hostname(RESPONSE request);
+
+ protected abstract int port(RESPONSE request);
+
+ protected abstract int status(RESPONSE response);
+
+ @Override
+ protected String spanType() {
+ return "http";
+ }
+
+ public Span onRequest(final Span span, final REQUEST request) {
+ assert span != null;
+ if (request != null) {
+ span.setMeta("http.method", method(request));
+ span.setMeta("http.url", url(request));
+ }
+ return span;
+ }
+
+ public Span onResponse(final Span span, final RESPONSE response) {
+ assert span != null;
+ if (response != null) {
+ span.setMeta("peer.hostname", hostname(response));
+ span.setMeta("peer.port", port(response));
+
+ final int status = status(response);
+ span.setMeta("http.status", status);
+ if (400 <= status && status < 500) {
+ span.setErrored(true);
+ }
+ }
+ return span;
+ }
+}

ADD
dd-trace/src/main/java/datadog/trace/decorator/HttpServerDecorator.java
+package datadog.trace.decorator;
+
+import datadog.trace.tracer.Span;
+
+public abstract class HttpServerDecorator<REQUEST, RESPONSE> extends ServerDecorator {
+
+ protected abstract String method(REQUEST request);
+
+ protected abstract String url(REQUEST request);
+
+ protected abstract String hostname(REQUEST request);
+
+ protected abstract int port(REQUEST request);
+
+ protected abstract int status(RESPONSE response);
+
+ @Override
+ protected String spanType() {
+ return "web";
+ }
+
+ public Span onRequest(final Span span, final REQUEST request) {
+ assert span != null;
+ if (request != null) {
+ span.setMeta("http.method", method(request));
+ span.setMeta("http.url", url(request));
+ span.setMeta("peer.hostname", hostname(request));
+ span.setMeta("peer.port", port(request));
+ // TODO set resource name from URL.
+ }
+ return span;
+ }
+
+ public Span onResponse(final Span span, final RESPONSE response) {
+ assert span != null;
+ if (response != null) {
+ span.setMeta("http.status", status(response));
+ if (status(response) >= 500) {
+ span.setErrored(true);
+ }
+ }
+ return span;
+ }
+
+ @Override
+ public Span onError(final Span span, final Throwable throwable) {
+ assert span != null;
+ final Object status = span.getMeta("http.status");
+ if (status == null || status.equals(200)) {
+ // Ensure status set correctly
+ span.setMeta("http.status", 500);
+ }
+ return super.onError(span, throwable);
+ }
+}

ADD
dd-trace/src/main/java/datadog/trace/decorator/ServerDecorator.java
+package datadog.trace.decorator;
+
+import datadog.trace.tracer.Span;
+
+public abstract class ServerDecorator extends BaseDecorator {
+
+ protected abstract String spanType();
+
+ @Override
+ public Span afterStart(final Span span) {
+ assert span != null;
+ span.setMeta("span.kind", "server");
+ span.setMeta("span.type", spanType());
+ return super.afterStart(span);
+ }
+}

Abstract decorator classes for new API

Intended to be implemented for each instrumentation, then called directly by the instrumentation in the appropriate locations.