Skip to content

Commit b9f054c

Browse files
committed
strict check avoids duplicate assertions
1 parent a458c1c commit b9f054c

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class MetricAssert extends AbstractAssert<MetricAssert, Metric> {
3131
private static final Integers integers = Integers.instance();
3232
private static final Maps maps = Maps.instance();
3333

34+
private boolean strict;
35+
3436
private boolean descriptionChecked;
3537
private boolean unitChecked;
3638
private boolean typeChecked;
@@ -40,20 +42,26 @@ public class MetricAssert extends AbstractAssert<MetricAssert, Metric> {
4042
super(actual, MetricAssert.class);
4143
}
4244

43-
public void validateAssertions() {
44-
info.description("missing assertion on description for metric '%s'", actual.getName());
45-
objects.assertEqual(info, descriptionChecked, true);
46-
47-
info.description("missing assertion on unit for metric '%s'", actual.getName());
48-
objects.assertEqual(info, unitChecked, true);
49-
50-
info.description("missing assertion on type for metric '%s'", actual.getName());
51-
objects.assertEqual(info, typeChecked, true);
45+
public void setStrict(boolean strict) {
46+
this.strict = strict;
47+
}
5248

53-
info.description("missing assertion on data point attributes for metric '%s", actual.getName());
54-
objects.assertEqual(info, dataPointAttributesChecked, true);
49+
public void strictCheck() {
50+
strictCheck("description", true, descriptionChecked);
51+
strictCheck("unit", true, unitChecked);
52+
strictCheck("type", true, typeChecked);
53+
strictCheck("data point attributes", true, dataPointAttributesChecked);
5554
}
5655

56+
private void strictCheck(String attribute, boolean expectedValue, boolean value) {
57+
if(!strict) {
58+
return;
59+
}
60+
String failMsgPrefix = expectedValue ? "duplicate" : "missing";
61+
info.description("%s assertion on %s for metric '%s'", failMsgPrefix, attribute,
62+
actual.getName());
63+
objects.assertEqual(info, value, expectedValue);
64+
}
5765
/**
5866
* Verifies metric description
5967
*
@@ -66,6 +74,7 @@ public MetricAssert hasDescription(String description) {
6674

6775
info.description("unexpected description for metric '%s'", actual.getName());
6876
objects.assertEqual(info, actual.getDescription(), description);
77+
strictCheck("description", false, descriptionChecked);
6978
descriptionChecked = true;
7079
return this;
7180
}
@@ -82,6 +91,7 @@ public MetricAssert hasUnit(String unit) {
8291

8392
info.description("unexpected unit for metric '%s'", actual.getName());
8493
objects.assertEqual(info, actual.getUnit(), unit);
94+
strictCheck("unit", false, unitChecked);
8595
unitChecked = true;
8696
return this;
8797
}
@@ -97,6 +107,7 @@ public MetricAssert isGauge() {
97107

98108
info.description("gauge expected for metric '%s'", actual.getName());
99109
objects.assertEqual(info, actual.hasGauge(), true);
110+
strictCheck("type", false, typeChecked);
100111
typeChecked = true;
101112
return this;
102113
}
@@ -123,6 +134,7 @@ private MetricAssert hasSum(boolean monotonic) {
123134
public MetricAssert isCounter() {
124135
// counters have a monotonic sum as their value can't decrease
125136
hasSum(true);
137+
strictCheck("type", false, typeChecked);
126138
typeChecked = true;
127139
return this;
128140
}
@@ -131,6 +143,7 @@ public MetricAssert isCounter() {
131143
public MetricAssert isUpDownCounter() {
132144
// up down counters are non-monotonic as their value can increase & decrease
133145
hasSum(false);
146+
strictCheck("type", false, typeChecked);
134147
typeChecked = true;
135148
return this;
136149
}
@@ -168,6 +181,7 @@ private MetricAssert checkDataPoints(Consumer<List<NumberDataPoint>> listConsume
168181
info.description("at least one set of data points expected for metric '%s'", actual.getName());
169182
integers.assertGreaterThan(info, count, 0);
170183

184+
strictCheck("data point attributes", false, dataPointAttributesChecked);
171185
dataPointAttributesChecked = true;
172186
return this;
173187
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ public MetricsVerifier register(String metricName, Consumer<MetricAssert> assert
5050
metricName,
5151
metric -> {
5252
MetricAssert metricAssert = assertThat(metric);
53+
metricAssert.setStrict(strictMode);
5354
assertion.accept(metricAssert);
54-
if (strictMode) {
55-
metricAssert.validateAssertions();
56-
}
55+
metricAssert.strictCheck();
5756
});
5857
return this;
5958
}

0 commit comments

Comments
 (0)