Skip to content

Commit c6e9be2

Browse files
committed
refactor
1 parent 92f3a05 commit c6e9be2

File tree

6 files changed

+155
-146
lines changed

6 files changed

+155
-146
lines changed

smoke-tests/apps/LiveMetrics/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LiveMetricsTest.java

Lines changed: 12 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,9 @@
1818
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9;
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.awaitility.Awaitility.await;
21-
import static org.junit.jupiter.api.Assertions.assertTrue;
22-
23-
import com.azure.json.JsonProviders;
24-
import com.azure.json.JsonReader;
25-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentIngress;
26-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentType;
27-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Exception;
28-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MetricPoint;
29-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MonitoringDataPoint;
30-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Trace;
31-
import java.io.IOException;
21+
22+
import com.microsoft.applicationinsights.smoketest.fakeingestion.LiveMetricsVerifier;
3223
import java.time.Duration;
33-
import java.util.List;
3424
import org.junit.jupiter.api.Test;
3525
import org.junit.jupiter.api.extension.RegisterExtension;
3626

@@ -46,127 +36,20 @@ void testTelemetryDataFlow() {
4636
.atMost(Duration.ofSeconds(60))
4737
.until(() -> testing.mockedIngestion.getCountForType("RequestData") == 1);
4838

49-
assertThat(testing.mockedIngestion.isPingReceived()).isTrue();
50-
5139
await()
5240
.untilAsserted(
5341
() -> {
54-
PostBodyVerifier verifier = new PostBodyVerifier();
55-
for (String postBody : testing.mockedIngestion.getPostBodies()) {
56-
verifier.searchPostBody(postBody);
57-
}
58-
59-
assertTrue(verifier.hasExceptionDoc());
60-
assertTrue(verifier.hasTraceDoc());
61-
assertTrue(verifier.hasDependency());
62-
assertTrue(verifier.hasRequest());
63-
});
64-
}
42+
LiveMetricsVerifier verifier = testing.mockedIngestion.getLiveMetrics();
43+
44+
verifier.confirmDocsAreFiltered();
45+
verifier.confirmPerfCountersNonZero();
6546

66-
static class PostBodyVerifier {
67-
boolean foundExceptionDoc;
68-
boolean foundTraceDoc;
69-
boolean foundDependency;
70-
boolean foundRequest;
71-
72-
public void searchPostBody(String postBody) throws IOException {
73-
// Each post body is a list with a singular MonitoringDataPoint
74-
List<MonitoringDataPoint> dataPoints;
75-
try (JsonReader reader = JsonProviders.createReader(postBody)) {
76-
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
77-
}
78-
79-
// Because the mock ping/posts should succeed, we should only have one MonitoringDataPoint per
80-
// post
81-
assertThat(dataPoints).hasSize(1);
82-
MonitoringDataPoint dataPoint = dataPoints.get(0);
83-
84-
List<DocumentIngress> docs = dataPoint.getDocuments();
85-
List<MetricPoint> metrics = dataPoint.getMetrics();
86-
87-
confirmDocsAreFiltered(docs);
88-
confirmPerfCountersNonZero(metrics);
89-
foundExceptionDoc = foundExceptionDoc || hasException(docs);
90-
foundTraceDoc = foundTraceDoc || hasTrace(docs);
91-
foundDependency = foundDependency || hasDependency(metrics);
92-
foundRequest = foundRequest || hasRequest(metrics);
93-
}
94-
95-
public boolean hasExceptionDoc() {
96-
return foundExceptionDoc;
97-
}
98-
99-
public boolean hasTraceDoc() {
100-
return foundTraceDoc;
101-
}
102-
103-
public boolean hasDependency() {
104-
return foundDependency;
105-
}
106-
107-
public boolean hasRequest() {
108-
return foundRequest;
109-
}
110-
111-
private void confirmDocsAreFiltered(List<DocumentIngress> docs) {
112-
for (DocumentIngress doc : docs) {
113-
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REMOTE_DEPENDENCY);
114-
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REQUEST);
115-
}
116-
}
117-
118-
private boolean hasException(List<DocumentIngress> docs) {
119-
for (DocumentIngress doc : docs) {
120-
if (doc.getDocumentType().equals(DocumentType.EXCEPTION)
121-
&& ((Exception) doc).getExceptionMessage().equals("Fake Exception")) {
122-
return true;
123-
}
124-
}
125-
return false;
126-
}
127-
128-
private boolean hasTrace(List<DocumentIngress> docs) {
129-
for (DocumentIngress doc : docs) {
130-
if (doc.getDocumentType().equals(DocumentType.TRACE)
131-
&& ((Trace) doc).getMessage().equals("This message should generate a trace")) {
132-
return true;
133-
}
134-
}
135-
return false;
136-
}
137-
138-
private boolean hasDependency(List<MetricPoint> metrics) {
139-
for (MetricPoint metric : metrics) {
140-
String name = metric.getName();
141-
double value = metric.getValue();
142-
if (name.equals("\\ApplicationInsights\\Dependency Calls/Sec")) {
143-
return value == 1;
144-
}
145-
}
146-
return false;
147-
}
148-
149-
private boolean hasRequest(List<MetricPoint> metrics) {
150-
for (MetricPoint metric : metrics) {
151-
String name = metric.getName();
152-
double value = metric.getValue();
153-
if (name.equals("\\ApplicationInsights\\Requests/Sec")) {
154-
return value == 1;
155-
}
156-
}
157-
return false;
158-
}
159-
160-
private void confirmPerfCountersNonZero(List<MetricPoint> metrics) {
161-
for (MetricPoint metric : metrics) {
162-
String name = metric.getName();
163-
double value = metric.getValue();
164-
if (name.equals("\\Process\\Physical Bytes")
165-
|| name.equals("\\% Process\\Processor Time Normalized")) {
166-
assertThat(value).isNotEqualTo(0);
167-
}
168-
}
169-
}
47+
assertThat(verifier.getExceptionCount("Fake Exception")).isEqualTo(1);
48+
assertThat(verifier.getTraceCount("This message should generate a trace"))
49+
.isEqualTo(1);
50+
assertThat(verifier.getDependencyCount()).isEqualTo(1);
51+
assertThat(verifier.getRequestCount()).isEqualTo(1);
52+
});
17053
}
17154

17255
@Environment(TOMCAT_8_JAVA_8)

smoke-tests/framework/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ dependencies {
2929
implementation("ch.qos.logback:logback-classic")
3030

3131
implementation("org.assertj:assertj-core")
32+
33+
// Azure dependencies for PostBodyVerifier
34+
implementation("com.azure:azure-json:1.0.0")
35+
implementation("com.azure:azure-monitor-opentelemetry-autoconfigure:1.1.0")
3236
}

smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtension.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,15 @@ protected String getAppContext() {
281281

282282
private void clearOutAnyInitLogs() throws Exception {
283283
if (!skipHealthCheck) {
284+
await().until(mockedIngestion::isReceivingLiveMetrics);
284285
String contextRootUrl = getBaseUrl() + "/";
285286
HttpHelper.getResponseCodeEnsuringSampled(contextRootUrl);
286287
waitForHealthCheckTelemetry(contextRootUrl);
287-
// wait for live metrics to start sending data (needed for LiveMetricsTest)
288-
await().untilAsserted(() -> assertThat(mockedIngestion.getPostBodies()).isNotEmpty());
288+
await()
289+
.untilAsserted(
290+
() ->
291+
assertThat(mockedIngestion.getLiveMetrics().getSuccessfulRequestCount())
292+
.isEqualTo(1));
289293

290294
System.out.println("Clearing any RequestData from health check.");
291295
mockedIngestion.resetData();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketest.fakeingestion;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
import com.azure.json.JsonProviders;
9+
import com.azure.json.JsonReader;
10+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentIngress;
11+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentType;
12+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Exception;
13+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MetricPoint;
14+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MonitoringDataPoint;
15+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Trace;
16+
import java.io.IOException;
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
public class LiveMetricsVerifier {
21+
22+
private final List<MonitoringDataPoint> points = new ArrayList<>();
23+
24+
public void apply(String postBody) throws IOException {
25+
List<MonitoringDataPoint> dataPoints;
26+
try (JsonReader reader = JsonProviders.createReader(postBody)) {
27+
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
28+
}
29+
30+
// Because the mock ping/posts should succeed, there should be one MonitoringDataPoint per post
31+
assertThat(dataPoints).hasSize(1);
32+
points.add(dataPoints.get(0));
33+
}
34+
35+
public int getRequestCount() {
36+
return getMetricCount("\\ApplicationInsights\\Requests/Sec");
37+
}
38+
39+
public int getSuccessfulRequestCount() {
40+
return getMetricCount("\\ApplicationInsights\\Requests Succeeded/Sec");
41+
}
42+
43+
public int getDependencyCount() {
44+
return getMetricCount("\\ApplicationInsights\\Dependency Calls/Sec");
45+
}
46+
47+
public int getExceptionCount(String exceptionMessage) {
48+
int count = 0;
49+
for (MonitoringDataPoint point : points) {
50+
List<DocumentIngress> docs = point.getDocuments();
51+
for (DocumentIngress doc : docs) {
52+
if (doc.getDocumentType().equals(DocumentType.EXCEPTION)) {
53+
Exception ex = (Exception) doc;
54+
if (ex.getExceptionMessage().equals(exceptionMessage)) {
55+
count++;
56+
}
57+
}
58+
}
59+
}
60+
return count;
61+
}
62+
63+
public int getTraceCount(String traceMessage) {
64+
int count = 0;
65+
for (MonitoringDataPoint point : points) {
66+
List<DocumentIngress> docs = point.getDocuments();
67+
for (DocumentIngress doc : docs) {
68+
if (doc.getDocumentType().equals(DocumentType.TRACE)) {
69+
Trace trace = (Trace) doc;
70+
if (trace.getMessage().equals(traceMessage)) {
71+
count++;
72+
}
73+
}
74+
}
75+
}
76+
return count;
77+
}
78+
79+
public void confirmDocsAreFiltered() {
80+
for (MonitoringDataPoint point : points) {
81+
List<DocumentIngress> docs = point.getDocuments();
82+
for (DocumentIngress doc : docs) {
83+
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REMOTE_DEPENDENCY);
84+
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REQUEST);
85+
}
86+
}
87+
}
88+
89+
public void confirmPerfCountersNonZero() {
90+
for (MonitoringDataPoint point : points) {
91+
List<MetricPoint> metrics = point.getMetrics();
92+
for (MetricPoint metric : metrics) {
93+
String name = metric.getName();
94+
double value = metric.getValue();
95+
if (name.equals("\\Process\\Physical Bytes")
96+
|| name.equals("\\% Process\\Processor Time Normalized")) {
97+
assertThat(value).isNotEqualTo(0);
98+
}
99+
}
100+
}
101+
}
102+
103+
private int getMetricCount(String metricName) {
104+
int count = 0;
105+
for (MonitoringDataPoint point : points) {
106+
List<MetricPoint> metrics = point.getMetrics();
107+
for (MetricPoint metric : metrics) {
108+
String name = metric.getName();
109+
double value = metric.getValue();
110+
if (name.equals(metricName)) {
111+
if (Math.floor(value) != value) {
112+
throw new IllegalStateException("Not an integer: " + value);
113+
}
114+
count += (int) value;
115+
}
116+
}
117+
}
118+
return count;
119+
}
120+
}

smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/fakeingestion/MockedAppInsightsIngestionServer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,12 @@ public boolean test(Envelope input) {
284284
return items;
285285
}
286286

287-
public boolean isPingReceived() {
288-
return quickPulseServlet.isPingReceived();
287+
public boolean isReceivingLiveMetrics() {
288+
return quickPulseServlet.isReceivingLiveMetrics();
289289
}
290290

291-
public List<String> getPostBodies() {
292-
return quickPulseServlet.getPostBodies();
291+
public LiveMetricsVerifier getLiveMetrics() {
292+
return quickPulseServlet.getVerifier();
293293
}
294294

295295
@SuppressWarnings("SystemOut")

smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/fakeingestion/MockedQuickPulseServlet.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import com.google.common.io.CharStreams;
77
import java.io.IOException;
88
import java.io.StringWriter;
9-
import java.util.ArrayList;
10-
import java.util.List;
119
import java.util.concurrent.atomic.AtomicBoolean;
1210
import javax.servlet.http.HttpServlet;
1311
import javax.servlet.http.HttpServletRequest;
@@ -16,7 +14,8 @@
1614
public class MockedQuickPulseServlet extends HttpServlet {
1715

1816
private final AtomicBoolean pingReceived = new AtomicBoolean(false);
19-
private final List<String> postBodies = new ArrayList<>();
17+
private final AtomicBoolean postReceived = new AtomicBoolean(false);
18+
private volatile LiveMetricsVerifier verifier = new LiveMetricsVerifier();
2019
private final Object lock = new Object();
2120

2221
private static final String BODY =
@@ -52,8 +51,9 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
5251
resp.getWriter().write(BODY);
5352

5453
} else if (path.equals("/post")) {
54+
postReceived.set(true);
5555
synchronized (lock) {
56-
postBodies.add(body);
56+
verifier.apply(body);
5757
}
5858
logit("post body: " + body);
5959
// continue to post
@@ -65,14 +65,12 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
6565
}
6666
}
6767

68-
public boolean isPingReceived() {
69-
return pingReceived.get();
68+
public boolean isReceivingLiveMetrics() {
69+
return pingReceived.get() && postReceived.get();
7070
}
7171

72-
public List<String> getPostBodies() {
73-
synchronized (lock) {
74-
return new ArrayList<>(postBodies);
75-
}
72+
public LiveMetricsVerifier getVerifier() {
73+
return verifier;
7674
}
7775

7876
public void setRequestLoggingEnabled(boolean enabled) {
@@ -81,7 +79,7 @@ public void setRequestLoggingEnabled(boolean enabled) {
8179

8280
public void resetData() {
8381
synchronized (lock) {
84-
postBodies.clear();
82+
verifier = new LiveMetricsVerifier();
8583
}
8684
}
8785
}

0 commit comments

Comments
 (0)