diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/MetricAssert.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/MetricAssert.java index c086c74a7..94bd81187 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/MetricAssert.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/MetricAssert.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Map; import java.util.function.Consumer; +import java.util.logging.Logger; import java.util.stream.Collectors; import org.assertj.core.api.AbstractAssert; import org.assertj.core.internal.Integers; @@ -178,6 +179,17 @@ public MetricAssert hasDataPointsWithoutAttributes() { private MetricAssert checkDataPoints(Consumer> listConsumer) { // in practice usually one set of data points is provided but the // protobuf does not enforce that, so we have to ensure checking at least one + int count = consumeNumberDataPoints(listConsumer); + info.description("at least one set of data points expected for metric '%s'", actual.getName()); + integers.assertGreaterThan(info, count, 0); + + strictCheck( + "data point attributes", /* expectedCheckStatus= */ false, dataPointAttributesChecked); + dataPointAttributesChecked = true; + return this; + } + + private int consumeNumberDataPoints(Consumer> listConsumer) { int count = 0; if (actual.hasGauge()) { count++; @@ -187,13 +199,7 @@ private MetricAssert checkDataPoints(Consumer> listConsume count++; listConsumer.accept(actual.getSum().getDataPointsList()); } - info.description("at least one set of data points expected for metric '%s'", actual.getName()); - integers.assertGreaterThan(info, count, 0); - - strictCheck( - "data point attributes", /* expectedCheckStatus= */ false, dataPointAttributesChecked); - dataPointAttributesChecked = true; - return this; + return count; } private void dataPointsCommonCheck(List dataPoints) { @@ -263,4 +269,35 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherGroup... m } }); } + + /** + * Call this to have the metric data points for this metric name get logged. Care should be used, + * because it might be verbose. Can be used for sporadic/occasional test failures. + * + * @return this + */ + @CanIgnoreReturnValue + public MetricAssert logAttributes() { + Logger logger = Logger.getLogger(getClass().getName()); + consumeNumberDataPoints( + points -> + points.forEach( + point -> { + StringBuilder sb = new StringBuilder(actual.getName() + " -> attrs = ["); + List attrStrings = + point.getAttributesList().stream() + .map( + kv -> + ("{" + + kv.getKey() + + "=> " + + String.valueOf(kv.getValue()).trim() + + "}")) + .collect(Collectors.toList()); + sb.append(String.join(",", attrStrings)); + sb.append("]"); + logger.info(sb.toString()); + })); + return this; + } } diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/HBaseIntegrationTest.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/HBaseIntegrationTest.java index d8cf66eaa..4756fe270 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/HBaseIntegrationTest.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/HBaseIntegrationTest.java @@ -120,6 +120,7 @@ protected MetricsVerifier createMetricsVerifier() { "hbase.region_server.queue.length", metric -> metric + .logAttributes() .isUpDownCounter() .hasDescription("The number of RPC handlers actively servicing requests.") .hasUnit("{handler}")