Skip to content

Commit b26cf42

Browse files
committed
Coe review changes
1 parent 80994f2 commit b26cf42

File tree

5 files changed

+102
-85
lines changed

5 files changed

+102
-85
lines changed

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcher.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,41 @@
55

66
package io.opentelemetry.contrib.jmxscraper.assertions;
77

8-
import static io.opentelemetry.contrib.jmxscraper.assertions.AttributeValueMatcher.ANY_VALUE_MATCHER;
9-
108
import java.util.Objects;
119

1210
/** Implements functionality of matching data point attributes. */
1311
public class AttributeMatcher {
14-
private final String name;
15-
private final AttributeValueMatcher attributeValueMatcher;
12+
private final String attributeName;
13+
private final String attributeValue;
1614

1715
/**
18-
* Create instance used to match data point attribute with te same name and with any value.
16+
* Create instance used to match data point attribute with any value.
1917
*
20-
* @param name attribute name
18+
* @param attributeName matched attribute name
2119
*/
22-
AttributeMatcher(String name) {
23-
this.name = name;
24-
this.attributeValueMatcher = ANY_VALUE_MATCHER;
20+
AttributeMatcher(String attributeName) {
21+
this.attributeName = attributeName;
22+
this.attributeValue = null;
2523
}
2624

2725
/**
2826
* Create instance used to match data point attribute with te same name and with the same value.
2927
*
30-
* @param name attribute name
31-
* @param value attribute value
28+
* @param attributeName attribute name
29+
* @param attributeValue attribute value
3230
*/
33-
AttributeMatcher(String name, String value) {
34-
this.name = name;
35-
this.attributeValueMatcher = new AttributeValueMatcher(value);
31+
AttributeMatcher(String attributeName, String attributeValue) {
32+
this.attributeName = attributeName;
33+
this.attributeValue = attributeValue;
3634
}
3735

38-
public String getName() {
39-
return name;
36+
/**
37+
* Return name of data point attribute that this AttributeMatcher is supposed to match value with.
38+
*
39+
* @return name of validated attribute
40+
*/
41+
public String getAttributeName() {
42+
return attributeName;
4043
}
4144

4245
@Override
@@ -48,21 +51,33 @@ public boolean equals(Object o) {
4851
return false;
4952
}
5053
AttributeMatcher other = (AttributeMatcher) o;
51-
return Objects.equals(name, other.name)
52-
&& attributeValueMatcher.matchValue(other.attributeValueMatcher);
54+
return Objects.equals(attributeName, other.attributeName);
5355
}
5456

5557
@Override
5658
public int hashCode() {
5759
// Do not use value matcher here to support value wildcards
58-
return Objects.hash(name);
60+
return Objects.hash(attributeName);
5961
}
6062

6163
@Override
6264
public String toString() {
63-
return attributeValueMatcher.getValue() == null
64-
? '{' + name + '}'
65-
: '{' + name + '=' + attributeValueMatcher.getValue() + '}';
65+
return attributeValue == null
66+
? '{' + attributeName + '}'
67+
: '{' + attributeName + '=' + attributeValue + '}';
68+
}
69+
70+
/**
71+
* Verify if this matcher is matching provided attribute value. If this matcher holds null value
72+
* then it is matching any attribute value
73+
*
74+
* @param value a value to be matched
75+
* @return true if this matcher is matching provided value, false otherwise.
76+
*/
77+
boolean matchesValue(String value) {
78+
if ((attributeValue == null) || (value == null)) {
79+
return true;
80+
}
81+
return Objects.equals(attributeValue, value);
6682
}
67-
;
6883
}

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeValueMatcher.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/DataPointAttributes.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,45 @@
1616
public class DataPointAttributes {
1717
private DataPointAttributes() {}
1818

19+
/**
20+
* Create instance of matcher that will be used to check if data point attribute with given name
21+
* has provided value.
22+
*
23+
* @param name name of the data point attribute to check
24+
* @param value expected value of data point attribute
25+
* @return instance of matcher
26+
*/
1927
public static AttributeMatcher attribute(String name, String value) {
2028
return new AttributeMatcher(name, value);
2129
}
2230

31+
/**
32+
* Create instance of matcher that will be used to check if data point attribute with given name
33+
* exists. Any value of the attribute is considered as matching.
34+
*
35+
* @param name name of the data point attribute to check
36+
* @return instance of matcher
37+
*/
2338
public static AttributeMatcher attributeWithAnyValue(String name) {
2439
return new AttributeMatcher(name);
2540
}
2641

42+
/**
43+
* Create a set of attribute matchers that will be used to verify set of data point attributes.
44+
*
45+
* @param attributes list of matchers to create set. It must contain matchers with unique names.
46+
* @return set of unique attribute matchers
47+
* @throws IllegalArgumentException if provided list contains two or more matchers with the same
48+
* name.
49+
* @see MetricAssert#hasDataPointsWithAttributes(Set[]) for detailed description of the algorithm
50+
* of matching
51+
*/
2752
public static Set<AttributeMatcher> attributeSet(AttributeMatcher... attributes) {
28-
return Arrays.stream(attributes).collect(Collectors.toSet());
53+
Set<AttributeMatcher> matcherSet = Arrays.stream(attributes).collect(Collectors.toSet());
54+
if (matcherSet.size() < attributes.length) {
55+
throw new IllegalArgumentException(
56+
"Duplicated matchers found in " + Arrays.toString(attributes));
57+
}
58+
return matcherSet;
2959
}
3060
}

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/MetricAssert.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55

66
package io.opentelemetry.contrib.jmxscraper.assertions;
77

8-
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute;
8+
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeSet;
99

1010
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1111
import io.opentelemetry.proto.common.v1.KeyValue;
1212
import io.opentelemetry.proto.metrics.v1.Metric;
1313
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
1414
import java.util.Arrays;
1515
import java.util.Collection;
16-
import java.util.Collections;
1716
import java.util.HashSet;
1817
import java.util.List;
18+
import java.util.Map;
1919
import java.util.Set;
2020
import java.util.function.Consumer;
2121
import java.util.stream.Collectors;
@@ -234,64 +234,78 @@ private void dataPointsCommonCheck(List<NumberDataPoint> dataPoints) {
234234
}
235235

236236
/**
237-
* Verifies that all data points have the same expected one attribute
237+
* Verifies that all metric data points have the same expected one attribute
238238
*
239-
* @param expectedAttribute expected attribute
239+
* @param expectedAttribute attribute matcher to validate data points attributes
240240
* @return this
241241
*/
242242
@CanIgnoreReturnValue
243243
public final MetricAssert hasDataPointsWithOneAttribute(AttributeMatcher expectedAttribute) {
244-
return hasDataPointsWithAttributes(Collections.singleton(expectedAttribute));
244+
return hasDataPointsWithAttributes(attributeSet(expectedAttribute));
245245
}
246246

247247
/**
248-
* Verifies that every provided attribute matcher set matches attributes of at least one metric
249-
* data point. Attribute matcher set is considered matching data point attributes if each matcher
250-
* matches exactly one data point attribute
248+
* Verifies that every data point attributes set is matched by one of matcher sets provided. Data
249+
* point attributes set is matched by matcher set if each attribute is matched by one matcher and
250+
* matcher count is equal to attribute count.
251251
*
252-
* @param attributeSets array of attribute matcher sets
252+
* @param attributeMatchers array of attribute matcher sets
253253
* @return this
254254
*/
255255
@SafeVarargs
256256
@CanIgnoreReturnValue
257257
@SuppressWarnings("varargs") // required to avoid warning
258-
public final MetricAssert hasDataPointsWithAttributes(Set<AttributeMatcher>... attributeSets) {
258+
public final MetricAssert hasDataPointsWithAttributes(
259+
Set<AttributeMatcher>... attributeMatchers) {
259260
return checkDataPoints(
260261
dataPoints -> {
261262
dataPointsCommonCheck(dataPoints);
262263

263-
boolean[] matchedSets = new boolean[attributeSets.length];
264+
boolean[] matchedSets = new boolean[attributeMatchers.length];
264265

265266
// validate each datapoint attributes match exactly one of the provided attributes sets
266267
for (NumberDataPoint dataPoint : dataPoints) {
267-
Set<AttributeMatcher> dataPointAttributes = toSet(dataPoint.getAttributesList());
268+
Map<String, String> dataPointAttributes = toMap(dataPoint.getAttributesList());
268269
int matchCount = 0;
269-
for (int i = 0; i < attributeSets.length; i++) {
270-
if (attributeSets[i].equals(dataPointAttributes)) {
270+
for (int i = 0; i < attributeMatchers.length; i++) {
271+
if (matchAttributes(attributeMatchers[i], dataPointAttributes)) {
271272
matchedSets[i] = true;
272273
matchCount++;
273274
}
274275
}
275276

276277
info.description(
277278
"data point attributes '%s' for metric '%s' must match exactly one of the attribute sets '%s'",
278-
dataPointAttributes, actual.getName(), Arrays.asList(attributeSets));
279+
dataPointAttributes, actual.getName(), Arrays.asList(attributeMatchers));
279280
integers.assertEqual(info, matchCount, 1);
280281
}
281282

282283
// check that all attribute sets matched at least once
283284
for (int i = 0; i < matchedSets.length; i++) {
284285
info.description(
285286
"no data point matched attribute set '%s' for metric '%s'",
286-
attributeSets[i], actual.getName());
287+
attributeMatchers[i], actual.getName());
287288
objects.assertEqual(info, matchedSets[i], true);
288289
}
289290
});
290291
}
291292

292-
private static Set<AttributeMatcher> toSet(List<KeyValue> list) {
293+
private static boolean matchAttributes(
294+
Set<AttributeMatcher> attributeMatchers, Map<String, String> dataPointAttributes) {
295+
if (attributeMatchers.size() != dataPointAttributes.size()) {
296+
return false;
297+
}
298+
for (AttributeMatcher matcher : attributeMatchers) {
299+
String attributeValue = dataPointAttributes.get(matcher.getAttributeName());
300+
if (!matcher.matchesValue(attributeValue)) {
301+
return false;
302+
}
303+
}
304+
return true;
305+
}
306+
307+
private static Map<String, String> toMap(List<KeyValue> list) {
293308
return list.stream()
294-
.map(entry -> attribute(entry.getKey(), entry.getValue().getStringValue()))
295-
.collect(Collectors.toSet());
309+
.collect(Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue()));
296310
}
297311
}

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/HBaseIntegrationTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ protected JmxScraperContainer customizeScraperContainer(
3636
@Override
3737
protected MetricsVerifier createMetricsVerifier() {
3838
return MetricsVerifier.create()
39-
.disableStrictMode()
4039
.add(
4140
"hbase.master.region_server.count",
4241
metric ->

0 commit comments

Comments
 (0)