diff --git a/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/JvmTargetSystemIntegrationTest.java b/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/JvmTargetSystemIntegrationTest.java index 91de5de0d..682e23957 100644 --- a/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/JvmTargetSystemIntegrationTest.java +++ b/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/JvmTargetSystemIntegrationTest.java @@ -31,13 +31,13 @@ void endToEnd() { "Metaspace", "Par Survivor Space"); waitAndAssertMetrics( - metric -> assertGauge(metric, "jvm.classes.loaded", "number of loaded classes", "1"), + metric -> assertGauge(metric, "jvm.classes.loaded", "number of loaded classes", "{class}"), metric -> assertTypedSum( metric, "jvm.gc.collections.count", "total number of collections that have occurred", - "1", + "{collection}", Arrays.asList("ConcurrentMarkSweep", "ParNew")), metric -> assertTypedSum( @@ -46,27 +46,27 @@ void endToEnd() { "the approximate accumulated collection elapsed time in milliseconds", "ms", Arrays.asList("ConcurrentMarkSweep", "ParNew")), - metric -> assertGauge(metric, "jvm.memory.heap.committed", "current heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.heap.init", "current heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.heap.max", "current heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.heap.used", "current heap usage", "by"), + metric -> assertGauge(metric, "jvm.memory.heap.committed", "current heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.heap.init", "current heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.heap.max", "current heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.heap.used", "current heap usage", "By"), metric -> - assertGauge(metric, "jvm.memory.nonheap.committed", "current non-heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.nonheap.init", "current non-heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.nonheap.max", "current non-heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.nonheap.used", "current non-heap usage", "by"), + assertGauge(metric, "jvm.memory.nonheap.committed", "current non-heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.nonheap.init", "current non-heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.nonheap.max", "current non-heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.nonheap.used", "current non-heap usage", "By"), metric -> assertTypedGauge( - metric, "jvm.memory.pool.committed", "current memory pool usage", "by", gcLabels), + metric, "jvm.memory.pool.committed", "current memory pool usage", "By", gcLabels), metric -> assertTypedGauge( - metric, "jvm.memory.pool.init", "current memory pool usage", "by", gcLabels), + metric, "jvm.memory.pool.init", "current memory pool usage", "By", gcLabels), metric -> assertTypedGauge( - metric, "jvm.memory.pool.max", "current memory pool usage", "by", gcLabels), + metric, "jvm.memory.pool.max", "current memory pool usage", "By", gcLabels), metric -> assertTypedGauge( - metric, "jvm.memory.pool.used", "current memory pool usage", "by", gcLabels), - metric -> assertGauge(metric, "jvm.threads.count", "number of threads", "1")); + metric, "jvm.memory.pool.used", "current memory pool usage", "By", gcLabels), + metric -> assertGauge(metric, "jvm.threads.count", "number of threads", "{thread}")); } } diff --git a/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/KafkaIntegrationTest.java b/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/KafkaIntegrationTest.java index acc272262..a54909b67 100644 --- a/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/KafkaIntegrationTest.java +++ b/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/KafkaIntegrationTest.java @@ -380,13 +380,14 @@ void endToEnd() { List> assertions = new ArrayList<>(kafkaBrokerAssertions()); assertions.addAll( Arrays.asList( - metric -> assertGauge(metric, "jvm.classes.loaded", "number of loaded classes", "1"), + metric -> + assertGauge(metric, "jvm.classes.loaded", "number of loaded classes", "{class}"), metric -> assertTypedSum( metric, "jvm.gc.collections.count", "total number of collections that have occurred", - "1", + "{collection}", Arrays.asList("G1 Young Generation", "G1 Old Generation")), metric -> assertTypedSum( @@ -396,36 +397,36 @@ void endToEnd() { "ms", Arrays.asList("G1 Young Generation", "G1 Old Generation")), metric -> - assertGauge(metric, "jvm.memory.heap.committed", "current heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.heap.init", "current heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.heap.max", "current heap usage", "by"), - metric -> assertGauge(metric, "jvm.memory.heap.used", "current heap usage", "by"), + assertGauge(metric, "jvm.memory.heap.committed", "current heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.heap.init", "current heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.heap.max", "current heap usage", "By"), + metric -> assertGauge(metric, "jvm.memory.heap.used", "current heap usage", "By"), metric -> assertGauge( - metric, "jvm.memory.nonheap.committed", "current non-heap usage", "by"), + metric, "jvm.memory.nonheap.committed", "current non-heap usage", "By"), metric -> - assertGauge(metric, "jvm.memory.nonheap.init", "current non-heap usage", "by"), + assertGauge(metric, "jvm.memory.nonheap.init", "current non-heap usage", "By"), metric -> - assertGauge(metric, "jvm.memory.nonheap.max", "current non-heap usage", "by"), + assertGauge(metric, "jvm.memory.nonheap.max", "current non-heap usage", "By"), metric -> - assertGauge(metric, "jvm.memory.nonheap.used", "current non-heap usage", "by"), + assertGauge(metric, "jvm.memory.nonheap.used", "current non-heap usage", "By"), metric -> assertTypedGauge( metric, "jvm.memory.pool.committed", "current memory pool usage", - "by", + "By", gcLabels), metric -> assertTypedGauge( - metric, "jvm.memory.pool.init", "current memory pool usage", "by", gcLabels), + metric, "jvm.memory.pool.init", "current memory pool usage", "By", gcLabels), metric -> assertTypedGauge( - metric, "jvm.memory.pool.max", "current memory pool usage", "by", gcLabels), + metric, "jvm.memory.pool.max", "current memory pool usage", "By", gcLabels), metric -> assertTypedGauge( - metric, "jvm.memory.pool.used", "current memory pool usage", "by", gcLabels), - metric -> assertGauge(metric, "jvm.threads.count", "number of threads", "1"))); + metric, "jvm.memory.pool.used", "current memory pool usage", "By", gcLabels), + metric -> assertGauge(metric, "jvm.threads.count", "number of threads", "{thread}"))); waitAndAssertMetrics(assertions); } diff --git a/jmx-metrics/src/main/resources/target-systems/jvm.groovy b/jmx-metrics/src/main/resources/target-systems/jvm.groovy index 4de2a6a02..73e33b0d5 100644 --- a/jmx-metrics/src/main/resources/target-systems/jvm.groovy +++ b/jmx-metrics/src/main/resources/target-systems/jvm.groovy @@ -16,11 +16,11 @@ def classLoading = otel.mbean("java.lang:type=ClassLoading") otel.instrument(classLoading, "jvm.classes.loaded", "number of loaded classes", - "1", "LoadedClassCount", otel.&longValueCallback) + "{class}", "LoadedClassCount", otel.&longValueCallback) def garbageCollector = otel.mbeans("java.lang:type=GarbageCollector,*") otel.instrument(garbageCollector, "jvm.gc.collections.count", "total number of collections that have occurred", - "1", ["name" : { mbean -> mbean.name().getKeyProperty("name") }], + "{collection}", ["name" : { mbean -> mbean.name().getKeyProperty("name") }], "CollectionCount", otel.&longCounterCallback) otel.instrument(garbageCollector, "jvm.gc.collections.elapsed", "the approximate accumulated collection elapsed time in milliseconds", "ms", @@ -29,15 +29,15 @@ otel.instrument(garbageCollector, "jvm.gc.collections.elapsed", def memory = otel.mbean("java.lang:type=Memory") otel.instrument(memory, "jvm.memory.heap", "current heap usage", - "by", "HeapMemoryUsage", otel.&longValueCallback) + "By", "HeapMemoryUsage", otel.&longValueCallback) otel.instrument(memory, "jvm.memory.nonheap", "current non-heap usage", - "by", "NonHeapMemoryUsage", otel.&longValueCallback) + "By", "NonHeapMemoryUsage", otel.&longValueCallback) def memoryPool = otel.mbeans("java.lang:type=MemoryPool,*") otel.instrument(memoryPool, "jvm.memory.pool", "current memory pool usage", - "by", ["name" : { mbean -> mbean.name().getKeyProperty("name") }], + "By", ["name" : { mbean -> mbean.name().getKeyProperty("name") }], "Usage", otel.&longValueCallback) def threading = otel.mbean("java.lang:type=Threading") otel.instrument(threading, "jvm.threads.count", "number of threads", - "1", "ThreadCount", otel.&longValueCallback) + "{thread}", "ThreadCount", otel.&longValueCallback) 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 1a4eaace3..c086c74a7 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 @@ -12,11 +12,8 @@ import io.opentelemetry.proto.metrics.v1.Metric; import io.opentelemetry.proto.metrics.v1.NumberDataPoint; import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; import org.assertj.core.api.AbstractAssert; @@ -199,31 +196,6 @@ private MetricAssert checkDataPoints(Consumer> listConsume return this; } - // TODO: To be removed and calls will be replaced with hasDataPointsWithAttributes() - @CanIgnoreReturnValue - public MetricAssert hasTypedDataPoints(Collection types) { - return checkDataPoints( - dataPoints -> { - dataPointsCommonCheck(dataPoints); - - Set foundValues = new HashSet<>(); - for (NumberDataPoint dataPoint : dataPoints) { - List attributes = dataPoint.getAttributesList(); - - info.description( - "expected exactly one 'name' attribute for typed data point in metric '%s'", - actual.getName()); - iterables.assertHasSize(info, attributes, 1); - - objects.assertEqual(info, attributes.get(0).getKey(), "name"); - foundValues.add(attributes.get(0).getValue().getStringValue()); - } - info.description( - "missing or unexpected type attribute for metric '%s'", actual.getName()); - iterables.assertContainsExactlyInAnyOrder(info, foundValues, types.toArray()); - }); - } - private void dataPointsCommonCheck(List dataPoints) { info.description("unable to retrieve data points from metric '%s'", actual.getName()); objects.assertNotNull(info, dataPoints); diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JvmIntegrationTest.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JvmIntegrationTest.java index 70dacdccc..3e9b73367 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JvmIntegrationTest.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JvmIntegrationTest.java @@ -5,11 +5,13 @@ package io.opentelemetry.contrib.jmxscraper.target_systems; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup; + import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer; import io.opentelemetry.contrib.jmxscraper.TestAppContainer; +import io.opentelemetry.contrib.jmxscraper.assertions.AttributeMatcherGroup; import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.Wait; @@ -34,15 +36,16 @@ protected JmxScraperContainer customizeScraperContainer( @Override protected MetricsVerifier createMetricsVerifier() { // those values depend on the JVM GC configured - List gcLabels = - Arrays.asList( + AttributeMatcherGroup[] memoryAttributes = + nameAttributeMatchers( "Code Cache", "PS Eden Space", "PS Old Gen", "Metaspace", "Compressed Class Space", "PS Survivor Space"); - List gcCollectionLabels = Arrays.asList("PS MarkSweep", "PS Scavenge"); + AttributeMatcherGroup[] gcAlgorithmAttributes = + nameAttributeMatchers("PS MarkSweep", "PS Scavenge"); return MetricsVerifier.create() .add( @@ -50,7 +53,7 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("number of loaded classes") - .hasUnit("1") + .hasUnit("{class}") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -58,9 +61,9 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("total number of collections that have occurred") - .hasUnit("1") + .hasUnit("{collection}") .isCounter() - .hasTypedDataPoints(gcCollectionLabels)) + .hasDataPointsWithAttributes(gcAlgorithmAttributes)) .add( "jvm.gc.collections.elapsed", metric -> @@ -69,13 +72,13 @@ protected MetricsVerifier createMetricsVerifier() { "the approximate accumulated collection elapsed time in milliseconds") .hasUnit("ms") .isCounter() - .hasTypedDataPoints(gcCollectionLabels)) + .hasDataPointsWithAttributes(gcAlgorithmAttributes)) .add( "jvm.memory.heap.committed", metric -> metric .hasDescription("current heap usage") - .hasUnit("by") + .hasUnit("By") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -83,7 +86,7 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("current heap usage") - .hasUnit("by") + .hasUnit("By") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -91,7 +94,7 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("current heap usage") - .hasUnit("by") + .hasUnit("By") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -99,7 +102,7 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("current heap usage") - .hasUnit("by") + .hasUnit("By") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -107,7 +110,7 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("current non-heap usage") - .hasUnit("by") + .hasUnit("By") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -115,7 +118,7 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("current non-heap usage") - .hasUnit("by") + .hasUnit("By") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -123,7 +126,7 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("current non-heap usage") - .hasUnit("by") + .hasUnit("By") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -131,7 +134,7 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("current non-heap usage") - .hasUnit("by") + .hasUnit("By") .isGauge() .hasDataPointsWithoutAttributes()) .add( @@ -139,40 +142,48 @@ protected MetricsVerifier createMetricsVerifier() { metric -> metric .hasDescription("current memory pool usage") - .hasUnit("by") + .hasUnit("By") .isGauge() - .hasTypedDataPoints(gcLabels)) + .hasDataPointsWithAttributes(memoryAttributes)) .add( "jvm.memory.pool.init", metric -> metric .hasDescription("current memory pool usage") - .hasUnit("by") + .hasUnit("By") .isGauge() - .hasTypedDataPoints(gcLabels)) + .hasDataPointsWithAttributes(memoryAttributes)) .add( "jvm.memory.pool.max", metric -> metric .hasDescription("current memory pool usage") - .hasUnit("by") + .hasUnit("By") .isGauge() - .hasTypedDataPoints(gcLabels)) + .hasDataPointsWithAttributes(memoryAttributes)) .add( "jvm.memory.pool.used", metric -> metric .hasDescription("current memory pool usage") - .hasUnit("by") + .hasUnit("By") .isGauge() - .hasTypedDataPoints(gcLabels)) + .hasDataPointsWithAttributes(memoryAttributes)) .add( "jvm.threads.count", metric -> metric .hasDescription("number of threads") - .hasUnit("1") + .hasUnit("{thread}") .isGauge() .hasDataPointsWithoutAttributes()); } + + private static AttributeMatcherGroup[] nameAttributeMatchers(String... values) { + AttributeMatcherGroup[] groups = new AttributeMatcherGroup[values.length]; + for (int i = 0; i < values.length; i++) { + groups[i] = attributeGroup(attribute("name", values[i])); + } + return groups; + } } diff --git a/jmx-scraper/src/main/resources/jvm.yaml b/jmx-scraper/src/main/resources/jvm.yaml index d3e95d5d4..b706ff7c0 100644 --- a/jmx-scraper/src/main/resources/jvm.yaml +++ b/jmx-scraper/src/main/resources/jvm.yaml @@ -7,7 +7,7 @@ rules: LoadedClassCount: metric: jvm.classes.loaded type: gauge - unit: '1' + unit: '{class}' desc: number of loaded classes - bean: java.lang:type=GarbageCollector,name=* @@ -15,7 +15,7 @@ rules: CollectionCount: metric: jvm.gc.collections.count type: counter - unit: '1' + unit: '{collection}' desc: total number of collections that have occurred metricAttribute: name: param(name) @@ -28,7 +28,7 @@ rules: name: param(name) - bean: java.lang:type=Memory - unit: by + unit: By prefix: jvm.memory. mapping: HeapMemoryUsage.committed: @@ -66,7 +66,7 @@ rules: - bean: java.lang:type=MemoryPool,name=* type: gauge - unit: by + unit: By metricAttribute: name: param(name) mapping: @@ -87,5 +87,5 @@ rules: mapping: ThreadCount: metric: jvm.threads.count - unit: '1' + unit: '{thread}' desc: number of threads