Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
heathkd committed Apr 20, 2020
1 parent 2bc6657 commit e40a1a6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import static datadog.trace.instrumentation.mulehttpconnector.server.TraceCompletionListener.LISTENER;

public class ServerAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.This final Object source, @Advice.Argument(0) final FilterChainContext ctx) {
@Advice.This final Object source,
@Advice.Argument(0) final FilterChainContext ctx,
@Advice.Thrown final Throwable throwable) {

if (!(ctx.getMessage() instanceof HttpContent)) {
return;
Expand All @@ -42,12 +44,16 @@ public static void onExit(

DECORATE.onConnection(span, httpRequest);
DECORATE.onRequest(span, httpRequest);
DECORATE.onResponse(span, httpResponse);

if (throwable == null) {
DECORATE.onResponse(span, httpResponse);
} else {
DECORATE.onError(span, throwable);
scope.close();
}

LISTENER.setSpan(span);
ctx.addCompletionListener(LISTENER);

scope.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ protected String method(final HttpRequestPacket httpRequest) {

@Override
protected URI url(final HttpRequestPacket httpRequest) throws URISyntaxException {
return new URI(httpRequest.getRequestURI());
return new URI(
(httpRequest.isSecure() ? "https://" : "http://")
+ httpRequest.getRemoteHost()
+ ":"
+ httpRequest.getLocalPort()
+ httpRequest.getRequestURI()
+ (httpRequest.getQueryString() != null ? "?" + httpRequest.getQueryString() : ""));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public void onComplete(final FilterChainContext filterChainContext) {
if (span != null) {
DECORATE.beforeFinish(span);
span.finish();
final TraceScope scope = activeScope();
if (scope != null) {
scope.close();
}
}
final TraceScope scope = activeScope();
if (scope != null) {
scope.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.AUTH_REQUIRED
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.NOT_FOUND
Expand All @@ -33,13 +34,16 @@ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.REDIRE
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SUCCESS
import static java.lang.Integer.max
import static java.lang.Runtime.getRuntime
import static java.lang.String.valueOf
import static java.nio.charset.Charset.defaultCharset
import static java.util.concurrent.TimeUnit.MILLISECONDS
import static org.glassfish.grizzly.memory.Buffers.wrap

class MuleHttpServerTest extends HttpServerTest<HttpServer> {

private TCPNIOTransport transport;
private TCPNIOServerConnection serverConnection;
private final int DEFAULT_SERVER_TIMEOUT_MILLIS = 60000
private final int DEFAULT_SERVER_TIMEOUT_MILLIS = 80000;
private final int DEFAULT_CLIENT_TIMEOUT_MILLIS = 30000;
private final int DEFAULT_MAX_HTTP_PACKET_HEADER_SIZE = 8192;
private final int DEFAULT_CONNECTION_IDLE_TIMEOUT_MILLIS = 30000;
Expand Down Expand Up @@ -76,6 +80,11 @@ class MuleHttpServerTest extends HttpServerTest<HttpServer> {
return "mule.http.server"
}

@Override
boolean reorderControllerSpan() {
true
}

void setUpTransport(FilterChain filterChain) {
TCPNIOTransportBuilder transportBuilder = TCPNIOTransportBuilder.newInstance()
.setOptimizedForMultiplexing(true)
Expand Down Expand Up @@ -115,7 +124,6 @@ class MuleHttpServerTest extends HttpServerTest<HttpServer> {
KeepAlive keepAlive = new KeepAlive();
keepAlive.setMaxRequestsCount(MAX_KEEP_ALIVE_REQUESTS);
keepAlive.setIdleTimeoutInSeconds(convertToSeconds(DEFAULT_CONNECTION_IDLE_TIMEOUT_MILLIS));

return new HttpServerFilter();
}

Expand All @@ -135,58 +143,94 @@ class MuleHttpServerTest extends HttpServerTest<HttpServer> {
final HttpContent httpContent = ctx.getMessage();
final HttpRequestPacket request = (HttpRequestPacket) httpContent.getHttpHeader();

final HttpResponsePacket response = buildResponse(request)
ctx.write(response);
final ResponseParameters responseParameters = buildResponse(request);
final HttpResponsePacket response = HttpResponsePacket.builder(request).status(responseParameters.getStatus()).build()

final String CONTENT_LENGTH = "Content-Length";

final HttpResponsePacket.Builder responsePacketBuilder = HttpResponsePacket.builder(request);
responsePacketBuilder.status(responseParameters.getStatus());

responsePacketBuilder.header(CONTENT_LENGTH, valueOf(responseParameters.getResponseBody().length));

controller(responseParameters.getEndpoint()) {
ctx.write(HttpContent.builder(responsePacketBuilder.build())
.content(wrap(ctx.getMemoryManager(), responseParameters.getResponseBody()))
.build());
}
}
return ctx.getInvokeAction();
return ctx.getStopAction();
}

public HttpResponsePacket buildResponse(HttpRequestPacket request) {
public ResponseParameters buildResponse(HttpRequestPacket request) {
final String uri = request.getRequestURI()
final String requestParams = request.getQueryString();
final String fullPath = uri + (requestParams != null ? "?" + requestParams : "");

println requestParams
println uri
println fullPath

int status
String reasonPhrase
switch (uri) {
HttpServerTest.ServerEndpoint endpoint
switch (fullPath) {
case "/success":
status = SUCCESS.status
reasonPhrase = SUCCESS.body
endpoint = SUCCESS
break
case "/redirect":
status = REDIRECT.status
reasonPhrase = REDIRECT.body
endpoint = REDIRECT
break
case "/error-status":
status = ERROR.status
reasonPhrase = ERROR.body
endpoint = ERROR
break
case "/exception":
status = EXCEPTION.status
reasonPhrase = EXCEPTION.body
endpoint = EXCEPTION
break
case "/notFound":
status = NOT_FOUND.status
reasonPhrase = NOT_FOUND.body
endpoint = NOT_FOUND
break
case "/query?some=query":
status = QUERY_PARAM.status
reasonPhrase = QUERY_PARAM.body
endpoint = QUERY_PARAM
break
case "/path/123/param":
status = PATH_PARAM.status
reasonPhrase = PATH_PARAM.body
endpoint = PATH_PARAM
break
case "/authRequired":
status = PATH_PARAM.status
reasonPhrase = PATH_PARAM.body
endpoint = AUTH_REQUIRED
break
default:
status = NOT_FOUND.status
reasonPhrase = NOT_FOUND.body
endpoint = NOT_FOUND
break
}

return HttpResponsePacket.builder(request).status(status).reasonPhrase(reasonPhrase).build();
int status = endpoint.status
String responseBody = endpoint.body

final byte[] responseBodyBytes = responseBody.getBytes(defaultCharset());
return new ResponseParameters(endpoint, status, responseBodyBytes);
}

class ResponseParameters {
HttpServerTest.ServerEndpoint endpoint
int status
byte[] responseBody

public ResponseParameters(HttpServerTest.ServerEndpoint endpoint, status, byte[] responseBody) {
this.endpoint = endpoint
this.status = status;
this.responseBody = responseBody;
}

int getStatus() {
return status;
}

byte[] getResponseBody() {
return responseBody;
}

HttpServerTest.ServerEndpoint getEndpoint() {
return endpoint;
}
}
}
}
Expand Down

0 comments on commit e40a1a6

Please sign in to comment.