Skip to content

Commit

Permalink
Add synthetic metric dimension smoke test (#2528)
Browse files Browse the repository at this point in the history
Co-authored-by: Helen <[email protected]>
  • Loading branch information
trask and heyams authored Sep 17, 2022
1 parent 3b36f75 commit bc353f0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ abstract class HttpPreaggregatedMetricsTest {
@Test
@TargetUri("/httpUrlConnection")
void testHttpUrlConnection() throws Exception {
testHttpUrlConnectionAndSynthetic(false);
}

@Test
@TargetUri(value = "/httpUrlConnection", userAgent = "AlwaysOn")
void testHttpUrlConnectionAndSynthetic() throws Exception {
testHttpUrlConnectionAndSynthetic(true);
}

private void testHttpUrlConnectionAndSynthetic(boolean synthetic) throws Exception {
verifyHttpclientRequestsAndDependencies("https://mock.codes/200?q=spaces%20test");

List<Envelope> clientMetrics =
Expand All @@ -41,7 +51,7 @@ void testHttpUrlConnection() throws Exception {
testing.mockedIngestion.waitForMetricItems("http.server.duration", 1);

verifyHttpClientPreAggregatedMetrics(clientMetrics);
verifyHttpServerPreAggregatedMetrics(serverMetrics);
verifyHttpServerPreAggregatedMetrics(serverMetrics, synthetic);
}

private static void verifyHttpclientRequestsAndDependencies(String successUrlWithQueryString)
Expand Down Expand Up @@ -113,28 +123,29 @@ private static void verifyHttpClientPreAggregatedMetrics(List<Envelope> metrics)
Envelope envelope1 = metrics.get(0);
validateTags(envelope1);
MetricData md1 = (MetricData) ((Data<?>) envelope1.getData()).getBaseData();
validateMetricData("client", md1, "200");
validateMetricData("client", md1, "200", false);

// 2nd pre-aggregated metric
Envelope envelope2 = metrics.get(1);
validateTags(envelope2);
MetricData md2 = (MetricData) ((Data<?>) envelope2.getData()).getBaseData();
validateMetricData("client", md2, "404");
validateMetricData("client", md2, "404", false);

// 3rd pre-aggregated metric
Envelope envelope3 = metrics.get(2);
validateTags(envelope3);
MetricData md3 = (MetricData) ((Data<?>) envelope3.getData()).getBaseData();
validateMetricData("client", md3, "500");
validateMetricData("client", md3, "500", false);
}

private static void verifyHttpServerPreAggregatedMetrics(List<Envelope> metrics) {
private static void verifyHttpServerPreAggregatedMetrics(
List<Envelope> metrics, boolean synthetic) {
assertThat(metrics.size()).isEqualTo(1);
// 1st pre-aggregated metric
Envelope envelope1 = metrics.get(0);
validateTags(envelope1);
MetricData md1 = (MetricData) ((Data<?>) envelope1.getData()).getBaseData();
validateMetricData("server", md1, "200");
validateMetricData("server", md1, "200", synthetic);
}

private static void validateTags(Envelope envelope) {
Expand All @@ -144,7 +155,8 @@ private static void validateTags(Envelope envelope) {
assertThat(tags).containsEntry("ai.cloud.role", "testrolename");
}

private static void validateMetricData(String type, MetricData metricData, String resultCode) {
private static void validateMetricData(
String type, MetricData metricData, String resultCode, boolean synthetic) {
List<DataPoint> dataPoints = metricData.getMetrics();
assertThat(dataPoints).hasSize(1);
DataPoint dataPoint = dataPoints.get(0);
Expand All @@ -161,13 +173,14 @@ private static void validateMetricData(String type, MetricData metricData, Strin
assertThat(properties.get("Dependency.Success")).isEqualTo(expectedSuccess);
assertThat(properties.get("dependency/target")).isEqualTo("mock.codes");
assertThat(properties.get("Dependency.Type")).isEqualTo("Http");
assertThat(properties.get("operation/synthetic")).isEqualTo("False");
} else {
assertThat(properties).hasSize(7);
assertThat(properties.get("_MS.MetricId")).isEqualTo("requests/duration");
assertThat(properties.get("request/resultCode")).isEqualTo(resultCode);
assertThat(properties.get("Request.Success")).isEqualTo(expectedSuccess);
assertThat(properties.get("operation/synthetic")).isEqualTo(synthetic ? "True" : "False");
}
assertThat(properties.get("operation/synthetic")).isEqualTo("False");
assertThat(properties.get("cloud/roleInstance")).isEqualTo("testroleinstance");
assertThat(properties.get("cloud/roleName")).isEqualTo("testrolename");
assertThat(properties.get("_MS.IsAutocollected")).isEqualTo("True");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ public static int getResponseCodeEnsuringSampled(String url) throws IOException
return getResponseCode(httpGet);
}

public static String get(String url) throws IOException {
return getBody(new HttpGet(url));
public static String get(String url, String userAgent) throws IOException {
HttpGet httpGet = new HttpGet(url);
if (!userAgent.isEmpty()) {
httpGet.setHeader("User-Agent", userAgent);
}
return getBody(httpGet);
}

private static String getBody(HttpGet httpGet) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
System.out.println("calling " + url + " " + targetUri.callCount() + " times");
}
for (int i = 0; i < targetUri.callCount(); i++) {
HttpHelper.get(url);
HttpHelper.get(url, targetUri.userAgent());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@

/** The number of times to call the target uri. */
int callCount() default 1;

String userAgent() default "";
}

0 comments on commit bc353f0

Please sign in to comment.