1515import java .util .List ;
1616import java .util .Map ;
1717import java .util .function .Consumer ;
18+ import java .util .logging .Logger ;
1819import java .util .stream .Collectors ;
1920import org .assertj .core .api .AbstractAssert ;
2021import org .assertj .core .internal .Integers ;
@@ -178,6 +179,17 @@ public MetricAssert hasDataPointsWithoutAttributes() {
178179 private MetricAssert checkDataPoints (Consumer <List <NumberDataPoint >> listConsumer ) {
179180 // in practice usually one set of data points is provided but the
180181 // protobuf does not enforce that, so we have to ensure checking at least one
182+ int count = consumeNumberDataPoints (listConsumer );
183+ info .description ("at least one set of data points expected for metric '%s'" , actual .getName ());
184+ integers .assertGreaterThan (info , count , 0 );
185+
186+ strictCheck (
187+ "data point attributes" , /* expectedCheckStatus= */ false , dataPointAttributesChecked );
188+ dataPointAttributesChecked = true ;
189+ return this ;
190+ }
191+
192+ private int consumeNumberDataPoints (Consumer <List <NumberDataPoint >> listConsumer ) {
181193 int count = 0 ;
182194 if (actual .hasGauge ()) {
183195 count ++;
@@ -187,13 +199,7 @@ private MetricAssert checkDataPoints(Consumer<List<NumberDataPoint>> listConsume
187199 count ++;
188200 listConsumer .accept (actual .getSum ().getDataPointsList ());
189201 }
190- info .description ("at least one set of data points expected for metric '%s'" , actual .getName ());
191- integers .assertGreaterThan (info , count , 0 );
192-
193- strictCheck (
194- "data point attributes" , /* expectedCheckStatus= */ false , dataPointAttributesChecked );
195- dataPointAttributesChecked = true ;
196- return this ;
202+ return count ;
197203 }
198204
199205 private void dataPointsCommonCheck (List <NumberDataPoint > dataPoints ) {
@@ -263,4 +269,35 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherGroup... m
263269 }
264270 });
265271 }
272+
273+ /**
274+ * Call this to have the metric data points for this metric name get logged. Care should be used,
275+ * because it might be verbose. Can be used for sporadic/occasional test failures.
276+ *
277+ * @return this
278+ */
279+ @ CanIgnoreReturnValue
280+ public MetricAssert logAttributes () {
281+ Logger logger = Logger .getLogger (getClass ().getName ());
282+ consumeNumberDataPoints (
283+ points ->
284+ points .forEach (
285+ point -> {
286+ StringBuilder sb = new StringBuilder (actual .getName () + " -> attrs = [" );
287+ List <String > attrStrings =
288+ point .getAttributesList ().stream ()
289+ .map (
290+ kv ->
291+ ("{"
292+ + kv .getKey ()
293+ + "=> "
294+ + String .valueOf (kv .getValue ()).trim ()
295+ + "}" ))
296+ .collect (Collectors .toList ());
297+ sb .append (String .join ("," , attrStrings ));
298+ sb .append ("]" );
299+ logger .info (sb .toString ());
300+ }));
301+ return this ;
302+ }
266303}
0 commit comments