Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import net.bytebuddy.asm.Advice;
import org.apache.axis2.context.MessageContext;
import org.apache.http.HttpResponse;
import org.apache.http.nio.NHttpClientConnection;
import org.apache.synapse.transport.passthru.TargetContext;

Expand Down Expand Up @@ -113,8 +114,8 @@ public static final class ClientResponseAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static ContextScope beginResponse(
@Advice.Argument(0) final NHttpClientConnection connection) {
// check and remove context so it won't be finished twice
Context context = (Context) connection.getContext().removeAttribute(SYNAPSE_CONTEXT_KEY);
// don't remove stored context here because the response callback may run multiple times
Context context = (Context) connection.getContext().getAttribute(SYNAPSE_CONTEXT_KEY);
if (null != context) {
return context.attach();
}
Expand All @@ -129,15 +130,30 @@ public static void responseReceived(
if (null == scope) {
return;
}
AgentSpan span = spanFromContext(scope.context());
DECORATE.onResponse(span, connection.getHttpResponse());
final AgentSpan span = spanFromContext(scope.context());
final HttpResponse httpResponse = connection.getHttpResponse();
boolean isFinal = false;
if (httpResponse != null && httpResponse.getStatusLine() != null) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
// 1xx (e.g. 100 Continue) - not final response
isFinal = statusCode >= 200;
}

if (isFinal) {
DECORATE.onResponse(span, httpResponse);
}
if (null != error) {
DECORATE.onError(span, error);
}
DECORATE.beforeFinish(scope.context());
scope.close();
if (span != null) {
span.finish();
if ((isFinal || error != null)
&& connection.getContext().removeAttribute(SYNAPSE_CONTEXT_KEY) != null) {
DECORATE.beforeFinish(scope.context());
scope.close();
if (span != null) {
span.finish();
}
} else {
scope.close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import net.bytebuddy.asm.Advice;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.nio.NHttpServerConnection;

@AutoService(InstrumenterModule.class)
Expand Down Expand Up @@ -92,8 +93,8 @@ public static final class ServerResponseAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static ContextScope beginResponse(
@Advice.Argument(0) final NHttpServerConnection connection) {
// check and remove context so it won't be finished twice
Context context = (Context) connection.getContext().removeAttribute(SYNAPSE_CONTEXT_KEY);
// don't remove stored context here because the response callback may run multiple times
Context context = (Context) connection.getContext().getAttribute(SYNAPSE_CONTEXT_KEY);
if (null != context) {
return context.attach();
}
Expand All @@ -108,14 +109,28 @@ public static void responseReady(
if (null == scope) {
return;
}
AgentSpan span = spanFromContext(scope.context());
DECORATE.onResponse(span, connection.getHttpResponse());
final AgentSpan span = spanFromContext(scope.context());
final HttpResponse httpResponse = connection.getHttpResponse();
boolean isFinal = false;
if (httpResponse != null && httpResponse.getStatusLine() != null) {
isFinal = httpResponse.getStatusLine().getStatusCode() >= 200;
}

if (isFinal) {
DECORATE.onResponse(span, httpResponse);
}
if (null != error) {
DECORATE.onError(span, error);
}
DECORATE.beforeFinish(scope.context());
scope.close();
span.finish();

if ((isFinal || error != null)
&& connection.getContext().removeAttribute(SYNAPSE_CONTEXT_KEY) != null) {
DECORATE.beforeFinish(scope.context());
scope.close();
span.finish();
} else {
scope.close();
}
}
}

Expand Down