From 10efe16ed998caea073d9d96e80d61c2a4948c56 Mon Sep 17 00:00:00 2001 From: Robert Niedziela <175605712+robsunday@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:35:13 +0100 Subject: [PATCH] [jmx-scraper] Assertions refactoring - Tomcat integration test converted (#1589) Co-authored-by: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Co-authored-by: jason plumb <75337021+breedx-splk@users.noreply.github.com> --- .../target_systems/TomcatIntegrationTest.java | 10 +- .../resources/target-systems/tomcat.groovy | 20 +-- .../target_systems/MetricAssertions.java | 58 -------- .../target_systems/TomcatIntegrationTest.java | 139 ++++++++++-------- jmx-scraper/src/main/resources/tomcat.yaml | 14 +- 5 files changed, 96 insertions(+), 145 deletions(-) diff --git a/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/TomcatIntegrationTest.java b/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/TomcatIntegrationTest.java index ad42c1927..5aa08f2c0 100644 --- a/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/TomcatIntegrationTest.java +++ b/jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/TomcatIntegrationTest.java @@ -53,13 +53,13 @@ class TomcatIntegrationTest extends AbstractIntegrationTest { void endToEnd() { waitAndAssertMetrics( metric -> - assertGauge(metric, "tomcat.sessions", "The number of active sessions.", "sessions"), + assertGauge(metric, "tomcat.sessions", "The number of active sessions.", "{session}"), metric -> assertSumWithAttributes( metric, "tomcat.errors", "The number of errors encountered.", - "errors", + "{error}", attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))), metric -> assertSumWithAttributes( @@ -73,7 +73,7 @@ void endToEnd() { metric, "tomcat.traffic", "The number of bytes transmitted and received.", - "by", + "By", attrs -> attrs.containsOnly( entry("proto_handler", "\"http-nio-8080\""), entry("direction", "sent")), @@ -86,7 +86,7 @@ void endToEnd() { metric, "tomcat.threads", "The number of threads", - "threads", + "{thread}", attrs -> attrs.containsOnly( entry("proto_handler", "\"http-nio-8080\""), entry("state", "idle")), @@ -105,7 +105,7 @@ void endToEnd() { metric, "tomcat.request_count", "The total requests.", - "requests", + "{request}", attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\"")))); } } diff --git a/jmx-metrics/src/main/resources/target-systems/tomcat.groovy b/jmx-metrics/src/main/resources/target-systems/tomcat.groovy index 8c692befc..32fe2246d 100644 --- a/jmx-metrics/src/main/resources/target-systems/tomcat.groovy +++ b/jmx-metrics/src/main/resources/target-systems/tomcat.groovy @@ -16,13 +16,13 @@ def beantomcatmanager = otel.mbeans("Catalina:type=Manager,host=localhost,context=*") -otel.instrument(beantomcatmanager, "tomcat.sessions", "The number of active sessions.", "sessions", "activeSessions", otel.&longValueCallback) +otel.instrument(beantomcatmanager, "tomcat.sessions", "The number of active sessions.", "{session}", "activeSessions", otel.&longValueCallback) def beantomcatrequestProcessor = otel.mbeans("Catalina:type=GlobalRequestProcessor,name=*") -otel.instrument(beantomcatrequestProcessor, "tomcat.errors", "The number of errors encountered.", "errors", +otel.instrument(beantomcatrequestProcessor, "tomcat.errors", "The number of errors encountered.", "{error}", ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }], "errorCount", otel.&longCounterCallback) -otel.instrument(beantomcatrequestProcessor, "tomcat.request_count", "The total requests.", "requests", +otel.instrument(beantomcatrequestProcessor, "tomcat.request_count", "The total requests.", "{request}", ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }], "requestCount", otel.&longCounterCallback) otel.instrument(beantomcatrequestProcessor, "tomcat.max_time", "Maximum time to process a request.", "ms", @@ -32,24 +32,24 @@ otel.instrument(beantomcatrequestProcessor, "tomcat.processing_time", "The total ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }], "processingTime", otel.&longCounterCallback) otel.instrument(beantomcatrequestProcessor, "tomcat.traffic", - "The number of bytes transmitted and received.", "by", + "The number of bytes transmitted and received.", "By", ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name")}], ["bytesReceived":["direction" : {"received"}], "bytesSent": ["direction" : {"sent"}]], otel.&longCounterCallback) def beantomcatconnectors = otel.mbeans("Catalina:type=ThreadPool,name=*") -otel.instrument(beantomcatconnectors, "tomcat.threads", "The number of threads", "threads", +otel.instrument(beantomcatconnectors, "tomcat.threads", "The number of threads", "{thread}", ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }], ["currentThreadCount":["state":{"idle"}],"currentThreadsBusy":["state":{"busy"}]], otel.&longValueCallback) def beantomcatnewmanager = otel.mbeans("Tomcat:type=Manager,host=localhost,context=*") -otel.instrument(beantomcatnewmanager, "tomcat.sessions", "The number of active sessions.", "sessions", "activeSessions", otel.&longValueCallback) +otel.instrument(beantomcatnewmanager, "tomcat.sessions", "The number of active sessions.", "{session}", "activeSessions", otel.&longValueCallback) def beantomcatnewrequestProcessor = otel.mbeans("Tomcat:type=GlobalRequestProcessor,name=*") -otel.instrument(beantomcatnewrequestProcessor, "tomcat.errors", "The number of errors encountered.", "errors", +otel.instrument(beantomcatnewrequestProcessor, "tomcat.errors", "The number of errors encountered.", "{error}", ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }], "errorCount", otel.&longCounterCallback) -otel.instrument(beantomcatnewrequestProcessor, "tomcat.request_count", "The total requests.", "requests", +otel.instrument(beantomcatnewrequestProcessor, "tomcat.request_count", "The total requests.", "{request}", ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }], "requestCount", otel.&longCounterCallback) otel.instrument(beantomcatnewrequestProcessor, "tomcat.max_time", "Maximum time to process a request.", "ms", @@ -59,12 +59,12 @@ otel.instrument(beantomcatnewrequestProcessor, "tomcat.processing_time", "The to ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }], "processingTime", otel.&longCounterCallback) otel.instrument(beantomcatnewrequestProcessor, "tomcat.traffic", - "The number of bytes transmitted and received.", "by", + "The number of bytes transmitted and received.", "By", ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name")}], ["bytesReceived":["direction" : {"received"}], "bytesSent": ["direction" : {"sent"}]], otel.&longCounterCallback) def beantomcatnewconnectors = otel.mbeans("Tomcat:type=ThreadPool,name=*") -otel.instrument(beantomcatnewconnectors, "tomcat.threads", "The number of threads", "threads", +otel.instrument(beantomcatnewconnectors, "tomcat.threads", "The number of threads", "{thread}", ["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }], ["currentThreadCount":["state":{"idle"}],"currentThreadsBusy":["state":{"busy"}]], otel.&longValueCallback) diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/MetricAssertions.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/MetricAssertions.java index 3cc4e8d50..826fe4729 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/MetricAssertions.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/MetricAssertions.java @@ -22,15 +22,6 @@ class MetricAssertions { private MetricAssertions() {} - static void assertGauge(Metric metric, String name, String description, String unit) { - assertThat(metric.getName()).isEqualTo(name); - assertThat(metric) - .hasDescription(description) - .hasUnit(unit) - .isGauge() - .hasDataPointsWithoutAttributes(); - } - static void assertSum(Metric metric, String name, String description, String unit) { assertSum(metric, name, description, unit, /* isMonotonic= */ true); } @@ -73,37 +64,6 @@ static void assertSumWithAttributes( assertAttributedPoints(metric.getSum().getDataPointsList(), attributeGroupAssertions); } - @SafeVarargs - static void assertSumWithAttributesMultiplePoints( - Metric metric, - String name, - String description, - String unit, - boolean isMonotonic, - Consumer>... attributeGroupAssertions) { - - assertThat(metric.getName()).isEqualTo(name); - assertThat(metric).hasDescription(description).hasUnit(unit); - - assertThat(metric.hasSum()).isTrue(); - assertThat(metric.getSum().getIsMonotonic()).isEqualTo(isMonotonic); - assertAttributedMultiplePoints(metric.getSum().getDataPointsList(), attributeGroupAssertions); - } - - @SafeVarargs - static void assertGaugeWithAttributes( - Metric metric, - String name, - String description, - String unit, - Consumer>... attributeGroupAssertions) { - assertThat(metric.getName()).isEqualTo(name); - - assertThat(metric).hasDescription(description).hasUnit(unit).isGauge(); - - assertAttributedPoints(metric.getGauge().getDataPointsList(), attributeGroupAssertions); - } - @SuppressWarnings("unchecked") private static void assertAttributedPoints( List points, @@ -122,22 +82,4 @@ private static void assertAttributedPoints( KeyValue::getKey, keyValue -> keyValue.getValue().getStringValue()))) .satisfiesExactlyInAnyOrder(assertions); } - - @SuppressWarnings("unchecked") - private static void assertAttributedMultiplePoints( - List points, - Consumer>... attributeGroupAssertions) { - - points.stream() - .map(NumberDataPoint::getAttributesList) - .forEach( - kvList -> { - Map kvMap = - kvList.stream() - .collect( - Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue())); - Arrays.stream(attributeGroupAssertions) - .forEach(assertion -> assertion.accept(assertThat(kvMap))); - }); - } } diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/TomcatIntegrationTest.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/TomcatIntegrationTest.java index 66fb3ec86..bec9453f8 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/TomcatIntegrationTest.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/TomcatIntegrationTest.java @@ -5,9 +5,9 @@ package io.opentelemetry.contrib.jmxscraper.target_systems; -import static io.opentelemetry.contrib.jmxscraper.target_systems.MetricAssertions.assertGaugeWithAttributes; -import static io.opentelemetry.contrib.jmxscraper.target_systems.MetricAssertions.assertSumWithAttributes; -import static org.assertj.core.api.Assertions.entry; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeWithAnyValue; import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer; import java.nio.file.Path; @@ -46,67 +46,76 @@ protected JmxScraperContainer customizeScraperContainer( } @Override - protected void verifyMetrics() { - waitAndAssertMetrics( - metric -> - assertGaugeWithAttributes( - metric, - "tomcat.sessions", - "The number of active sessions", - "sessions", - attrs -> attrs.containsKey("context")), - metric -> - assertSumWithAttributes( - metric, - "tomcat.errors", - "The number of errors encountered", - "errors", - attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))), - metric -> - assertSumWithAttributes( - metric, - "tomcat.processing_time", - "The total processing time", - "ms", - attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))), - metric -> - assertSumWithAttributes( - metric, - "tomcat.traffic", - "The number of bytes transmitted and received", - "by", - attrs -> - attrs.containsOnly( - entry("proto_handler", "\"http-nio-8080\""), entry("direction", "sent")), - attrs -> - attrs.containsOnly( - entry("proto_handler", "\"http-nio-8080\""), - entry("direction", "received"))), - metric -> - assertGaugeWithAttributes( - metric, - "tomcat.threads", - "The number of threads", - "threads", - attrs -> - attrs.containsOnly( - entry("proto_handler", "\"http-nio-8080\""), entry("state", "idle")), - attrs -> - attrs.containsOnly( - entry("proto_handler", "\"http-nio-8080\""), entry("state", "busy"))), - metric -> - assertGaugeWithAttributes( - metric, - "tomcat.max_time", - "Maximum time to process a request", - "ms", - attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))), - metric -> - assertSumWithAttributes( - metric, - "tomcat.request_count", - "The total requests", - "requests", - attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\"")))); + protected MetricsVerifier createMetricsVerifier() { + return MetricsVerifier.create() + .add( + "tomcat.sessions", + metric -> + metric + .hasDescription("The number of active sessions") + .hasUnit("{session}") + .isGauge() + .hasDataPointsWithOneAttribute(attributeWithAnyValue("context"))) + .add( + "tomcat.errors", + metric -> + metric + .hasDescription("The number of errors encountered") + .hasUnit("{error}") + .isCounter() + .hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\""))) + .add( + "tomcat.processing_time", + metric -> + metric + .hasDescription("The total processing time") + .hasUnit("ms") + .isCounter() + .hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\""))) + .add( + "tomcat.traffic", + metric -> + metric + .hasDescription("The number of bytes transmitted and received") + .hasUnit("By") + .isCounter() + .hasDataPointsWithAttributes( + attributeGroup( + attribute("direction", "sent"), + attribute("proto_handler", "\"http-nio-8080\"")), + attributeGroup( + attribute("direction", "received"), + attribute("proto_handler", "\"http-nio-8080\"")))) + .add( + "tomcat.threads", + metric -> + metric + .hasDescription("The number of threads") + .hasUnit("{thread}") + .isGauge() + .hasDataPointsWithAttributes( + attributeGroup( + attribute("state", "idle"), + attribute("proto_handler", "\"http-nio-8080\"")), + attributeGroup( + attribute("state", "busy"), + attribute("proto_handler", "\"http-nio-8080\"")))) + .add( + "tomcat.max_time", + metric -> + metric + .hasDescription("Maximum time to process a request") + .hasUnit("ms") + .isGauge() + .hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\""))) + .add( + "tomcat.request_count", + metric -> + metric + .hasDescription("The total requests") + .hasUnit("{request}") + .isCounter() + .hasDataPointsWithOneAttribute( + attribute("proto_handler", "\"http-nio-8080\""))); } } diff --git a/jmx-scraper/src/main/resources/tomcat.yaml b/jmx-scraper/src/main/resources/tomcat.yaml index 076be6400..ad67ce8b8 100644 --- a/jmx-scraper/src/main/resources/tomcat.yaml +++ b/jmx-scraper/src/main/resources/tomcat.yaml @@ -15,7 +15,7 @@ rules: activeSessions: metric: tomcat.sessions type: gauge - unit: sessions + unit: "{session}" desc: The number of active sessions - beans: @@ -28,12 +28,12 @@ rules: errorCount: metric: errors type: counter - unit: errors + unit: "{error}" desc: The number of errors encountered requestCount: metric: request_count type: counter - unit: requests + unit: "{request}" desc: The total requests maxTime: metric: max_time @@ -48,14 +48,14 @@ rules: bytesSent: metric: traffic type: counter - unit: by + unit: By desc: The number of bytes transmitted and received metricAttribute: direction: const(sent) bytesReceived: metric: traffic type: counter - unit: by + unit: By desc: The number of bytes transmitted and received metricAttribute: direction: const(received) @@ -71,13 +71,13 @@ rules: metric: threads desc: The number of threads type: gauge - unit: threads + unit: "{thread}" metricAttribute: state: const(idle) currentThreadsBusy: metric: threads desc: The number of threads type: gauge - unit: threads + unit: "{thread}" metricAttribute: state: const(busy)