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

Fix multiple spans being created for HTTPUrlConnection HEAD requests #3353

Merged
merged 4 commits into from
Oct 10, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:

=== Unreleased

[float]
===== Bug fixes
* Fixed too many spans being created for `HTTPUrlConnection` requests with method `HEAD` - {pull}3353[#3353]

[[release-notes-1.x]]
=== Java Agent version 1.x

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@ public static class AdviceClass {
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
public static Object enter(@Advice.This HttpURLConnection thiz,
@Advice.FieldValue("connected") boolean connected,
@Advice.FieldValue("responseCode") int responseCode,
@Advice.Origin String signature) {

//With HEAD requests the connected stays false
Copy link
Member

Choose a reason for hiding this comment

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

[minor] that's really an implementation detail here, could we also check the HTTP verb to be sure ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd argue that even for non-HEAD requests this approach is safer: If a responseCode is available, this implies that the connection has already been made. Therefore I think an additional check would not be good here.

Copy link
Member

Choose a reason for hiding this comment

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

Good for me, let's keep it as-is then !

boolean actuallyConnected = connected || responseCode != -1;

boolean isNestedCall = callDepth.isNestedCallAndIncrement();
ElasticContext<?> activeContext = tracer.currentContext();
AbstractSpan<?> parentSpan = activeContext.getSpan();
Span<?> span = null;
if (parentSpan != null) {
span = inFlightSpans.get(thiz);
if (span == null && !connected) {
if (span == null && !actuallyConnected) {
final URL url = thiz.getURL();
span = HttpClientHelper.startHttpClientSpan(activeContext, thiz.getRequestMethod(), url.toString(), url.getProtocol(), url.getHost(), url.getPort());
}
Expand All @@ -98,7 +102,7 @@ public static Object enter(@Advice.This HttpURLConnection thiz,
}
}

if (!isNestedCall && !connected) {
if (!isNestedCall && !actuallyConnected) {
tracer.currentContext().propagateContext(thiz, UrlConnectionPropertyAccessor.instance(), UrlConnectionPropertyAccessor.instance());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public void testMultipleConnect() throws Exception {
expectSpan("/").verify();
}

@Test
public void testMultipleGetResponseCodeWithHead() throws Exception {
final HttpURLConnection urlConnection = (HttpURLConnection) new URL(getBaseUrl() + "/").openConnection();
urlConnection.setRequestMethod("HEAD");
//Call twice to make sure we don't create two spans
urlConnection.getResponseCode();
urlConnection.getResponseCode();
expectSpan("/").verify();
}

@Test
public void testGetOutputStream() throws Exception {
final HttpURLConnection urlConnection = (HttpURLConnection) new URL(getBaseUrl() + "/").openConnection();
Expand Down
Loading