Merge pull request #376 from DataDog/mar-kolya/akka-http-server-renames

akka http server renames
This commit is contained in:
Nikolay Martynov 2018-06-26 18:10:09 -04:00 committed by GitHub
commit 2447f6a99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 87 deletions

View File

@ -1,9 +1,11 @@
import akka.NotUsed
import akka.stream.javadsl.Source
import akka.stream.testkit.javadsl.TestSink
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
import io.opentracing.tag.Tags
import net.bytebuddy.utility.JavaModule
import static java.util.concurrent.TimeUnit.SECONDS
import datadog.trace.agent.test.AgentTestRunner
import play.inject.guice.GuiceApplicationBuilder
import spock.lang.Shared
@ -64,7 +66,7 @@ class LagomTest extends AgentTestRunner {
Source.from(Arrays.asList("msg1", "msg2", "msg3"))
.concat(Source.maybe())
Source<String, NotUsed> output = service.echo().invoke(input)
.toCompletableFuture().get(5, SECONDS)
.toCompletableFuture().get()
Probe<String> probe = output.runWith(TestSink.probe(server.system()), server.materializer())
probe.request(10)
probe.expectNext("msg1")
@ -82,12 +84,12 @@ class LagomTest extends AgentTestRunner {
errored false
tags {
defaultTags()
"http.status_code" 101
"http.url" "ws://localhost:${server.port()}/echo"
"http.method" "GET"
"span.kind" "server"
"span.type" "web"
"component" "akka-http-server"
"$Tags.HTTP_STATUS.key" 101
"$Tags.HTTP_URL.key" "ws://localhost:${server.port()}/echo"
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$DDTags.SPAN_TYPE" DDSpanTypes.WEB_SERVLET
"$Tags.COMPONENT.key" "akka-http-server"
}
}
span(1) {
@ -106,7 +108,7 @@ class LagomTest extends AgentTestRunner {
Source.from(Arrays.asList("msg1", "msg2", "msg3"))
.concat(Source.maybe())
try {
service.error().invoke(input).toCompletableFuture().get(5, SECONDS)
service.error().invoke(input).toCompletableFuture().get()
} catch (Exception e) {
}
@ -120,13 +122,13 @@ class LagomTest extends AgentTestRunner {
errored true
tags {
defaultTags()
"http.status_code" 500
"http.url" "ws://localhost:${server.port()}/error"
"http.method" "GET"
"span.kind" "server"
"span.type" "web"
"component" "akka-http-server"
"error" true
"$Tags.HTTP_STATUS.key" 500
"$Tags.HTTP_URL.key" "ws://localhost:${server.port()}/error"
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$DDTags.SPAN_TYPE" DDSpanTypes.WEB_SERVLET
"$Tags.COMPONENT.key" "akka-http-server"
"$Tags.ERROR.key" true
}
}
}

View File

@ -6,7 +6,6 @@ import akka.http.javadsl.model.HttpHeader;
import akka.http.scaladsl.model.HttpRequest;
import akka.http.scaladsl.model.HttpResponse;
import akka.stream.*;
import akka.stream.stage.*;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.*;
import datadog.trace.api.DDSpanTypes;
@ -33,8 +32,8 @@ import scala.runtime.AbstractFunction1;
@Slf4j
@AutoService(Instrumenter.class)
public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
public AkkaHttpInstrumentation() {
public final class AkkaHttpServerInstrumentation extends Instrumenter.Configurable {
public AkkaHttpServerInstrumentation() {
super("akka-http", "akka-http-server");
}
@ -45,11 +44,12 @@ public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
private static final HelperInjector akkaHttpHelperInjector =
new HelperInjector(
AkkaHttpInstrumentation.class.getName() + "$DatadogSyncWrapper",
AkkaHttpInstrumentation.class.getName() + "$DatadogAsyncWrapper",
AkkaHttpInstrumentation.class.getName() + "$DatadogAsyncWrapper$1",
AkkaHttpInstrumentation.class.getName() + "$DatadogAsyncWrapper$2",
AkkaHttpInstrumentation.class.getName() + "$AkkaHttpHeaders");
AkkaHttpServerInstrumentation.class.getName() + "$DatadogWrapperHelper",
AkkaHttpServerInstrumentation.class.getName() + "$DatadogSyncWrapper",
AkkaHttpServerInstrumentation.class.getName() + "$DatadogAsyncWrapper",
AkkaHttpServerInstrumentation.class.getName() + "$DatadogAsyncWrapper$1",
AkkaHttpServerInstrumentation.class.getName() + "$DatadogAsyncWrapper$2",
AkkaHttpServerInstrumentation.class.getName() + "$AkkaHttpServerHeaders");
@Override
public AgentBuilder apply(final AgentBuilder agentBuilder) {
@ -57,9 +57,9 @@ public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
.type(named("akka.http.scaladsl.HttpExt"))
.transform(DDTransformers.defaultTransformers())
.transform(akkaHttpHelperInjector)
// Insturmenting akka-streams bindAndHandle api was previously attempted.
// Instrumenting akka-streams bindAndHandle api was previously attempted.
// This proved difficult as there was no clean way to close the async scope
// in the graph logic after the user's requst handler completes.
// in the graph logic after the user's request handler completes.
//
// Instead, we're instrumenting the bindAndHandle function helpers by
// wrapping the scala functions with our own handlers.
@ -95,31 +95,11 @@ public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
}
}
public static class DatadogSyncWrapper extends AbstractFunction1<HttpRequest, HttpResponse> {
private final Function1<HttpRequest, HttpResponse> userHandler;
public DatadogSyncWrapper(Function1<HttpRequest, HttpResponse> userHandler) {
this.userHandler = userHandler;
}
@Override
public HttpResponse apply(HttpRequest request) {
final Scope scope = DatadogSyncWrapper.createSpan(request);
try {
final HttpResponse response = userHandler.apply(request);
scope.close();
finishSpan(scope.span(), response);
return response;
} catch (Throwable t) {
scope.close();
finishSpan(scope.span(), t);
throw t;
}
}
public static class DatadogWrapperHelper {
public static Scope createSpan(HttpRequest request) {
final SpanContext extractedContext =
GlobalTracer.get().extract(Format.Builtin.HTTP_HEADERS, new AkkaHttpHeaders(request));
GlobalTracer.get()
.extract(Format.Builtin.HTTP_HEADERS, new AkkaHttpServerHeaders(request));
final Scope scope =
GlobalTracer.get()
.buildSpan("akka-http.request")
@ -158,6 +138,29 @@ public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
}
}
public static class DatadogSyncWrapper extends AbstractFunction1<HttpRequest, HttpResponse> {
private final Function1<HttpRequest, HttpResponse> userHandler;
public DatadogSyncWrapper(Function1<HttpRequest, HttpResponse> userHandler) {
this.userHandler = userHandler;
}
@Override
public HttpResponse apply(HttpRequest request) {
final Scope scope = DatadogWrapperHelper.createSpan(request);
try {
final HttpResponse response = userHandler.apply(request);
scope.close();
DatadogWrapperHelper.finishSpan(scope.span(), response);
return response;
} catch (Throwable t) {
scope.close();
DatadogWrapperHelper.finishSpan(scope.span(), t);
throw t;
}
}
}
public static class DatadogAsyncWrapper
extends AbstractFunction1<HttpRequest, Future<HttpResponse>> {
private final Function1<HttpRequest, Future<HttpResponse>> userHandler;
@ -172,13 +175,13 @@ public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
@Override
public Future<HttpResponse> apply(HttpRequest request) {
final Scope scope = DatadogSyncWrapper.createSpan(request);
final Scope scope = DatadogWrapperHelper.createSpan(request);
Future<HttpResponse> futureResponse = null;
try {
futureResponse = userHandler.apply(request);
} catch (Throwable t) {
scope.close();
DatadogSyncWrapper.finishSpan(scope.span(), t);
DatadogWrapperHelper.finishSpan(scope.span(), t);
throw t;
}
final Future<HttpResponse> wrapped =
@ -186,14 +189,14 @@ public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
new AbstractFunction1<HttpResponse, HttpResponse>() {
@Override
public HttpResponse apply(HttpResponse response) {
DatadogSyncWrapper.finishSpan(scope.span(), response);
DatadogWrapperHelper.finishSpan(scope.span(), response);
return response;
}
},
new AbstractFunction1<Throwable, Throwable>() {
@Override
public Throwable apply(Throwable t) {
DatadogSyncWrapper.finishSpan(scope.span(), t);
DatadogWrapperHelper.finishSpan(scope.span(), t);
return t;
}
},
@ -203,10 +206,10 @@ public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
}
}
public static class AkkaHttpHeaders implements TextMap {
public static class AkkaHttpServerHeaders implements TextMap {
private final HttpRequest request;
public AkkaHttpHeaders(HttpRequest request) {
public AkkaHttpServerHeaders(HttpRequest request) {
this.request = request;
}
@ -222,8 +225,8 @@ public final class AkkaHttpInstrumentation extends Instrumenter.Configurable {
}
@Override
public void put(String s, String s1) {
throw new IllegalStateException("akka http headers can only be extracted");
public void put(String name, String value) {
throw new IllegalStateException("akka http server headers can only be extracted");
}
}
}

View File

@ -1,11 +1,14 @@
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
import io.opentracing.tag.Tags
import okhttp3.OkHttpClient
import okhttp3.Request
import spock.lang.Shared
import static datadog.trace.agent.test.ListWriterAssert.assertTraces
class AkkaHttpInstrumentationTest extends AgentTestRunner {
class AkkaHttpServerInstrumentationTest extends AgentTestRunner {
static {
System.setProperty("dd.integration.akka-http-server.enabled", "true")
}
@ -52,12 +55,12 @@ class AkkaHttpInstrumentationTest extends AgentTestRunner {
errored false
tags {
defaultTags()
"http.status_code" 200
"http.url" "http://localhost:$port/test"
"http.method" "GET"
"span.kind" "server"
"span.type" "web"
"component" "akka-http-server"
"$Tags.HTTP_STATUS.key" 200
"$Tags.HTTP_URL.key" "http://localhost:$port/test"
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$DDTags.SPAN_TYPE" DDSpanTypes.WEB_SERVLET
"$Tags.COMPONENT.key" "akka-http-server"
}
}
span(1) {
@ -94,16 +97,13 @@ class AkkaHttpInstrumentationTest extends AgentTestRunner {
errored true
tags {
defaultTags()
"http.status_code" 500
"http.url" "http://localhost:$port/$endpoint"
"http.method" "GET"
"span.kind" "server"
"span.type" "web"
"component" "akka-http-server"
"error" true
"error.type" RuntimeException.name
"error.msg" errorMessage
"error.stack" String
"$Tags.HTTP_STATUS.key" 500
"$Tags.HTTP_URL.key" "http://localhost:$port/$endpoint"
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$DDTags.SPAN_TYPE" DDSpanTypes.WEB_SERVLET
"$Tags.COMPONENT.key" "akka-http-server"
errorTags RuntimeException, errorMessage
}
}
}
@ -137,13 +137,13 @@ class AkkaHttpInstrumentationTest extends AgentTestRunner {
errored true
tags {
defaultTags()
"http.status_code" 500
"http.url" "http://localhost:$port/server-error"
"http.method" "GET"
"span.kind" "server"
"span.type" "web"
"component" "akka-http-server"
"error" true
"$Tags.HTTP_STATUS.key" 500
"$Tags.HTTP_URL.key" "http://localhost:$port/server-error"
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$DDTags.SPAN_TYPE" DDSpanTypes.WEB_SERVLET
"$Tags.COMPONENT.key" "akka-http-server"
"$Tags.ERROR.key" true
}
}
}
@ -176,12 +176,12 @@ class AkkaHttpInstrumentationTest extends AgentTestRunner {
errored false
tags {
defaultTags()
"http.status_code" 404
"http.url" "http://localhost:$port/not-found"
"http.method" "GET"
"span.kind" "server"
"span.type" "web"
"component" "akka-http-server"
"$Tags.HTTP_STATUS.key" 404
"$Tags.HTTP_URL.key" "http://localhost:$port/not-found"
"$Tags.HTTP_METHOD.key" "GET"
"$Tags.SPAN_KIND.key" Tags.SPAN_KIND_SERVER
"$DDTags.SPAN_TYPE" DDSpanTypes.WEB_SERVLET
"$Tags.COMPONENT.key" "akka-http-server"
}
}
}