Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify HttpUrlConnection instrumentation #5673

Merged
merged 1 commit into from
Mar 23, 2022
Merged
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 @@ -69,10 +69,6 @@ public static void methodEnter(
// top-level HttpURLConnection calls
return;
}
// double increment on first entry is used to prevent infinite recursion in case end()
// captures response headers due to HttpUrlConnection.getHeaderField() calling
// HttpUrlConnection.getInputStream() which then enters this advice again
callDepth.getAndIncrement();

Context parentContext = currentContext();
if (!instrumenter().shouldStart(parentContext, connection)) {
Expand Down Expand Up @@ -108,16 +104,16 @@ public static void methodExit(
@Advice.Local("otelHttpUrlState") HttpUrlState httpUrlState,
@Advice.Local("otelScope") Scope scope,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
// checking against 1 instead of against 0, because of the double increment which is used to
// prevent infinite recursion in case end() captures response headers
if (callDepth.decrementAndGet() > 1) {
if (callDepth.decrementAndGet() > 0) {
return;
}
if (scope == null) {
// need to perform the second decrement here since bailing out early
callDepth.decrementAndGet();
return;
}
// prevent infinite recursion in case end() captures response headers due to
// HttpUrlConnection.getHeaderField() calling HttpUrlConnection.getInputStream() which then
// enters this advice again
callDepth.getAndIncrement();
Comment on lines +113 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much nicer 👍

try {
scope.close();

Expand All @@ -144,8 +140,6 @@ public static void methodExit(
httpUrlState.finished = true;
}
} finally {
// double increment is used to prevent infinite recursion in case end() captures response
// headers
callDepth.decrementAndGet();
}
}
Expand Down