Skip to content

Commit 5dcd0a8

Browse files
Merge pull request #1902 from newrelic/1750_HttpUrlConnection_memleak
Make sure we clean up the dtTracer and externalTracer when we get an …
2 parents 23ae820 + a7f653d commit 5dcd0a8

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

instrumentation/httpurlconnection/src/main/java/com/nr/agent/instrumentation/httpurlconnection/MetricState.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
import com.newrelic.agent.bridge.TracedMethod;
1212
import com.newrelic.agent.bridge.Transaction;
1313
import com.newrelic.agent.bridge.external.URISupport;
14+
import com.newrelic.api.agent.GenericParameters;
1415
import com.newrelic.api.agent.HeaderType;
1516
import com.newrelic.api.agent.HttpParameters;
1617
import com.newrelic.api.agent.OutboundHeaders;
1718

1819
import java.net.HttpURLConnection;
1920
import java.net.URI;
21+
import java.net.UnknownHostException;
2022

2123
/**
2224
* <p>
@@ -42,6 +44,7 @@
4244
*/
4345
public class MetricState {
4446
private static final String LIBRARY = "HttpURLConnection";
47+
private static final URI UNKNOWN_HOST = URI.create("UnknownHost");
4548

4649
// the guids for these tracers are swapped so the DT is attached to the tracer that
4750
// has the external
@@ -108,6 +111,24 @@ public void inboundPostamble(HttpURLConnection connection, int responseCode, Str
108111
}
109112
}
110113

114+
public void handleException(TracedMethod tracer, Exception e) {
115+
if (externalTracer != tracer) {
116+
return;
117+
}
118+
119+
if (!externalReported && e instanceof UnknownHostException) {
120+
externalTracer.reportAsExternal(GenericParameters
121+
.library(LIBRARY)
122+
.uri(UNKNOWN_HOST)
123+
.procedure("failed")
124+
.build());
125+
externalReported = true;
126+
}
127+
128+
dtTracer = null;
129+
externalTracer = null;
130+
}
131+
111132
/**
112133
* Calls the reportAsExternal API. This results in a Span being created for the current TracedMethod/Segment and the Span
113134
* category being set to http which represents a Span that made an external http request. This is required for external

instrumentation/httpurlconnection/src/main/java/java/net/HttpURLConnection.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ public synchronized InputStream getInputStream() throws IOException {
7878
// This does a network request (if getResponseCode() wasn't called first)
7979
inputStream = Weaver.callOriginal();
8080
} catch (Exception e) {
81-
// This is the default legacy behavior of the AbstractExternalComponentTracer
82-
if (e instanceof UnknownHostException) {
83-
method.setMetricName("External", "UnknownHost", "HttpURLConnection");
84-
}
81+
metricState.handleException(method, e);
8582
throw e;
8683
}
8784

@@ -103,10 +100,7 @@ public int getResponseCode() throws Exception {
103100
// This does a network request (if getInputStream() wasn't called first)
104101
responseCodeValue = Weaver.callOriginal();
105102
} catch (Exception e) {
106-
// This is the default legacy behavior of the AbstractExternalComponentTracer
107-
if (e instanceof UnknownHostException) {
108-
method.setMetricName("External", "UnknownHost", "HttpURLConnection");
109-
}
103+
metricState.handleException(method, e);
110104
throw e;
111105
}
112106

@@ -123,9 +117,7 @@ public String getResponseMessage() throws IOException {
123117
try {
124118
responseMessageValue = Weaver.callOriginal();
125119
} catch (Exception e) {
126-
if (e instanceof UnknownHostException) {
127-
method.setMetricName("External", "UnknownHost", "HttpURLConnection");
128-
}
120+
metricState.handleException(method, e);
129121
throw e;
130122
}
131123
metricState.inboundPostamble(this, responseCode, responseMessageValue, GET_RESPONSE_MSG, method);

0 commit comments

Comments
 (0)