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

Update and Fix Tests #37225

Merged
merged 13 commits into from
Dec 7, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/monitor/azure-monitor-opentelemetry-exporter",
"Tag": "java/monitor/azure-monitor-opentelemetry-exporter_61683f4a82"
"Tag": "java/monitor/azure-monitor-opentelemetry-exporter_c852f179b0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,32 +217,22 @@ public void testStatsbeat() throws Exception {
assertThat(customValidationPolicy.url)
.isEqualTo(new URL("https://westus-0.in.applicationinsights.azure.com/v2.1/track"));

TelemetryItem attachStatsbeat =
customValidationPolicy.actualTelemetryItems.stream()
.filter(item -> item.getName().equals("Statsbeat"))
.filter(item -> {
MetricsData metricsData = (MetricsData) item.getData().getBaseData();
return metricsData.getMetrics().stream().allMatch(metricDataPoint -> metricDataPoint.getName().equals("Attach"));
})
.findFirst()
.get();
validateAttachStatsbeat(attachStatsbeat);

TelemetryItem featureStatsbeat =
customValidationPolicy.actualTelemetryItems.stream()
.filter(item -> item.getName().equals("Statsbeat"))
.filter(item -> {
MetricsData metricsData = (MetricsData) item.getData().getBaseData();
return metricsData.getMetrics().stream().allMatch(metricDataPoint -> metricDataPoint.getName().equals("Feature"));
})
.findFirst()
.get();
validateFeatureStatsbeat(featureStatsbeat);
verifyStatsbeatTelemetry(customValidationPolicy);
}

@Test
public void testStatsbeatShutdownWhen400InvalidIKeyReturned() throws Exception {
String fakeBody = "{\"itemsReceived\":1,\"itemsAccepted\":0,\"errors\":[{\"index\":0,\"statusCode\":400,\"message\":\"Invalid instrumentation key\"}]}";
String fakeBody = "{\"itemsReceived\":4,\"itemsAccepted\":0,\"errors\":[{\"index\":0,\"statusCode\":400,\"message\":\"Invalid instrumentation key\"},{\"index\":1,\"statusCode\":400,\"message\":\"Invalid instrumentation key\"},{\"index\":2,\"statusCode\":400,\"message\":\"Invalid instrumentation key\"},{\"index\":3,\"statusCode\":400,\"message\":\"Invalid instrumentation key\"}]}";
verifyStatsbeatShutdownOrnNot(fakeBody, true);
}

@Test
public void testStatsbeatNotShutDownWhen400InvalidDataReturned() throws Exception {
String fakeBody = "{\"itemsReceived\":1,\"itemsAccepted\":0,\"errors\":[{\"index\":0,\"statusCode\":400,\"message\":\"102: Field 'time' on type 'Envelope' is not a valid time string. Expected: date, Actual: fake\"}]}";
verifyStatsbeatShutdownOrnNot(fakeBody, false);
}

private void verifyStatsbeatShutdownOrnNot(String fakeBody, boolean shutdown) throws Exception {
MockedHttpClient mockedHttpClient =
new MockedHttpClient(
request -> {
Expand All @@ -254,20 +244,49 @@ public void testStatsbeatShutdownWhen400InvalidIKeyReturned() throws Exception {
AzureMonitorExportersEndToEndTest.CustomValidationPolicy customValidationPolicy = new AzureMonitorExportersEndToEndTest.CustomValidationPolicy(countDownLatch);
OpenTelemetrySdk openTelemetrySdk =
TestUtils.createOpenTelemetrySdk(
getHttpPipeline(customValidationPolicy, mockedHttpClient), getConfiguration(), STATSBEAT_CONNECTION_STRING);
getHttpPipeline(customValidationPolicy, mockedHttpClient), getStatsbeatConfiguration(), STATSBEAT_CONNECTION_STRING);

generateMetric(openTelemetrySdk);

// close to flush
openTelemetrySdk.close();

Thread.sleep(1000);
Thread.sleep(2000);

// wait for export
countDownLatch.await(10, SECONDS);
assertThat(customValidationPolicy.url)
.isEqualTo(new URL("https://westus-0.in.applicationinsights.azure.com/v2.1/track"));
assertThat(customValidationPolicy.actualTelemetryItems.stream().filter(item -> item.getName().equals("Statsbeat")).count()).isEqualTo(0);

if (shutdown) {
assertThat(customValidationPolicy.actualTelemetryItems.stream().filter(item -> item.getName().equals("Statsbeat")).count()).isEqualTo(0);
} else {
verifyStatsbeatTelemetry(customValidationPolicy);
}
}

private void verifyStatsbeatTelemetry(CustomValidationPolicy customValidationPolicy) {
TelemetryItem attachStatsbeat =
customValidationPolicy.actualTelemetryItems.stream()
.filter(item -> item.getName().equals("Statsbeat"))
.filter(item -> {
MetricsData metricsData = (MetricsData) item.getData().getBaseData();
return metricsData.getMetrics().stream().allMatch(metricDataPoint -> metricDataPoint.getName().equals("Attach"));
})
.findFirst()
.get();
validateAttachStatsbeat(attachStatsbeat);

TelemetryItem featureStatsbeat =
customValidationPolicy.actualTelemetryItems.stream()
.filter(item -> item.getName().equals("Statsbeat"))
.filter(item -> {
MetricsData metricsData = (MetricsData) item.getData().getBaseData();
return metricsData.getMetrics().stream().allMatch(metricDataPoint -> metricDataPoint.getName().equals("Feature"));
})
.findFirst()
.get();
validateFeatureStatsbeat(featureStatsbeat);
}

private static Map<String, String> getConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ HttpPipeline getHttpPipeline(@Nullable HttpPipelinePolicy policy, HttpClient htt
policies.add(interceptorManager.getRecordPolicy());
}

if (getTestMode() == TestMode.PLAYBACK) {
if (getTestMode() == TestMode.PLAYBACK && httpClient == null) {
httpClient = interceptorManager.getPlaybackClient();
}

Expand Down