Skip to content

Commit a78c06a

Browse files
committed
initial test changes
1 parent 4c24f73 commit a78c06a

File tree

3 files changed

+87
-59
lines changed

3 files changed

+87
-59
lines changed

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

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.time.Duration;
2323
import java.util.ArrayList;
2424
import java.util.List;
25-
import com.azure.json.JsonToken;
2625
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentIngress;
27-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentStreamInfo;
26+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Exception;
27+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Trace;
28+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.DocumentType;
29+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.MetricPoint;
2830
import org.awaitility.Awaitility;
2931
import org.junit.jupiter.api.Test;
3032
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -39,28 +41,68 @@ abstract class LiveMetricsTest {
3941

4042
@Test
4143
@TargetUri("/test")
42-
void testPingPostAndTelemetryDataFlow() throws Exception {
43-
44+
void testTelemetryDataFlow() throws java.lang.Exception {
4445
Awaitility.await()
45-
.atMost(Duration.ofSeconds(15));
46-
47-
assertThat(testing.mockedIngestion.getNumPingsReceived()).isEqualTo(1);
48-
//ping body
49-
String pingBody = testing.mockedIngestion.getLastPingBody();
50-
51-
// After the ping, we expect a post to happen roughly every second.
52-
assertThat(testing.mockedIngestion.getNumPostsReceived()).isGreaterThanOrEqualTo(10);
53-
String postBody = testing.mockedIngestion.getLastPostBody();
54-
55-
// Verify that the telemetry data is in the last post body
56-
JsonReader reader = JsonProviders.createReader(postBody);
57-
List<MonitoringDataPoint> dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
58-
assertThat(dataPoints).hasSize(1);
59-
60-
MonitoringDataPoint dataPoint = dataPoints.get(0);
61-
List<DocumentIngress> docs = dataPoint.getDocuments();
62-
63-
46+
.atMost(Duration.ofSeconds(20));
47+
48+
49+
assertThat(testing.mockedIngestion.isPingReceived()).isTrue();
50+
51+
52+
List<String> postBodies = testing.mockedIngestion.getPostBodies();
53+
assertThat(postBodies).hasSizeGreaterThan(0); // should post at least once
54+
55+
boolean foundExceptionDoc = false;
56+
boolean foundTraceDoc = false;
57+
boolean foundDependency = false;
58+
boolean foundRequest = false;
59+
60+
for (String postBody : postBodies) {
61+
// Verify that the telemetry data is in the last post body
62+
List<MonitoringDataPoint> dataPoints = new ArrayList<>();
63+
try {
64+
JsonReader reader = JsonProviders.createReader(postBody);
65+
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
66+
} catch (IOException e) {
67+
throw new java.lang.Exception("Failed to parse post request body", e);
68+
}
69+
70+
assertThat(dataPoints).hasSize(1);
71+
MonitoringDataPoint dataPoint = dataPoints.get(0);
72+
List<DocumentIngress> docs = dataPoint.getDocuments();
73+
List<MetricPoint> metrics = dataPoint.getMetrics();
74+
// check that the expected documents are present
75+
// With the default filtering configuration, we should only see the exception and trace documents.
76+
// Request/Dep did not fail, so there should not be documents for those.
77+
78+
for (DocumentIngress doc : docs) {
79+
if (doc.getDocumentType().equals(DocumentType.EXCEPTION) &&
80+
((Exception) doc).getExceptionMessage().equals("Fake Exception")) {
81+
foundExceptionDoc = true;
82+
} else if (doc.getDocumentType().equals(DocumentType.TRACE) &&
83+
((Trace) doc).getMessage().equals("This message should generate a trace")) {
84+
foundTraceDoc = true;
85+
}
86+
}
87+
88+
// check that the expected metrics have the correct values
89+
90+
for (MetricPoint metric : metrics) {
91+
String name = metric.getName();
92+
double value = metric.getValue();
93+
if (name.equals("\\ApplicationInsights\\Dependency Calls/Sec") && value == 1) {
94+
foundDependency = true;
95+
} else if (name.equals("\\ApplicationInsights\\Requests/Sec") && value == 1) {
96+
foundRequest = true;
97+
}
98+
}
99+
100+
}
101+
102+
assertThat(foundExceptionDoc).isTrue();
103+
assertThat(foundTraceDoc).isTrue();
104+
assertThat(foundDependency).isTrue();
105+
assertThat(foundRequest).isTrue();
64106

65107
}
66108

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,12 @@ public boolean test(Envelope input) {
279279
return items;
280280
}
281281

282-
public int getNumPingsReceived() {
283-
return quickPulseServlet.getNumPingsReceived();
282+
public boolean isPingReceived() {
283+
return quickPulseServlet.isPingReceived();
284284
}
285285

286-
public int getNumPostsReceived() {
287-
return quickPulseServlet.getNumPostsReceived();
288-
}
289-
290-
public String getLastPingBody() {
291-
return quickPulseServlet.getPingBody();
292-
}
293-
294-
public String getLastPostBody() {
295-
return quickPulseServlet.getLastPostBody();
286+
public List<String> getPostBodies() {
287+
return quickPulseServlet.getPostBodies();
296288
}
297289

298290
@SuppressWarnings("SystemOut")

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

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
import javax.servlet.http.HttpServletResponse;
99
import java.io.IOException;
1010
import java.io.StringWriter;
11-
import java.util.concurrent.atomic.AtomicInteger;
12-
import java.util.concurrent.atomic.AtomicReference;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.concurrent.atomic.AtomicBoolean;
1314

1415
public class MockedQuickPulseServlet extends HttpServlet {
1516

16-
private final AtomicInteger liveMetricsPingReceived = new AtomicInteger();
17-
18-
private final AtomicInteger liveMetricsPostReceived = new AtomicInteger();
19-
20-
private final AtomicReference<String> pingBody = new AtomicReference<>();
21-
private final AtomicReference<String> lastPostBody = new AtomicReference<>();
17+
private final AtomicBoolean pingReceived = new AtomicBoolean(false);
18+
private final List<String> postBodies = new ArrayList<>();
19+
private final Object lock = new Object();
2220

2321
private static final String MOCK_RESPONSE_JSON_DEFAULT_CONFIG =
2422
"{\"ETag\":\"fake::etag\",\"Metrics\":[],\"QuotaInfo\":null,\"DocumentStreams\":[{\"Id\":\"all-types-default\",\"DocumentFilterGroups\":[{\"TelemetryType\":\"Request\",\"Filters\":{\"Filters\":[{\"FieldName\":\"Success\",\"Predicate\":\"Equal\",\"Comparand\":\"false\"}]}},{\"TelemetryType\":\"Dependency\",\"Filters\":{\"Filters\":[{\"FieldName\":\"Success\",\"Predicate\":\"Equal\",\"Comparand\":\"false\"}]}},{\"TelemetryType\":\"Exception\",\"Filters\":{\"Filters\":[]}},{\"TelemetryType\":\"Event\",\"Filters\":{\"Filters\":[]}},{\"TelemetryType\":\"Trace\",\"Filters\":{\"Filters\":[]}}]}]}";
@@ -46,39 +44,35 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
4644
String path = req.getPathInfo();
4745
logit("QuickPulse received: " + path);
4846
if (path.equals("/ping")) {
49-
liveMetricsPingReceived.incrementAndGet();
50-
pingBody.set(body);
47+
pingReceived.set(true);
5148
logit("ping body: " + body);
5249
// want the following request to be a post
50+
resp.setHeader("x-ms-qps-configuration-etag", "fake::etag");
5351
resp.setHeader("x-ms-qps-subscribed", "true");
5452
resp.setContentType("application/json");
5553
resp.getWriter().write(MOCK_RESPONSE_JSON_DEFAULT_CONFIG);
5654

5755
} else if (path.equals("/post")) {
58-
liveMetricsPostReceived.incrementAndGet();
59-
lastPostBody.set(body);
56+
synchronized (lock) {
57+
postBodies.add(body);
58+
}
6059
logit("post body: " + body);
6160
// continue to post
6261
resp.setHeader("x-ms-qps-subscribed", "true");
62+
resp.setHeader("x-ms-qps-configuration-etag", "fake::etag");
6363
} else {
6464
resp.setStatus(404);
6565
}
6666
}
6767

68-
public int getNumPingsReceived() {
69-
return liveMetricsPingReceived.get();
70-
}
71-
72-
public int getNumPostsReceived() {
73-
return liveMetricsPostReceived.get();
68+
public boolean isPingReceived() {
69+
return pingReceived.get();
7470
}
7571

76-
public String getPingBody() {
77-
return pingBody.get();
78-
}
79-
80-
public String getLastPostBody() {
81-
return lastPostBody.get();
72+
public List<String> getPostBodies() {
73+
synchronized (lock) {
74+
return new ArrayList<>(postBodies);
75+
}
8276
}
8377

8478
public void setRequestLoggingEnabled(boolean enabled) {

0 commit comments

Comments
 (0)