@@ -64,6 +64,59 @@ public MetricsVerifier assertGauge(String metricName, String description, String
6464 return this ;
6565 }
6666
67+ @ SuppressWarnings ("CanIgnoreReturnValueSuggester" )
68+ public MetricsVerifier assertCounter (String metricName , String description , String unit ) {
69+ return assertSum (metricName , description , unit , /* isMonotonic= */ true );
70+ }
71+
72+ @ SuppressWarnings ("CanIgnoreReturnValueSuggester" )
73+ public MetricsVerifier assertUpDownCounter (String metricName , String description , String unit ) {
74+ return assertSum (metricName , description , unit , /* isMonotonic= */ false );
75+ }
76+
77+ @ SafeVarargs
78+ @ SuppressWarnings ("CanIgnoreReturnValueSuggester" )
79+ public final MetricsVerifier assertCounterWithAttributes (
80+ String metricName ,
81+ String description ,
82+ String unit ,
83+ Consumer <MapAssert <String , String >>... attributeGroupAssertions ) {
84+ return assertSumWithAttributes (
85+ metricName , description , unit , /* isMonotonic= */ true , attributeGroupAssertions );
86+ }
87+
88+ @ SafeVarargs
89+ @ SuppressWarnings ("CanIgnoreReturnValueSuggester" )
90+ public final MetricsVerifier assertUpDownCounterWithAttributes (
91+ String metricName ,
92+ String description ,
93+ String unit ,
94+ Consumer <MapAssert <String , String >>... attributeGroupAssertions ) {
95+ return assertSumWithAttributes (
96+ metricName , description , unit , /* isMonotonic= */ false , attributeGroupAssertions );
97+ }
98+
99+ @ SafeVarargs
100+ @ SuppressWarnings ("CanIgnoreReturnValueSuggester" )
101+ private final MetricsVerifier assertSumWithAttributes (
102+ String metricName ,
103+ String description ,
104+ String unit ,
105+ boolean isMonotonic ,
106+ Consumer <MapAssert <String , String >>... attributeGroupAssertions ) {
107+ assertions .put (
108+ metricName ,
109+ metric -> {
110+ assertDescription (metric , description );
111+ assertUnit (metric , unit );
112+ assertMetricWithSum (metric , isMonotonic );
113+ assertAttributedPoints (
114+ metricName , metric .getSum ().getDataPointsList (), attributeGroupAssertions );
115+ });
116+
117+ return this ;
118+ }
119+
67120 @ SuppressWarnings ("CanIgnoreReturnValueSuggester" )
68121 public MetricsVerifier assertTypedSum (
69122 String metricName , String description , String unit , List <String > types ) {
@@ -72,7 +125,7 @@ public MetricsVerifier assertTypedSum(
72125 metric -> {
73126 assertDescription (metric , description );
74127 assertUnit (metric , unit );
75- assertMetricWithSum (metric );
128+ assertMetricWithSum (metric , /* isMonotonic= */ true );
76129 assertTypedPoints (metricName , metric .getSum ().getDataPointsList (), types );
77130 });
78131
@@ -95,44 +148,65 @@ public MetricsVerifier assertTypedGauge(
95148 }
96149
97150 public void verify (List <Metric > metrics ) {
151+ verifyAllExpectedMetricsWereReceived (metrics );
152+
98153 Set <String > unverifiedMetrics = new HashSet <>();
99- Set <String > skippedAssertions = assertions .keySet ();
100154
101155 for (Metric metric : metrics ) {
102156 String metricName = metric .getName ();
103157 Consumer <Metric > assertion = assertions .get (metricName );
104158
105159 if (assertion != null ) {
106160 assertion .accept (metric );
107- skippedAssertions .remove (metricName );
108161 } else {
109162 unverifiedMetrics .add (metricName );
110163 }
111164 }
112165
113- if (!skippedAssertions .isEmpty ()) {
114- fail ("The following metrics was expected but not received: " + skippedAssertions );
115- }
116166 if (strictMode && !unverifiedMetrics .isEmpty ()) {
117- fail ("The following metrics was received but not verified: " + unverifiedMetrics );
167+ fail ("The following metrics were received but not verified: " + unverifiedMetrics );
168+ }
169+ }
170+
171+ @ SuppressWarnings ("SystemOut" )
172+ private void verifyAllExpectedMetricsWereReceived (List <Metric > metrics ) {
173+ Set <String > receivedMetricNames =
174+ metrics .stream ().map (Metric ::getName ).collect (Collectors .toSet ());
175+ Set <String > assertionNames = new HashSet <>(assertions .keySet ());
176+
177+ assertionNames .removeAll (receivedMetricNames );
178+ if (!assertionNames .isEmpty ()) {
179+ fail ("The following metrics were expected but not received: " + assertionNames );
118180 }
119181 }
120182
183+ @ SuppressWarnings ("CanIgnoreReturnValueSuggester" )
184+ private MetricsVerifier assertSum (
185+ String metricName , String description , String unit , boolean isMonotonic ) {
186+ assertions .put (
187+ metricName ,
188+ metric -> {
189+ assertDescription (metric , description );
190+ assertUnit (metric , unit );
191+ assertMetricWithSum (metric , isMonotonic );
192+ assertThat (metric .getSum ().getDataPointsList ())
193+ .satisfiesExactly (point -> assertThat (point .getAttributesList ()).isEmpty ());
194+ });
195+
196+ return this ;
197+ }
198+
121199 private static void assertMetricWithGauge (Metric metric ) {
122200 assertThat (metric .hasGauge ()).withFailMessage ("Metric with gauge expected" ).isTrue ();
123201 }
124202
125- private static void assertMetricWithSum (Metric metric ) {
203+ private static void assertMetricWithSum (Metric metric , boolean isMonotonic ) {
126204 assertThat (metric .hasSum ()).withFailMessage ("Metric with sum expected" ).isTrue ();
205+ assertThat (metric .getSum ().getIsMonotonic ())
206+ .withFailMessage ((isMonotonic ? "Monotonic" : "Non monotonic" ) + " sum expected" )
207+ .isEqualTo (isMonotonic );
127208 }
128209
129- // private static void assertMetricWithSum(Metric metric, boolean isMonotonic) {
130- // assertMetricWithSum(metric);
131- // assertThat(metric.getSum().getIsMonotonic())
132- // .withFailMessage("Metric should " + (isMonotonic ? "" : "not ") + "be monotonic")
133- // .isEqualTo(isMonotonic);
134- // }
135-
136210 private static void assertDescription (Metric metric , String expectedDescription ) {
137211 assertThat (metric .getDescription ())
138212 .describedAs (METRIC_VERIFICATION_FAILURE_MESSAGE , metric .getName ())
0 commit comments