From ba0f9003e01843100fb0d5bc120054a2c0430048 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:21:02 +0100 Subject: [PATCH 1/9] add attribute matcher set class --- .../assertions/AttributeMatcherSet.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java new file mode 100644 index 000000000..2c884681b --- /dev/null +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java @@ -0,0 +1,37 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.contrib.jmxscraper.assertions; + +import java.util.Collection; +import java.util.Map; +import java.util.stream.Collectors; + +/** Holder class for a set of attribute matchers */ +public class AttributeMatcherSet { + + // stored as a Map for easy lookup by name + private final Map matchers; + + /** + * Constructor for a set of attribute matchers + * + * @param matchers collection of matchers to build a set from + * @throws IllegalStateException if there is any duplicate key + */ + AttributeMatcherSet(Collection matchers) { + this.matchers = + matchers.stream().collect(Collectors.toMap(AttributeMatcher::getAttributeName, m -> m)); + } + + Map getMatchers() { + return matchers; + } + + @Override + public String toString() { + return matchers.values().toString(); + } +} From fdc64e3bb2d57c7468afc109296870a51c47b4d6 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:21:34 +0100 Subject: [PATCH 2/9] remove equals/hashcode --- .../assertions/AttributeMatcher.java | 26 +++---------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java index 55b13cbb9..ed2226131 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java @@ -6,12 +6,12 @@ package io.opentelemetry.contrib.jmxscraper.assertions; import java.util.Objects; +import javax.annotation.Nullable; /** Implements functionality of matching data point attributes. */ public class AttributeMatcher { private final String attributeName; - @Nullable - private final String attributeValue; + @Nullable private final String attributeValue; /** * Create instance used to match data point attribute with any value. @@ -29,7 +29,7 @@ public class AttributeMatcher { * @param attributeName attribute name * @param attributeValue attribute value */ - AttributeMatcher(String attributeName, String attributeValue) { + AttributeMatcher(String attributeName, @Nullable String attributeValue) { this.attributeName = attributeName; this.attributeValue = attributeValue; } @@ -43,24 +43,6 @@ public String getAttributeName() { return attributeName; } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof AttributeMatcher)) { - return false; - } - AttributeMatcher other = (AttributeMatcher) o; - return Objects.equals(attributeName, other.attributeName); - } - - @Override - public int hashCode() { - // Do not use value matcher here to support value wildcards - return Objects.hash(attributeName); - } - @Override public String toString() { return attributeValue == null @@ -76,7 +58,7 @@ public String toString() { * @return true if this matcher is matching provided value, false otherwise. */ boolean matchesValue(String value) { - if ((attributeValue == null) || (value == null)) { + if (attributeValue == null) { return true; } return Objects.equals(attributeValue, value); From 7754e3ac2c35530607bcf78e4592609dce8aa6ec Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:24:50 +0100 Subject: [PATCH 3/9] use AttributeMatcherSet for matching --- .../assertions/DataPointAttributes.java | 15 ++----- .../jmxscraper/assertions/MetricAssert.java | 39 ++++++++++++++----- .../CassandraIntegrationTest.java | 5 +-- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java index ba8d7d016..2feb76b47 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java @@ -6,8 +6,6 @@ package io.opentelemetry.contrib.jmxscraper.assertions; import java.util.Arrays; -import java.util.Set; -import java.util.stream.Collectors; /** * Utility class implementing convenience static methods to construct data point attribute matchers @@ -46,15 +44,10 @@ public static AttributeMatcher attributeWithAnyValue(String name) { * @return set of unique attribute matchers * @throws IllegalArgumentException if provided list contains two or more matchers with the same * name. - * @see MetricAssert#hasDataPointsWithAttributes(Set[]) for detailed description of the algorithm - * used for matching + * @see MetricAssert#hasDataPointsWithAttributes(AttributeMatcherSet...) for detailed description + * off the algorithm used for matching */ - public static Set attributeSet(AttributeMatcher... attributes) { - Set matcherSet = Arrays.stream(attributes).collect(Collectors.toSet()); - if (matcherSet.size() < attributes.length) { - throw new IllegalArgumentException( - "Duplicated matchers found in " + Arrays.toString(attributes)); - } - return matcherSet; + public static AttributeMatcherSet attributeSet(AttributeMatcher... attributes) { + return new AttributeMatcherSet(Arrays.asList(attributes)); } } 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 94a53444f..b3e200393 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 @@ -254,11 +254,9 @@ public final MetricAssert hasDataPointsWithOneAttribute(AttributeMatcher expecte * @param attributeMatchers array of attribute matcher sets * @return this */ - @SafeVarargs @CanIgnoreReturnValue @SuppressWarnings("varargs") // required to avoid warning - public final MetricAssert hasDataPointsWithAttributes( - Set... attributeMatchers) { + public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... attributeMatchers) { return checkDataPoints( dataPoints -> { dataPointsCommonCheck(dataPoints); @@ -293,16 +291,37 @@ public final MetricAssert hasDataPointsWithAttributes( } private static boolean matchAttributes( - Set attributeMatchers, Map dataPointAttributes) { - if (attributeMatchers.size() != dataPointAttributes.size()) { - return false; - } - for (AttributeMatcher matcher : attributeMatchers) { - String attributeValue = dataPointAttributes.get(matcher.getAttributeName()); - if (!matcher.matchesValue(attributeValue)) { + AttributeMatcherSet attributeMatcherSet, Map dataPointAttributes) { + + Map matchers = attributeMatcherSet.getMatchers(); + + Set toMatch = new HashSet<>(dataPointAttributes.keySet()); + Set matched = new HashSet<>(); + for (Map.Entry entry : dataPointAttributes.entrySet()) { + AttributeMatcher matcher = matchers.get(entry.getKey()); + if (matcher == null) { + // no matcher for this key: unexpected key + return false; + } + + String value = entry.getValue(); + if (!matcher.matchesValue(value)) { + // value does not match: unexpected value return false; } + toMatch.remove(entry.getKey()); + matched.add(entry.getKey()); } + + if (!toMatch.isEmpty()) { + // unexpected entries in attributes + return false; + } + if (!matched.containsAll(matchers.keySet())) { + // some matchers were not match + return false; + } + return true; } diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/CassandraIntegrationTest.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/CassandraIntegrationTest.java index e72c904ee..a9a55ad9d 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/CassandraIntegrationTest.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/CassandraIntegrationTest.java @@ -9,10 +9,9 @@ import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeSet; import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer; -import io.opentelemetry.contrib.jmxscraper.assertions.AttributeMatcher; +import io.opentelemetry.contrib.jmxscraper.assertions.AttributeMatcherSet; import java.nio.file.Path; import java.time.Duration; -import java.util.Set; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.Wait; @@ -184,7 +183,7 @@ protected MetricsVerifier createMetricsVerifier() { errorCountAttributes("Write", "Unavailable"))); } - private static Set errorCountAttributes(String operation, String status) { + private static AttributeMatcherSet errorCountAttributes(String operation, String status) { return attributeSet(attribute("operation", operation), attribute("status", status)); } } From 7b7f0d08c9fd50e3f3940c92a00053d294839572 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:33:41 +0100 Subject: [PATCH 4/9] code cleanup --- .../contrib/jmxscraper/assertions/MetricAssert.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 b3e200393..bd8e6d790 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 @@ -255,7 +255,6 @@ public final MetricAssert hasDataPointsWithOneAttribute(AttributeMatcher expecte * @return this */ @CanIgnoreReturnValue - @SuppressWarnings("varargs") // required to avoid warning public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... attributeMatchers) { return checkDataPoints( dataPoints -> { @@ -318,7 +317,7 @@ private static boolean matchAttributes( return false; } if (!matched.containsAll(matchers.keySet())) { - // some matchers were not match + // some matchers were not matched return false; } From b170021665d28702e734d549173d497d209940b7 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:02:54 +0100 Subject: [PATCH 5/9] move set matching --- .../assertions/AttributeMatcherSet.java | 27 ++++++++++++++ .../jmxscraper/assertions/MetricAssert.java | 37 +------------------ 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java index 2c884681b..649262c0b 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java @@ -30,6 +30,33 @@ Map getMatchers() { return matchers; } + /** + * Checks if attributes match this attribute matcher set + * + * @param attributes attributes to check as map + * @return {@literal true} when the attributes match all attributes from this set + */ + public boolean matches(Map attributes) { + if(attributes.size() != matchers.size()) { + return false; + } + + for (Map.Entry entry : attributes.entrySet()) { + AttributeMatcher matcher = matchers.get(entry.getKey()); + if (matcher == null) { + // no matcher for this key: unexpected key + return false; + } + + if (!matcher.matchesValue(entry.getValue())) { + // value does not match: unexpected value + return false; + } + } + + return true; + } + @Override public String toString() { return matchers.values().toString(); 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 bd8e6d790..4dbdc7a33 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 @@ -267,7 +267,7 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att Map dataPointAttributes = toMap(dataPoint.getAttributesList()); int matchCount = 0; for (int i = 0; i < attributeMatchers.length; i++) { - if (matchAttributes(attributeMatchers[i], dataPointAttributes)) { + if (attributeMatchers[i].matches(dataPointAttributes)) { matchedSets[i] = true; matchCount++; } @@ -289,41 +289,6 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att }); } - private static boolean matchAttributes( - AttributeMatcherSet attributeMatcherSet, Map dataPointAttributes) { - - Map matchers = attributeMatcherSet.getMatchers(); - - Set toMatch = new HashSet<>(dataPointAttributes.keySet()); - Set matched = new HashSet<>(); - for (Map.Entry entry : dataPointAttributes.entrySet()) { - AttributeMatcher matcher = matchers.get(entry.getKey()); - if (matcher == null) { - // no matcher for this key: unexpected key - return false; - } - - String value = entry.getValue(); - if (!matcher.matchesValue(value)) { - // value does not match: unexpected value - return false; - } - toMatch.remove(entry.getKey()); - matched.add(entry.getKey()); - } - - if (!toMatch.isEmpty()) { - // unexpected entries in attributes - return false; - } - if (!matched.containsAll(matchers.keySet())) { - // some matchers were not matched - return false; - } - - return true; - } - private static Map toMap(List list) { return list.stream() .collect(Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue())); From 59a7dfc951e5f9cd24cf635db48b97789c6589d4 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:03:23 +0100 Subject: [PATCH 6/9] inline conversion to map --- .../contrib/jmxscraper/assertions/MetricAssert.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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 4dbdc7a33..3137ce7d8 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 @@ -264,7 +264,8 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att // validate each datapoint attributes match exactly one of the provided attributes sets for (NumberDataPoint dataPoint : dataPoints) { - Map dataPointAttributes = toMap(dataPoint.getAttributesList()); + Map dataPointAttributes = dataPoint.getAttributesList().stream() + .collect(Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue())); int matchCount = 0; for (int i = 0; i < attributeMatchers.length; i++) { if (attributeMatchers[i].matches(dataPointAttributes)) { @@ -289,8 +290,4 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att }); } - private static Map toMap(List list) { - return list.stream() - .collect(Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue())); - } } From 34b3ab30d59d40627c5e986467cbef5ac242c780 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:06:38 +0100 Subject: [PATCH 7/9] reformat & cleanup --- .../contrib/jmxscraper/assertions/AttributeMatcherSet.java | 6 +----- .../contrib/jmxscraper/assertions/MetricAssert.java | 7 ++++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java index 649262c0b..9fab0c56e 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java @@ -26,10 +26,6 @@ public class AttributeMatcherSet { matchers.stream().collect(Collectors.toMap(AttributeMatcher::getAttributeName, m -> m)); } - Map getMatchers() { - return matchers; - } - /** * Checks if attributes match this attribute matcher set * @@ -37,7 +33,7 @@ Map getMatchers() { * @return {@literal true} when the attributes match all attributes from this set */ public boolean matches(Map attributes) { - if(attributes.size() != matchers.size()) { + if (attributes.size() != matchers.size()) { return false; } 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 3137ce7d8..6c712c671 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 @@ -264,8 +264,10 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att // validate each datapoint attributes match exactly one of the provided attributes sets for (NumberDataPoint dataPoint : dataPoints) { - Map dataPointAttributes = dataPoint.getAttributesList().stream() - .collect(Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue())); + Map dataPointAttributes = + dataPoint.getAttributesList().stream() + .collect( + Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue())); int matchCount = 0; for (int i = 0; i < attributeMatchers.length; i++) { if (attributeMatchers[i].matches(dataPointAttributes)) { @@ -289,5 +291,4 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att } }); } - } From 45ba126f3c2c78f8c643c610f0a9bc6488348158 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:54:45 +0100 Subject: [PATCH 8/9] simplify matchesValue --- .../contrib/jmxscraper/assertions/AttributeMatcher.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java index 097f74a48..04aaff092 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java @@ -57,9 +57,6 @@ public String toString() { * @return true if this matcher is matching provided value, false otherwise. */ boolean matchesValue(String value) { - if (attributeValue == null) { - return true; - } - return Objects.equals(attributeValue, value); + return attributeValue == null || attributeValue.equals(value); } } From 5e69a82d2ef3e7532fe9f34548b6c36550ab6393 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:00:45 +0100 Subject: [PATCH 9/9] rename attribute set -> group --- .../assertions/AttributeMatcher.java | 1 - ...herSet.java => AttributeMatcherGroup.java} | 12 +++--- .../assertions/DataPointAttributes.java | 16 ++++---- .../jmxscraper/assertions/MetricAssert.java | 28 +++++++------- .../ActiveMqIntegrationTest.java | 18 ++++----- .../CassandraIntegrationTest.java | 14 +++---- .../target_systems/HBaseIntegrationTest.java | 38 +++++++++---------- .../target_systems/JettyIntegrationTest.java | 8 ++-- 8 files changed, 67 insertions(+), 68 deletions(-) rename jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/{AttributeMatcherSet.java => AttributeMatcherGroup.java} (82%) diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java index 04aaff092..491756311 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java @@ -5,7 +5,6 @@ package io.opentelemetry.contrib.jmxscraper.assertions; -import java.util.Objects; import javax.annotation.Nullable; /** Implements functionality of matching data point attributes. */ diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherGroup.java similarity index 82% rename from jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java rename to jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherGroup.java index 9fab0c56e..df87d739d 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherSet.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherGroup.java @@ -9,8 +9,8 @@ import java.util.Map; import java.util.stream.Collectors; -/** Holder class for a set of attribute matchers */ -public class AttributeMatcherSet { +/** Group of attribute matchers */ +public class AttributeMatcherGroup { // stored as a Map for easy lookup by name private final Map matchers; @@ -18,19 +18,19 @@ public class AttributeMatcherSet { /** * Constructor for a set of attribute matchers * - * @param matchers collection of matchers to build a set from + * @param matchers collection of matchers to build a group from * @throws IllegalStateException if there is any duplicate key */ - AttributeMatcherSet(Collection matchers) { + AttributeMatcherGroup(Collection matchers) { this.matchers = matchers.stream().collect(Collectors.toMap(AttributeMatcher::getAttributeName, m -> m)); } /** - * Checks if attributes match this attribute matcher set + * Checks if attributes match this attribute matcher group * * @param attributes attributes to check as map - * @return {@literal true} when the attributes match all attributes from this set + * @return {@literal true} when the attributes match all attributes from this group */ public boolean matches(Map attributes) { if (attributes.size() != matchers.size()) { diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java index 2feb76b47..4545c4f34 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java @@ -38,16 +38,16 @@ public static AttributeMatcher attributeWithAnyValue(String name) { } /** - * Create a set of attribute matchers that should be used to verify set of data point attributes. + * Creates a group of attribute matchers that should be used to verify data point attributes. * - * @param attributes list of matchers to create set. It must contain matchers with unique names. - * @return set of unique attribute matchers + * @param attributes list of matchers to create group. It must contain matchers with unique names. + * @return group of attribute matchers * @throws IllegalArgumentException if provided list contains two or more matchers with the same - * name. - * @see MetricAssert#hasDataPointsWithAttributes(AttributeMatcherSet...) for detailed description - * off the algorithm used for matching + * attribute name + * @see MetricAssert#hasDataPointsWithAttributes(AttributeMatcherGroup...) for detailed + * description off the algorithm used for matching */ - public static AttributeMatcherSet attributeSet(AttributeMatcher... attributes) { - return new AttributeMatcherSet(Arrays.asList(attributes)); + public static AttributeMatcherGroup attributeGroup(AttributeMatcher... attributes) { + return new AttributeMatcherGroup(Arrays.asList(attributes)); } } 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 6c712c671..1a4eaace3 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 @@ -5,7 +5,7 @@ package io.opentelemetry.contrib.jmxscraper.assertions; -import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeSet; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup; import com.google.errorprone.annotations.CanIgnoreReturnValue; import io.opentelemetry.proto.common.v1.KeyValue; @@ -241,26 +241,26 @@ private void dataPointsCommonCheck(List dataPoints) { */ @CanIgnoreReturnValue public final MetricAssert hasDataPointsWithOneAttribute(AttributeMatcher expectedAttribute) { - return hasDataPointsWithAttributes(attributeSet(expectedAttribute)); + return hasDataPointsWithAttributes(attributeGroup(expectedAttribute)); } /** - * Verifies that every data point attributes set is matched exactly by one of the matcher sets - * provided. Also, each matcher set must match at least one data point attributes set. Data point - * attributes set is matched by matcher set if each attribute is matched by one matcher and each - * matcher matches one attribute. In other words: number of attributes is the same as number of - * matchers and there is 1:1 matching between them. + * Verifies that every data point attributes is matched exactly by one of the matcher groups + * provided. Also, each matcher group must match at least one data point attributes set. Data + * point attributes are matched by matcher group if each attribute is matched by one matcher and + * each matcher matches one attribute. In other words: number of attributes is the same as number + * of matchers and there is 1:1 matching between them. * - * @param attributeMatchers array of attribute matcher sets + * @param matcherGroups array of attribute matcher groups * @return this */ @CanIgnoreReturnValue - public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... attributeMatchers) { + public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherGroup... matcherGroups) { return checkDataPoints( dataPoints -> { dataPointsCommonCheck(dataPoints); - boolean[] matchedSets = new boolean[attributeMatchers.length]; + boolean[] matchedSets = new boolean[matcherGroups.length]; // validate each datapoint attributes match exactly one of the provided attributes sets for (NumberDataPoint dataPoint : dataPoints) { @@ -269,8 +269,8 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att .collect( Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue())); int matchCount = 0; - for (int i = 0; i < attributeMatchers.length; i++) { - if (attributeMatchers[i].matches(dataPointAttributes)) { + for (int i = 0; i < matcherGroups.length; i++) { + if (matcherGroups[i].matches(dataPointAttributes)) { matchedSets[i] = true; matchCount++; } @@ -278,7 +278,7 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att info.description( "data point attributes '%s' for metric '%s' must match exactly one of the attribute sets '%s'", - dataPointAttributes, actual.getName(), Arrays.asList(attributeMatchers)); + dataPointAttributes, actual.getName(), Arrays.asList(matcherGroups)); integers.assertEqual(info, matchCount, 1); } @@ -286,7 +286,7 @@ public final MetricAssert hasDataPointsWithAttributes(AttributeMatcherSet... att for (int i = 0; i < matchedSets.length; i++) { info.description( "no data point matched attribute set '%s' for metric '%s'", - attributeMatchers[i], actual.getName()); + matcherGroups[i], actual.getName()); objects.assertEqual(info, matchedSets[i], true); } }); diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/ActiveMqIntegrationTest.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/ActiveMqIntegrationTest.java index dc35d4af5..4f909fdc5 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/ActiveMqIntegrationTest.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/ActiveMqIntegrationTest.java @@ -6,7 +6,7 @@ package io.opentelemetry.contrib.jmxscraper.target_systems; import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute; -import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeSet; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup; import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer; import java.nio.file.Path; @@ -48,7 +48,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("{consumer}") .isUpDownCounter() .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("destination", "ActiveMQ.Advisory.MasterBroker"), attribute("broker", "localhost")))) .add( @@ -59,7 +59,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("{producer}") .isUpDownCounter() .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("destination", "ActiveMQ.Advisory.MasterBroker"), attribute("broker", "localhost")))) .add( @@ -78,7 +78,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("%") .isGauge() .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("destination", "ActiveMQ.Advisory.MasterBroker"), attribute("broker", "localhost")))) .add( @@ -107,7 +107,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("{message}") .isUpDownCounter() .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("destination", "ActiveMQ.Advisory.MasterBroker"), attribute("broker", "localhost")))) .add( @@ -119,7 +119,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("{message}") .isCounter() .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("destination", "ActiveMQ.Advisory.MasterBroker"), attribute("broker", "localhost")))) .add( @@ -130,7 +130,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("{message}") .isCounter() .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("destination", "ActiveMQ.Advisory.MasterBroker"), attribute("broker", "localhost")))) .add( @@ -141,7 +141,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("{message}") .isCounter() .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("destination", "ActiveMQ.Advisory.MasterBroker"), attribute("broker", "localhost")))) .add( @@ -152,7 +152,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("ms") .isGauge() .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("destination", "ActiveMQ.Advisory.MasterBroker"), attribute("broker", "localhost")))); } diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/CassandraIntegrationTest.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/CassandraIntegrationTest.java index a9a55ad9d..7730f34a7 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/CassandraIntegrationTest.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/CassandraIntegrationTest.java @@ -6,10 +6,10 @@ package io.opentelemetry.contrib.jmxscraper.target_systems; import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute; -import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeSet; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup; import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer; -import io.opentelemetry.contrib.jmxscraper.assertions.AttributeMatcherSet; +import io.opentelemetry.contrib.jmxscraper.assertions.AttributeMatcherGroup; import java.nio.file.Path; import java.time.Duration; import org.testcontainers.containers.GenericContainer; @@ -161,9 +161,9 @@ protected MetricsVerifier createMetricsVerifier() { .hasUnit("1") .isCounter() .hasDataPointsWithAttributes( - attributeSet(attribute("operation", "RangeSlice")), - attributeSet(attribute("operation", "Read")), - attributeSet(attribute("operation", "Write")))) + attributeGroup(attribute("operation", "RangeSlice")), + attributeGroup(attribute("operation", "Read")), + attributeGroup(attribute("operation", "Write")))) .add( "cassandra.client.request.error.count", metric -> @@ -183,7 +183,7 @@ protected MetricsVerifier createMetricsVerifier() { errorCountAttributes("Write", "Unavailable"))); } - private static AttributeMatcherSet errorCountAttributes(String operation, String status) { - return attributeSet(attribute("operation", operation), attribute("status", status)); + private static AttributeMatcherGroup errorCountAttributes(String operation, String status) { + return attributeGroup(attribute("operation", operation), attribute("status", status)); } } 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 bece18c33..7aee15654 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 @@ -6,7 +6,7 @@ package io.opentelemetry.contrib.jmxscraper.target_systems; import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute; -import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeSet; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup; import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeWithAnyValue; import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer; @@ -44,8 +44,8 @@ protected MetricsVerifier createMetricsVerifier() { .hasDescription("The number of region servers.") .hasUnit("{server}") .hasDataPointsWithAttributes( - attributeSet(attribute("state", "dead")), - attributeSet(attribute("state", "live")))) + attributeGroup(attribute("state", "dead")), + attributeGroup(attribute("state", "live")))) .add( "hbase.master.regions_in_transition.count", metric -> @@ -128,9 +128,9 @@ protected MetricsVerifier createMetricsVerifier() { .hasDescription("The number of requests received.") .hasUnit("{request}") .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("state", "write"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("state", "read"), attributeWithAnyValue("region_server")))) .add( "hbase.region_server.queue.length", @@ -140,9 +140,9 @@ protected MetricsVerifier createMetricsVerifier() { .hasDescription("The number of RPC handlers actively servicing requests.") .hasUnit("{handler}") .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("state", "flush"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("state", "compaction"), attributeWithAnyValue("region_server")))) .add( @@ -162,9 +162,9 @@ protected MetricsVerifier createMetricsVerifier() { .hasDescription("Number of block cache hits/misses.") .hasUnit("{operation}") .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("state", "miss"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("state", "hit"), attributeWithAnyValue("region_server")))) .add( "hbase.region_server.files.local", @@ -436,17 +436,17 @@ protected MetricsVerifier createMetricsVerifier() { .hasDescription("Number of operations that took over 1000ms to complete.") .hasUnit("{operation}") .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("operation", "delete"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("operation", "append"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("operation", "get"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("operation", "put"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("operation", "increment"), attributeWithAnyValue("region_server")))) .add( @@ -473,12 +473,12 @@ protected MetricsVerifier createMetricsVerifier() { .hasDescription("The number of currently enqueued requests.") .hasUnit("{request}") .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("state", "replication"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("state", "user"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("state", "priority"), attributeWithAnyValue("region_server")))) .add( @@ -490,10 +490,10 @@ protected MetricsVerifier createMetricsVerifier() { "Number of client connection authentication failures/successes.") .hasUnit("{authentication request}") .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attribute("state", "successes"), attributeWithAnyValue("region_server")), - attributeSet( + attributeGroup( attribute("state", "failures"), attributeWithAnyValue("region_server")))) .add( diff --git a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JettyIntegrationTest.java b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JettyIntegrationTest.java index ed44316b9..86097cbaa 100644 --- a/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JettyIntegrationTest.java +++ b/jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JettyIntegrationTest.java @@ -6,7 +6,7 @@ package io.opentelemetry.contrib.jmxscraper.target_systems; import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute; -import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeSet; +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup; import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeWithAnyValue; import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer; @@ -88,7 +88,7 @@ protected MetricsVerifier createMetricsVerifier() { .hasDescription("The number of select calls.") .hasUnit("{operation}") .hasDataPointsWithAttributes( - attributeSet( + attributeGroup( attributeWithAnyValue("context"), attributeWithAnyValue("id")))) .add( "jetty.thread.count", @@ -98,8 +98,8 @@ protected MetricsVerifier createMetricsVerifier() { .hasDescription("The current number of threads.") .hasUnit("{thread}") .hasDataPointsWithAttributes( - attributeSet(attribute("state", "busy")), - attributeSet(attribute("state", "idle")))) + attributeGroup(attribute("state", "busy")), + attributeGroup(attribute("state", "idle")))) .add( "jetty.thread.queue.count", metric ->