Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 1 addition & 2 deletions smoke-tests/apps/AzureSdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ dependencies {
exclude("org.springframework.boot", "spring-boot-starter-tomcat")
}
// want to test with one of the earliest version supported, and not managed version used in agent
implementation(enforcedPlatform("com.azure:azure-sdk-bom:1.2.13"))
implementation("com.azure:azure-core")
implementation("com.azure:azure-core:1.39.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,10 @@
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8;
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

import com.azure.json.JsonProviders;
import com.azure.json.JsonReader;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentIngress;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentType;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Exception;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MetricPoint;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MonitoringDataPoint;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Trace;
import java.io.IOException;
import com.microsoft.applicationinsights.smoketest.fakeingestion.LiveMetricsVerifier;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand All @@ -41,135 +31,25 @@ abstract class LiveMetricsTest {

@Test
@TargetUri("/test")
void testTelemetryDataFlow() throws java.lang.Exception {
Awaitility.await()
void testTelemetryDataFlow() {
await()
.atMost(Duration.ofSeconds(60))
.until(() -> testing.mockedIngestion.getCountForType("RequestData") == 1);

PostBodyVerifier postBodyVerifier = new PostBodyVerifier();
await()
.untilAsserted(
() -> {
LiveMetricsVerifier verifier = testing.mockedIngestion.getLiveMetrics();

assertThat(testing.mockedIngestion.isPingReceived()).isTrue();
verifier.confirmDocsAreFiltered();
verifier.confirmPerfCountersNonZero();

List<String> postBodies = testing.mockedIngestion.getPostBodies();
assertThat(postBodies).hasSizeGreaterThan(0); // should post at least once

for (String postBody : postBodies) {
postBodyVerifier.searchPostBody(postBody);
}

assertThat(postBodyVerifier.hasExceptionDoc()).isTrue();
assertThat(postBodyVerifier.hasTraceDoc()).isTrue();
assertThat(postBodyVerifier.hasDependency()).isTrue();
assertThat(postBodyVerifier.hasRequest()).isTrue();
}

class PostBodyVerifier {
boolean foundExceptionDoc = false;
boolean foundTraceDoc = false;
boolean foundDependency = false;
boolean foundRequest = false;

public void searchPostBody(String postBody) {
// Each post body is a list with a singular MonitoringDataPoint
List<MonitoringDataPoint> dataPoints = new ArrayList<>();
try {
JsonReader reader = JsonProviders.createReader(postBody);
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
} catch (IOException e) {
throw new IllegalStateException("Failed to parse post request body", e);
}

// Because the mock ping/posts should succeed, we should only have one MonitoringDataPoint per
// post
assertThat(dataPoints).hasSize(1);
MonitoringDataPoint dataPoint = dataPoints.get(0);

List<DocumentIngress> docs = dataPoint.getDocuments();
List<MetricPoint> metrics = dataPoint.getMetrics();

confirmDocsAreFiltered(docs);
confirmPerfCountersNonZero(metrics);
foundExceptionDoc = foundExceptionDoc || hasException(docs);
foundTraceDoc = foundTraceDoc || hasTrace(docs);
foundDependency = foundDependency || hasDependency(metrics);
foundRequest = foundRequest || hasRequest(metrics);
}

public boolean hasExceptionDoc() {
return foundExceptionDoc;
}

public boolean hasTraceDoc() {
return foundTraceDoc;
}

public boolean hasDependency() {
return foundDependency;
}

public boolean hasRequest() {
return foundRequest;
}

private void confirmDocsAreFiltered(List<DocumentIngress> docs) {
for (DocumentIngress doc : docs) {
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REMOTE_DEPENDENCY);
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REQUEST);
}
}

private boolean hasException(List<DocumentIngress> docs) {
for (DocumentIngress doc : docs) {
if (doc.getDocumentType().equals(DocumentType.EXCEPTION)
&& ((Exception) doc).getExceptionMessage().equals("Fake Exception")) {
return true;
}
}
return false;
}

private boolean hasTrace(List<DocumentIngress> docs) {
for (DocumentIngress doc : docs) {
if (doc.getDocumentType().equals(DocumentType.TRACE)
&& ((Trace) doc).getMessage().equals("This message should generate a trace")) {
return true;
}
}
return false;
}

private boolean hasDependency(List<MetricPoint> metrics) {
for (MetricPoint metric : metrics) {
String name = metric.getName();
double value = metric.getValue();
if (name.equals("\\ApplicationInsights\\Dependency Calls/Sec")) {
return value == 1;
}
}
return false;
}

private boolean hasRequest(List<MetricPoint> metrics) {
for (MetricPoint metric : metrics) {
String name = metric.getName();
double value = metric.getValue();
if (name.equals("\\ApplicationInsights\\Requests/Sec")) {
return value == 1;
}
}
return false;
}

private void confirmPerfCountersNonZero(List<MetricPoint> metrics) {
for (MetricPoint metric : metrics) {
String name = metric.getName();
double value = metric.getValue();
if (name.equals("\\Process\\Physical Bytes")
|| name.equals("\\% Process\\Processor Time Normalized")) {
assertThat(value).isNotEqualTo(0);
}
}
}
assertThat(verifier.getExceptionCount("Fake Exception")).isEqualTo(1);
assertThat(verifier.getTraceCount("This message should generate a trace"))
.isEqualTo(1);
assertThat(verifier.getDependencyCountFromMetric()).isEqualTo(1);
assertThat(verifier.getRequestCountFromMetric()).isEqualTo(1);
});
}

@Environment(TOMCAT_8_JAVA_8)
Expand Down
3 changes: 3 additions & 0 deletions smoke-tests/framework/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ dependencies {
implementation("ch.qos.logback:logback-classic")

implementation("org.assertj:assertj-core")

implementation("com.azure:azure-json:1.0.0")
implementation("com.azure:azure-monitor-opentelemetry-autoconfigure:1.1.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,16 @@ protected String getAppContext() {

private void clearOutAnyInitLogs() throws Exception {
if (!skipHealthCheck) {
await().until(mockedIngestion::isReceivingLiveMetrics);
String contextRootUrl = getBaseUrl() + "/";
HttpHelper.getResponseCodeEnsuringSampled(contextRootUrl);
waitForHealthCheckTelemetry(contextRootUrl);
await()
.untilAsserted(
() ->
assertThat(mockedIngestion.getLiveMetrics().getRequestCount(contextRootUrl))
.isEqualTo(1));
Copy link
Member Author

Choose a reason for hiding this comment

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

this ensures that live metrics have been sent before resetData is called below


System.out.println("Clearing any RequestData from health check.");
mockedIngestion.resetData();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.applicationinsights.smoketest.fakeingestion;

import static org.assertj.core.api.Assertions.assertThat;

import com.azure.json.JsonProviders;
import com.azure.json.JsonReader;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentIngress;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentType;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Exception;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MetricPoint;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MonitoringDataPoint;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Request;
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Trace;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LiveMetricsVerifier {

private final List<MonitoringDataPoint> points = new ArrayList<>();

public void apply(String postBody) throws IOException {
List<MonitoringDataPoint> dataPoints;
try (JsonReader reader = JsonProviders.createReader(postBody)) {
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
}

// Because the mock ping/posts should succeed, there should be one MonitoringDataPoint per post
assertThat(dataPoints).hasSize(1);
points.add(dataPoints.get(0));
}

public int getRequestCountFromMetric() {
return getMetricCount("\\ApplicationInsights\\Requests/Sec");
}

public int getDependencyCountFromMetric() {
return getMetricCount("\\ApplicationInsights\\Dependency Calls/Sec");
}

public int getRequestCount(String url) {
int count = 0;
for (MonitoringDataPoint point : points) {
List<DocumentIngress> docs = point.getDocuments();
for (DocumentIngress doc : docs) {
if (doc.getDocumentType().equals(DocumentType.REQUEST)) {
Request request = (Request) doc;
if (url.equals(request.getUrl())) {
count++;
}
}
}
}
return count;
}

public int getExceptionCount(String exceptionMessage) {
int count = 0;
for (MonitoringDataPoint point : points) {
List<DocumentIngress> docs = point.getDocuments();
for (DocumentIngress doc : docs) {
if (doc.getDocumentType().equals(DocumentType.EXCEPTION)) {
Exception ex = (Exception) doc;
if (ex.getExceptionMessage().equals(exceptionMessage)) {
count++;
}
}
}
}
return count;
}

public int getTraceCount(String traceMessage) {
int count = 0;
for (MonitoringDataPoint point : points) {
List<DocumentIngress> docs = point.getDocuments();
for (DocumentIngress doc : docs) {
if (doc.getDocumentType().equals(DocumentType.TRACE)) {
Trace trace = (Trace) doc;
if (trace.getMessage().equals(traceMessage)) {
count++;
}
}
}
}
return count;
}

public void confirmDocsAreFiltered() {
for (MonitoringDataPoint point : points) {
List<DocumentIngress> docs = point.getDocuments();
for (DocumentIngress doc : docs) {
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REMOTE_DEPENDENCY);
}
}
}

public void confirmPerfCountersNonZero() {
for (MonitoringDataPoint point : points) {
List<MetricPoint> metrics = point.getMetrics();
for (MetricPoint metric : metrics) {
String name = metric.getName();
double value = metric.getValue();
if (name.equals("\\Process\\Physical Bytes")
|| name.equals("\\% Process\\Processor Time Normalized")) {
assertThat(value).isNotEqualTo(0);
}
}
}
}

private int getMetricCount(String metricName) {
int count = 0;
for (MonitoringDataPoint point : points) {
List<MetricPoint> metrics = point.getMetrics();
for (MetricPoint metric : metrics) {
String name = metric.getName();
double value = metric.getValue();
if (name.equals(metricName)) {
if (Math.floor(value) != value) {
throw new IllegalStateException("Not an integer: " + value);
}
count += (int) value;
}
}
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ public void stopServer() throws Exception {
System.out.println("Stopping fake Breeze ingestion...");
server.stop();
server.join();
quickPulseServlet.resetData();
}

public void resetData() {
this.servlet.resetData();
servlet.resetData();
quickPulseServlet.resetData();
}

public boolean hasData() {
Expand Down Expand Up @@ -283,12 +285,12 @@ public boolean test(Envelope input) {
return items;
}

public boolean isPingReceived() {
return quickPulseServlet.isPingReceived();
public boolean isReceivingLiveMetrics() {
return quickPulseServlet.isReceivingLiveMetrics();
}

public List<String> getPostBodies() {
return quickPulseServlet.getPostBodies();
public LiveMetricsVerifier getLiveMetrics() {
return quickPulseServlet.getVerifier();
}

@SuppressWarnings("SystemOut")
Expand Down
Loading
Loading