Skip to content

Commit 714623f

Browse files
committed
pr comments round 1
1 parent 30619bf commit 714623f

File tree

3 files changed

+65
-49
lines changed

3 files changed

+65
-49
lines changed

smoke-tests/apps/LiveMetrics/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestServlet.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
2727

2828
@WithSpan
2929
private void doWork() {
30-
System.out.println("Doing work to generate a dependency call, exception, and trace.");
3130
logger.error("This message should generate an exception!", new Exception("Fake Exception"));
3231
logger.info("This message should generate a trace");
3332
}

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

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ abstract class LiveMetricsTest {
3939

4040
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();
4141

42+
private boolean foundExceptionDoc = false;
43+
private boolean foundTraceDoc = false;
44+
private boolean foundDependency = false;
45+
private boolean foundRequest = false;
46+
4247
@Test
4348
@TargetUri("/test")
4449
void testTelemetryDataFlow() throws java.lang.Exception {
@@ -51,52 +56,8 @@ void testTelemetryDataFlow() throws java.lang.Exception {
5156
List<String> postBodies = testing.mockedIngestion.getPostBodies();
5257
assertThat(postBodies).hasSizeGreaterThan(0); // should post at least once
5358

54-
boolean foundExceptionDoc = false;
55-
boolean foundTraceDoc = false;
56-
boolean foundDependency = false;
57-
boolean foundRequest = false;
58-
5959
for (String postBody : postBodies) {
60-
// Each post body is a list with a singular MonitoringDataPoint
61-
List<MonitoringDataPoint> dataPoints = new ArrayList<>();
62-
try {
63-
JsonReader reader = JsonProviders.createReader(postBody);
64-
// See live metrics swagger to understand MonitoringDataPoint structure
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-
// Because the mock ping/posts should succeed, we should only have one MonitoringDataPoint per
71-
// post
72-
assertThat(dataPoints).hasSize(1);
73-
MonitoringDataPoint dataPoint = dataPoints.get(0);
74-
List<DocumentIngress> docs = dataPoint.getDocuments();
75-
List<MetricPoint> metrics = dataPoint.getMetrics();
76-
77-
// check that the expected documents are present
78-
// With the default filtering configuration, we should only see the exception and trace
79-
// documents.
80-
for (DocumentIngress doc : docs) {
81-
if (doc.getDocumentType().equals(DocumentType.EXCEPTION)
82-
&& ((Exception) doc).getExceptionMessage().equals("Fake Exception")) {
83-
foundExceptionDoc = true;
84-
} else if (doc.getDocumentType().equals(DocumentType.TRACE)
85-
&& ((Trace) doc).getMessage().equals("This message should generate a trace")) {
86-
foundTraceDoc = true;
87-
}
88-
}
89-
90-
// See if dependency and request are counted
91-
for (MetricPoint metric : metrics) {
92-
String name = metric.getName();
93-
double value = metric.getValue();
94-
if (name.equals("\\ApplicationInsights\\Dependency Calls/Sec") && value == 1) {
95-
foundDependency = true;
96-
} else if (name.equals("\\ApplicationInsights\\Requests/Sec") && value == 1) {
97-
foundRequest = true;
98-
}
99-
}
60+
searchPostBody(postBody);
10061
}
10162

10263
assertThat(foundExceptionDoc).isTrue();
@@ -105,6 +66,61 @@ void testTelemetryDataFlow() throws java.lang.Exception {
10566
assertThat(foundRequest).isTrue();
10667
}
10768

69+
// check if the expected trace/exception documents are present.
70+
// With the default filtering configuration, we should not see successful requests/dependencies,
71+
// only trace and exception.
72+
private void searchDocs(List<DocumentIngress> docs) {
73+
for (DocumentIngress doc : docs) {
74+
if (doc.getDocumentType().equals(DocumentType.EXCEPTION)
75+
&& ((Exception) doc).getExceptionMessage().equals("Fake Exception")) {
76+
foundExceptionDoc = true;
77+
} else if (doc.getDocumentType().equals(DocumentType.TRACE)
78+
&& ((Trace) doc).getMessage().equals("This message should generate a trace")) {
79+
foundTraceDoc = true;
80+
} else {
81+
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REMOTE_DEPENDENCY);
82+
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REQUEST);
83+
}
84+
}
85+
}
86+
87+
private void searchMetrics(List<MetricPoint> metrics) {
88+
for (MetricPoint metric : metrics) {
89+
String name = metric.getName();
90+
double value = metric.getValue();
91+
if (name.equals("\\ApplicationInsights\\Dependency Calls/Sec") && value == 1) {
92+
foundDependency = true;
93+
} else if (name.equals("\\ApplicationInsights\\Requests/Sec") && value == 1) {
94+
foundRequest = true;
95+
} else if (name.equals("\\Process\\Physical Bytes") || name.equals("\\% Process\\Processor Time Normalized")) {
96+
assertThat(value).isNotEqualTo(0);
97+
}
98+
}
99+
}
100+
101+
102+
private void searchPostBody(String postBody) {
103+
// Each post body is a list with a singular MonitoringDataPoint
104+
List<MonitoringDataPoint> dataPoints = new ArrayList<>();
105+
try {
106+
JsonReader reader = JsonProviders.createReader(postBody);
107+
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
108+
} catch (IOException e) {
109+
throw new IllegalStateException("Failed to parse post request body", e);
110+
}
111+
112+
// Because the mock ping/posts should succeed, we should only have one MonitoringDataPoint per
113+
// post
114+
assertThat(dataPoints).hasSize(1);
115+
MonitoringDataPoint dataPoint = dataPoints.get(0);
116+
117+
List<DocumentIngress> docs = dataPoint.getDocuments();
118+
List<MetricPoint> metrics = dataPoint.getMetrics();
119+
120+
searchDocs(docs);
121+
searchMetrics(metrics);
122+
}
123+
108124
@Environment(TOMCAT_8_JAVA_8)
109125
static class Tomcat8Java8Test extends LiveMetricsTest {}
110126

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.google.common.io.CharStreams;
77
import java.io.IOException;
88
import java.io.StringWriter;
9+
import java.rmi.ServerError;
910
import java.util.ArrayList;
1011
import java.util.List;
1112
import java.util.concurrent.atomic.AtomicBoolean;
@@ -19,7 +20,7 @@ public class MockedQuickPulseServlet extends HttpServlet {
1920
private final List<String> postBodies = new ArrayList<>();
2021
private final Object lock = new Object();
2122

22-
private static final String MOCK_RESPONSE_JSON_DEFAULT_CONFIG =
23+
private static final String BODY =
2324
"{\"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\":[]}}]}]}";
2425

2526
private volatile boolean loggingEnabled;
@@ -49,7 +50,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
4950
resp.setHeader("x-ms-qps-configuration-etag", "fake::etag");
5051
resp.setHeader("x-ms-qps-subscribed", "true");
5152
resp.setContentType("application/json");
52-
resp.getWriter().write(MOCK_RESPONSE_JSON_DEFAULT_CONFIG);
53+
resp.getWriter().write(BODY);
5354

5455
} else if (path.equals("/post")) {
5556
synchronized (lock) {
@@ -60,7 +61,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
6061
resp.setHeader("x-ms-qps-subscribed", "true");
6162
resp.setHeader("x-ms-qps-configuration-etag", "fake::etag");
6263
} else {
63-
resp.setStatus(404);
64+
throw new ServerError("Unexpected path: " + path + " please fix the test/mock server setup", new Error());
6465
}
6566
}
6667

0 commit comments

Comments
 (0)