Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -51,10 +51,12 @@ public static class ExecuteMethodAdvice {
public static class AdviceScope {
private final Context context;
private final Scope scope;
private final HttpMethod httpMethod;

private AdviceScope(Context context, Scope scope) {
private AdviceScope(Context context, Scope scope, HttpMethod httpMethod) {
this.context = context;
this.scope = scope;
this.httpMethod = httpMethod;
}

@Nullable
Expand All @@ -64,10 +66,10 @@ public static AdviceScope start(HttpMethod httpMethod) {
return null;
}
Context context = instrumenter().start(parentContext, httpMethod);
return new AdviceScope(context, context.makeCurrent());
return new AdviceScope(context, context.makeCurrent(), httpMethod);
}

public void end(HttpMethod httpMethod, Throwable throwable) {
public void end(Throwable throwable) {
scope.close();
instrumenter().end(context, httpMethod, httpMethod, throwable);
}
Expand All @@ -80,12 +82,11 @@ public static AdviceScope methodEnter(@Advice.Argument(1) HttpMethod httpMethod)

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Argument(1) HttpMethod httpMethod,
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {

if (adviceScope != null) {
adviceScope.end(httpMethod, throwable);
adviceScope.end(throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
import static io.opentelemetry.javaagent.instrumentation.apachehttpclient.v5_0.ApacheHttpClientSingletons.instrumenter;

import io.opentelemetry.context.Context;
import javax.annotation.Nullable;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.HttpResponse;

public class ApacheHttpClientHelper {

public static void doMethodExit(
Context context, ClassicHttpRequest request, Object result, Throwable throwable) {
Context context,
ClassicHttpRequest request,
@Nullable Object result,
@Nullable Throwable throwable) {
if (throwable != null) {
instrumenter().end(context, request, null, throwable);
} else if (result instanceof HttpResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ public void transform(TypeTransformer transformer) {
this.getClass().getName() + "$RequestWithHostAndContextAndHandlerAdvice");
}

public static class AdviceLocals {
public static class AdviceScope {
public final ClassicHttpRequest request;
public final Context parentContext;
public final Context context;
public final Scope scope;

private AdviceLocals(
private AdviceScope(
ClassicHttpRequest request, Context parentContext, Context context, Scope scope) {
this.request = request;
this.context = context;
Expand All @@ -139,39 +139,44 @@ private AdviceLocals(
}

@Nullable
public static AdviceLocals start(ClassicHttpRequest request) {
public static AdviceScope start(ClassicHttpRequest request) {
Context parentContext = currentContext();

if (!instrumenter().shouldStart(parentContext, request)) {
return null;
}
Context context = instrumenter().start(parentContext, request);
return new AdviceLocals(request, parentContext, context, context.makeCurrent());
return new AdviceScope(request, parentContext, context, context.makeCurrent());
}

public void end(Object result, Throwable throwable) {
public void end(@Nullable Object result, @Nullable Throwable throwable) {
scope.close();
ApacheHttpClientHelper.doMethodExit(context, request, result, throwable);
}

public WrappingStatusSettingResponseHandler<?> wrapResponseHandler(
HttpClientResponseHandler<?> handler) {
return new WrappingStatusSettingResponseHandler<>(context, parentContext, request, handler);
}
}

@SuppressWarnings("unused")
public static class RequestAdvice {

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceLocals methodEnter(@Advice.Argument(0) ClassicHttpRequest request) {
return AdviceLocals.start(request);
public static AdviceScope methodEnter(@Advice.Argument(0) ClassicHttpRequest request) {
return AdviceScope.start(request);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Return Object result,
@Advice.Thrown Throwable throwable,
@Advice.Enter @Nullable AdviceLocals locals) {
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope scope) {

if (locals != null) {
locals.end(result, throwable);
if (scope != null) {
scope.end(result, throwable);
}
}
}
Expand All @@ -186,30 +191,28 @@ public static Object[] methodEnter(
@Advice.Argument(1) HttpClientResponseHandler<?> originalHandler) {

HttpClientResponseHandler<?> handler = originalHandler;
AdviceLocals locals = AdviceLocals.start(request);
if (locals == null) {
AdviceScope scope = AdviceScope.start(request);
if (scope == null) {
return new Object[] {null, handler};
}

// Wrap the handler so we capture the status code
if (handler != null) {
handler =
new WrappingStatusSettingResponseHandler<>(
locals.context, locals.parentContext, request, handler);
handler = scope.wrapResponseHandler(handler);
}
return new Object[] {locals, handler};
return new Object[] {scope, handler};
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Argument(0) ClassicHttpRequest request,
@Advice.Return Object result,
@Advice.Thrown Throwable throwable,
@Advice.Return @Nullable Object result,
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter Object[] enterResult) {

AdviceLocals locals = (AdviceLocals) enterResult[0];
if (locals != null) {
locals.end(result, throwable);
AdviceScope scope = (AdviceScope) enterResult[0];
if (scope != null) {
scope.end(result, throwable);
}
}
}
Expand All @@ -224,30 +227,28 @@ public static Object[] methodEnter(
@Advice.Argument(2) HttpClientResponseHandler<?> originalHandler) {

HttpClientResponseHandler<?> handler = originalHandler;
AdviceLocals locals = AdviceLocals.start(request);
if (locals == null) {
AdviceScope scope = AdviceScope.start(request);
if (scope == null) {
return new Object[] {null, handler};
}

// Wrap the handler so we capture the status code
if (handler != null) {
handler =
new WrappingStatusSettingResponseHandler<>(
locals.context, locals.parentContext, request, handler);
handler = scope.wrapResponseHandler(handler);
}
return new Object[] {locals, handler};
return new Object[] {scope, handler};
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Argument(0) ClassicHttpRequest request,
@Advice.Return Object result,
@Advice.Thrown Throwable throwable,
@Advice.Return @Nullable Object result,
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter Object[] enterResult) {

AdviceLocals locals = (AdviceLocals) enterResult[0];
if (locals != null) {
locals.end(result, throwable);
AdviceScope scope = (AdviceScope) enterResult[0];
if (scope != null) {
scope.end(result, throwable);
}
}
}
Expand All @@ -257,20 +258,20 @@ public static class RequestWithHostAdvice {

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceLocals methodEnter(
public static AdviceScope methodEnter(
@Advice.Argument(0) HttpHost host, @Advice.Argument(1) ClassicHttpRequest request) {

return AdviceLocals.start(new RequestWithHost(host, request));
return AdviceScope.start(new RequestWithHost(host, request));
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Return Object result,
@Advice.Thrown Throwable throwable,
@Advice.Enter @Nullable AdviceLocals locals) {
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope scope) {

if (locals != null) {
locals.end(result, throwable);
if (scope != null) {
scope.end(result, throwable);
}
}
}
Expand All @@ -286,30 +287,28 @@ public static Object[] methodEnter(
@Advice.Argument(2) HttpClientResponseHandler<?> originalHandler) {

HttpClientResponseHandler<?> handler = originalHandler;
AdviceLocals locals = AdviceLocals.start(new RequestWithHost(host, request));
AdviceScope scope = AdviceScope.start(new RequestWithHost(host, request));

if (locals == null) {
if (scope == null) {
return new Object[] {null, handler};
}

// Wrap the handler so we capture the status code
if (handler != null) {
handler =
new WrappingStatusSettingResponseHandler<>(
locals.context, locals.parentContext, locals.request, handler);
scope.wrapResponseHandler(handler);
}
return new Object[] {locals, handler};
return new Object[] {scope, handler};
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Return Object result,
@Advice.Thrown Throwable throwable,
@Advice.Return @Nullable Object result,
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter Object[] enterResult) {

AdviceLocals locals = (AdviceLocals) enterResult[0];
if (locals != null) {
locals.end(result, throwable);
AdviceScope scope = (AdviceScope) enterResult[0];
if (scope != null) {
scope.end(result, throwable);
}
}
}
Expand All @@ -325,19 +324,17 @@ public static Object[] methodEnter(
@Advice.Argument(3) HttpClientResponseHandler<?> originalHandler) {

HttpClientResponseHandler<?> handler = originalHandler;
AdviceLocals locals = AdviceLocals.start(new RequestWithHost(host, request));
AdviceScope scope = AdviceScope.start(new RequestWithHost(host, request));

if (locals == null) {
if (scope == null) {
return new Object[] {null, handler};
}

// Wrap the handler so we capture the status code
if (handler != null) {
handler =
new WrappingStatusSettingResponseHandler<>(
locals.context, locals.parentContext, locals.request, handler);
handler = scope.wrapResponseHandler(handler);
}
return new Object[] {locals, handler};
return new Object[] {scope, handler};
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand All @@ -346,9 +343,9 @@ public static void methodExit(
@Advice.Thrown Throwable throwable,
@Advice.Enter Object[] enterResult) {

AdviceLocals locals = (AdviceLocals) enterResult[0];
if (locals != null) {
locals.end(result, throwable);
AdviceScope scope = (AdviceScope) enterResult[0];
if (scope != null) {
scope.end(result, throwable);
}
}
}
Expand Down