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

Changing HttpUrlConnection instr to not rely on segment/cleanup thread #1537

Merged
merged 3 commits into from
Oct 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class HttpURLConnectionTest {
static final TransactionDataList transactions = new TransactionDataList();
private static final String GET_OUTPUT_STREAM = "getOutputStream";
private static final String GET_INPUT_STREAM = "getInputStream";
private static final String GET_RESPONSE_CODE = "getResponseCode";
private static final String GET_RESPONSE_MSG = "getResponseMessage";
private static final String URL = "example.com";
private static final String REAL_HOST = "https://example.com";
private static final String FAKE_HOST = "http://www.thishostdoesnotexistbro.com"; // Better hope no one buys this domain...
Expand Down Expand Up @@ -95,7 +97,7 @@ public void outputStreamFirstTest() {
doOutputStreamFirstTest();
String scope = format("OtherTransaction/Custom/{0}/doOutputStreamFirstTest", TEST_CLASS);

verifyMetrics(URL, scope, true, GET_OUTPUT_STREAM);
verifyMetrics(URL, scope, false, GET_OUTPUT_STREAM);
}

@Trace(dispatcher = true)
Expand Down Expand Up @@ -130,7 +132,7 @@ public void connectFirstTest() {
doConnectFirstTest();
String scope = format("OtherTransaction/Custom/{0}/doConnectFirstTest", TEST_CLASS);

verifyMetrics(URL, scope, true, GET_OUTPUT_STREAM);
verifyMetrics(URL, scope, false, GET_OUTPUT_STREAM);
}

@Trace(dispatcher = true)
Expand Down Expand Up @@ -255,7 +257,7 @@ public void testHttpURLConnectionMetrics3() {
run3();
String scope = format("OtherTransaction/Custom/{0}/run3", TEST_CLASS);

verifyMetrics(URL, scope, true, GET_INPUT_STREAM);
verifyMetrics(URL, scope, true, GET_RESPONSE_CODE);
}

@Trace(dispatcher = true)
Expand Down Expand Up @@ -330,7 +332,7 @@ public void testHttpURLConnectionMetrics6() {
run6();
String scope = format("OtherTransaction/Custom/{0}/run6", TEST_CLASS);

verifyMetrics(URL, scope, true, GET_OUTPUT_STREAM);
verifyMetrics(URL, scope, false, GET_OUTPUT_STREAM);
}

@Trace(dispatcher = true)
Expand All @@ -355,7 +357,7 @@ public void testHttpURLConnectionMetrics7() {
run7();
String scope = format("OtherTransaction/Custom/{0}/run7", TEST_CLASS);

verifyMetrics(URL, scope, true, GET_OUTPUT_STREAM);
verifyMetrics(URL, scope, true, GET_RESPONSE_CODE);
}

@Trace(dispatcher = true)
Expand Down Expand Up @@ -383,7 +385,7 @@ public void testHttpURLConnectionMetrics8() {
run8();
String scope = format("OtherTransaction/Custom/{0}/run8", TEST_CLASS);

verifyMetrics(URL, scope, true, GET_OUTPUT_STREAM);
verifyMetrics(URL, scope, true, GET_RESPONSE_CODE);
}

@Trace(dispatcher = true)
Expand All @@ -410,7 +412,7 @@ public void testHttpURLConnectionMetrics9() {
run9();
String scope = format("OtherTransaction/Custom/{0}/run9", TEST_CLASS);

verifyMetrics(URL, scope, true, GET_INPUT_STREAM);
verifyMetrics(URL, scope, true, GET_RESPONSE_CODE);
}

@Trace(dispatcher = true)
Expand All @@ -430,6 +432,31 @@ private void run9() {
}
}

@Test
public void testHttpURLConnectionMetrics10() {
run10();
String scope = format("OtherTransaction/Custom/{0}/run10", TEST_CLASS);

verifyMetrics(URL, scope, true, GET_RESPONSE_MSG);
}

@Trace(dispatcher = true)
private void run10() {
HttpURLConnection connection = null;
try {
// Test 9: getResponseMessage()
connection = getHttpsExampleComConnection();
// GETs URL and reads response message (Network I/O)
System.out.println("Response message: " + connection.getResponseMessage());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

@Test
public void testUnresolvedHost() throws Exception {
unresolvedHost();
Expand Down Expand Up @@ -475,7 +502,8 @@ private void verifyMetrics(String url, String scope, boolean reportedExternalCal
Set<String> metrics = AgentHelper.getMetrics();
// Scoped external metrics
String httpURLConnectionGetInputStreamMetric = format("External/{0}/HttpURLConnection/getInputStream", url);
String httpURLConnectionGetOutputStreamMetric = format("External/{0}/HttpURLConnection/getOutputStream", url);
String httpURLConnectionGetResponseCodeMetric = format("External/{0}/HttpURLConnection/getResponseCode", url);
String httpURLConnectionGetResponseMessageMetric = format("External/{0}/HttpURLConnection/getResponseMessage", url);
// Unscoped external metrics
String externalHostAllMetric = format("External/{0}/all", url);
String externalAllMetric = "External/all";
Expand All @@ -488,8 +516,7 @@ private void verifyMetrics(String url, String scope, boolean reportedExternalCal
scopedMetricCount = 1;
// One of these scoped metrics should be generated when an external call is reported
Assert.assertTrue(metrics.toString(),
metrics.contains(httpURLConnectionGetOutputStreamMetric) ||
metrics.contains(httpURLConnectionGetInputStreamMetric));
metrics.contains(format("External/{0}/HttpURLConnection/" + methodInExternalMetric, url)));

unscopedMetricCount = 3;
// All three of these unscoped metrics should be generated when an external call is reported
Expand All @@ -498,25 +525,27 @@ private void verifyMetrics(String url, String scope, boolean reportedExternalCal
Assert.assertTrue(metrics.toString(), metrics.contains(externalAllOtherMetric));
} else {
Assert.assertFalse(metrics.toString(),
metrics.contains(httpURLConnectionGetOutputStreamMetric) ||
metrics.contains(httpURLConnectionGetInputStreamMetric));
metrics.contains(format("External/{0}/HttpURLConnection/" + methodInExternalMetric, url)));

Assert.assertFalse(metrics.toString(), metrics.contains(externalHostAllMetric));
Assert.assertFalse(metrics.toString(), metrics.contains(externalAllMetric));
Assert.assertFalse(metrics.toString(), metrics.contains(externalAllOtherMetric));
}

Map<String, Integer> scopedMetricCounts = getMetricCounts(
MetricName.create(httpURLConnectionGetOutputStreamMetric, scope),
MetricName.create(httpURLConnectionGetInputStreamMetric, scope));
MetricName.create(httpURLConnectionGetInputStreamMetric, scope),
MetricName.create(httpURLConnectionGetResponseCodeMetric, scope),
MetricName.create(httpURLConnectionGetResponseMessageMetric, scope)
);

Map<String, Integer> unscopedMetricCounts = getMetricCounts(
MetricName.create(externalHostAllMetric),
MetricName.create(externalAllMetric),
MetricName.create(externalAllOtherMetric));

int actualHttpURLConnectionScopedMetricCount = scopedMetricCounts.get(httpURLConnectionGetOutputStreamMetric) +
scopedMetricCounts.get(httpURLConnectionGetInputStreamMetric);
int actualHttpURLConnectionScopedMetricCount = scopedMetricCounts.get(httpURLConnectionGetInputStreamMetric) +
scopedMetricCounts.get(httpURLConnectionGetResponseCodeMetric) +
scopedMetricCounts.get(httpURLConnectionGetResponseMessageMetric);

int actualHttpURLConnectionUnscopedMetricCount = unscopedMetricCounts.get(externalHostAllMetric) +
unscopedMetricCounts.get(externalAllMetric) +
Expand All @@ -525,15 +554,17 @@ private void verifyMetrics(String url, String scope, boolean reportedExternalCal
assertEquals(scopedMetricCount, actualHttpURLConnectionScopedMetricCount);

if (scopedMetricCount == 0) {
assertEquals(0, (int) scopedMetricCounts.get(httpURLConnectionGetOutputStreamMetric));
assertEquals(0, (int) scopedMetricCounts.get(httpURLConnectionGetInputStreamMetric));
} else {
if (methodInExternalMetric != null) {
if (methodInExternalMetric.equals(GET_INPUT_STREAM)) {
assertEquals(1, (int) scopedMetricCounts.get(httpURLConnectionGetInputStreamMetric));
}
if (methodInExternalMetric.equals(GET_OUTPUT_STREAM)) {
assertEquals(1, (int) scopedMetricCounts.get(httpURLConnectionGetOutputStreamMetric));
if (methodInExternalMetric.equals(GET_RESPONSE_CODE)) {
assertEquals(1, (int) scopedMetricCounts.get(httpURLConnectionGetResponseCodeMetric));
}
if (methodInExternalMetric.equals(GET_RESPONSE_MSG)) {
assertEquals(1, (int) scopedMetricCounts.get(httpURLConnectionGetResponseMessageMetric));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public void httpURLConnection() throws Exception {
httpURLConnectionTx();

Set<String> metrics = AgentHelper.getMetrics();
assertTrue(metrics.toString(), metrics.contains("External/" + HOST + "/HttpURLConnection/getInputStream"));
assertTrue(metrics.toString(), metrics.contains("External/" + HOST + "/HttpURLConnection/getResponseCode"));

Map<String, Integer> metricCounts = getMetricCounts(
MetricName.create("External/" + HOST + "/HttpURLConnection/getInputStream",
MetricName.create("External/" + HOST + "/HttpURLConnection/getResponseCode",
"OtherTransaction/Custom/test.newrelic.test.agent.HttpCommonsTest/httpURLConnectionTx"));

assertEquals(1, (int) metricCounts.get("External/" + HOST + "/HttpURLConnection/getInputStream"));
assertEquals(1, (int) metricCounts.get("External/" + HOST + "/HttpURLConnection/getResponseCode"));
}

@Trace(dispatcher = true)
Expand Down
Loading
Loading