1717import static com .microsoft .applicationinsights .smoketest .EnvironmentValue .WILDFLY_13_JAVA_8 ;
1818import static com .microsoft .applicationinsights .smoketest .EnvironmentValue .WILDFLY_13_JAVA_8_OPENJ9 ;
1919import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .awaitility .Awaitility .await ;
2021
21- import com .azure .json .JsonProviders ;
22- import com .azure .json .JsonReader ;
23- import com .azure .monitor .opentelemetry .autoconfigure .implementation .quickpulse .swagger .models .DocumentIngress ;
24- import com .azure .monitor .opentelemetry .autoconfigure .implementation .quickpulse .swagger .models .DocumentType ;
25- import com .azure .monitor .opentelemetry .autoconfigure .implementation .quickpulse .swagger .models .Exception ;
26- import com .azure .monitor .opentelemetry .autoconfigure .implementation .quickpulse .swagger .models .MetricPoint ;
27- import com .azure .monitor .opentelemetry .autoconfigure .implementation .quickpulse .swagger .models .MonitoringDataPoint ;
28- import com .azure .monitor .opentelemetry .autoconfigure .implementation .quickpulse .swagger .models .Trace ;
29- import java .io .IOException ;
22+ import com .microsoft .applicationinsights .smoketest .fakeingestion .LiveMetricsVerifier ;
3023import java .time .Duration ;
31- import java .util .ArrayList ;
32- import java .util .List ;
33- import org .awaitility .Awaitility ;
3424import org .junit .jupiter .api .Test ;
3525import org .junit .jupiter .api .extension .RegisterExtension ;
3626
@@ -41,135 +31,25 @@ abstract class LiveMetricsTest {
4131
4232 @ Test
4333 @ TargetUri ("/test" )
44- void testTelemetryDataFlow () throws java . lang . Exception {
45- Awaitility . await ()
34+ void testTelemetryDataFlow () {
35+ await ()
4636 .atMost (Duration .ofSeconds (60 ))
4737 .until (() -> testing .mockedIngestion .getCountForType ("RequestData" ) == 1 );
4838
49- PostBodyVerifier postBodyVerifier = new PostBodyVerifier ();
39+ await ()
40+ .untilAsserted (
41+ () -> {
42+ LiveMetricsVerifier verifier = testing .mockedIngestion .getLiveMetrics ();
5043
51- assertThat (testing .mockedIngestion .isPingReceived ()).isTrue ();
44+ verifier .confirmDocsAreFiltered ();
45+ verifier .confirmPerfCountersNonZero ();
5246
53- List <String > postBodies = testing .mockedIngestion .getPostBodies ();
54- assertThat (postBodies ).hasSizeGreaterThan (0 ); // should post at least once
55-
56- for (String postBody : postBodies ) {
57- postBodyVerifier .searchPostBody (postBody );
58- }
59-
60- assertThat (postBodyVerifier .hasExceptionDoc ()).isTrue ();
61- assertThat (postBodyVerifier .hasTraceDoc ()).isTrue ();
62- assertThat (postBodyVerifier .hasDependency ()).isTrue ();
63- assertThat (postBodyVerifier .hasRequest ()).isTrue ();
64- }
65-
66- class PostBodyVerifier {
67- boolean foundExceptionDoc = false ;
68- boolean foundTraceDoc = false ;
69- boolean foundDependency = false ;
70- boolean foundRequest = false ;
71-
72- public void searchPostBody (String postBody ) {
73- // Each post body is a list with a singular MonitoringDataPoint
74- List <MonitoringDataPoint > dataPoints = new ArrayList <>();
75- try {
76- JsonReader reader = JsonProviders .createReader (postBody );
77- dataPoints = reader .readArray (MonitoringDataPoint ::fromJson );
78- } catch (IOException e ) {
79- throw new IllegalStateException ("Failed to parse post request body" , e );
80- }
81-
82- // Because the mock ping/posts should succeed, we should only have one MonitoringDataPoint per
83- // post
84- assertThat (dataPoints ).hasSize (1 );
85- MonitoringDataPoint dataPoint = dataPoints .get (0 );
86-
87- List <DocumentIngress > docs = dataPoint .getDocuments ();
88- List <MetricPoint > metrics = dataPoint .getMetrics ();
89-
90- confirmDocsAreFiltered (docs );
91- confirmPerfCountersNonZero (metrics );
92- foundExceptionDoc = foundExceptionDoc || hasException (docs );
93- foundTraceDoc = foundTraceDoc || hasTrace (docs );
94- foundDependency = foundDependency || hasDependency (metrics );
95- foundRequest = foundRequest || hasRequest (metrics );
96- }
97-
98- public boolean hasExceptionDoc () {
99- return foundExceptionDoc ;
100- }
101-
102- public boolean hasTraceDoc () {
103- return foundTraceDoc ;
104- }
105-
106- public boolean hasDependency () {
107- return foundDependency ;
108- }
109-
110- public boolean hasRequest () {
111- return foundRequest ;
112- }
113-
114- private void confirmDocsAreFiltered (List <DocumentIngress > docs ) {
115- for (DocumentIngress doc : docs ) {
116- assertThat (doc .getDocumentType ()).isNotEqualTo (DocumentType .REMOTE_DEPENDENCY );
117- assertThat (doc .getDocumentType ()).isNotEqualTo (DocumentType .REQUEST );
118- }
119- }
120-
121- private boolean hasException (List <DocumentIngress > docs ) {
122- for (DocumentIngress doc : docs ) {
123- if (doc .getDocumentType ().equals (DocumentType .EXCEPTION )
124- && ((Exception ) doc ).getExceptionMessage ().equals ("Fake Exception" )) {
125- return true ;
126- }
127- }
128- return false ;
129- }
130-
131- private boolean hasTrace (List <DocumentIngress > docs ) {
132- for (DocumentIngress doc : docs ) {
133- if (doc .getDocumentType ().equals (DocumentType .TRACE )
134- && ((Trace ) doc ).getMessage ().equals ("This message should generate a trace" )) {
135- return true ;
136- }
137- }
138- return false ;
139- }
140-
141- private boolean hasDependency (List <MetricPoint > metrics ) {
142- for (MetricPoint metric : metrics ) {
143- String name = metric .getName ();
144- double value = metric .getValue ();
145- if (name .equals ("\\ ApplicationInsights\\ Dependency Calls/Sec" )) {
146- return value == 1 ;
147- }
148- }
149- return false ;
150- }
151-
152- private boolean hasRequest (List <MetricPoint > metrics ) {
153- for (MetricPoint metric : metrics ) {
154- String name = metric .getName ();
155- double value = metric .getValue ();
156- if (name .equals ("\\ ApplicationInsights\\ Requests/Sec" )) {
157- return value == 1 ;
158- }
159- }
160- return false ;
161- }
162-
163- private void confirmPerfCountersNonZero (List <MetricPoint > metrics ) {
164- for (MetricPoint metric : metrics ) {
165- String name = metric .getName ();
166- double value = metric .getValue ();
167- if (name .equals ("\\ Process\\ Physical Bytes" )
168- || name .equals ("\\ % Process\\ Processor Time Normalized" )) {
169- assertThat (value ).isNotEqualTo (0 );
170- }
171- }
172- }
47+ assertThat (verifier .getExceptionCount ("Fake Exception" )).isEqualTo (1 );
48+ assertThat (verifier .getTraceCount ("This message should generate a trace" ))
49+ .isEqualTo (1 );
50+ assertThat (verifier .getDependencyCountFromMetric ()).isEqualTo (1 );
51+ assertThat (verifier .getRequestCountFromMetric ()).isEqualTo (1 );
52+ });
17353 }
17454
17555 @ Environment (TOMCAT_8_JAVA_8 )
0 commit comments