2222import java .time .Duration ;
2323import java .util .ArrayList ;
2424import java .util .List ;
25- import com .azure .json .JsonToken ;
2625import 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 ;
2830import org .awaitility .Awaitility ;
2931import org .junit .jupiter .api .Test ;
3032import 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
0 commit comments