Skip to content

Commit 8062a96

Browse files
committed
Added some JavaDocs.
Minor refactorings, mostly renaming. Fixed typos.
1 parent 0e5fd2c commit 8062a96

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,22 @@ public void setStrict(boolean strict) {
4747
}
4848

4949
public void strictCheck() {
50-
strictCheck("description", /* expectedValue= */ true, descriptionChecked);
51-
strictCheck("unit", /* expectedValue= */ true, unitChecked);
52-
strictCheck("type", /* expectedValue= */ true, typeChecked);
53-
strictCheck("data point attributes", /* expectedValue= */ true, dataPointAttributesChecked);
50+
strictCheck("description", /* expectedCheckStatus= */ true, descriptionChecked);
51+
strictCheck("unit", /* expectedCheckStatus= */ true, unitChecked);
52+
strictCheck("type", /* expectedCheckStatus= */ true, typeChecked);
53+
strictCheck(
54+
"data point attributes", /* expectedCheckStatus= */ true, dataPointAttributesChecked);
5455
}
5556

56-
private void strictCheck(String attribute, boolean expectedValue, boolean value) {
57+
private void strictCheck(
58+
String metricProperty, boolean expectedCheckStatus, boolean actualCheckStatus) {
5759
if (!strict) {
5860
return;
5961
}
60-
String failMsgPrefix = expectedValue ? "duplicate" : "missing";
62+
String failMsgPrefix = expectedCheckStatus ? "duplicate" : "missing";
6163
info.description(
62-
"%s assertion on %s for metric '%s'", failMsgPrefix, attribute, actual.getName());
63-
objects.assertEqual(info, value, expectedValue);
64+
"%s assertion on %s for metric '%s'", failMsgPrefix, metricProperty, actual.getName());
65+
objects.assertEqual(info, actualCheckStatus, expectedCheckStatus);
6466
}
6567

6668
/**
@@ -75,7 +77,7 @@ public MetricAssert hasDescription(String description) {
7577

7678
info.description("unexpected description for metric '%s'", actual.getName());
7779
objects.assertEqual(info, actual.getDescription(), description);
78-
strictCheck("description", /* expectedValue= */ false, descriptionChecked);
80+
strictCheck("description", /* expectedCheckStatus= */ false, descriptionChecked);
7981
descriptionChecked = true;
8082
return this;
8183
}
@@ -92,13 +94,13 @@ public MetricAssert hasUnit(String unit) {
9294

9395
info.description("unexpected unit for metric '%s'", actual.getName());
9496
objects.assertEqual(info, actual.getUnit(), unit);
95-
strictCheck("unit", /* expectedValue= */ false, unitChecked);
97+
strictCheck("unit", /* expectedCheckStatus= */ false, unitChecked);
9698
unitChecked = true;
9799
return this;
98100
}
99101

100102
/**
101-
* Verifies the metric to be a gauge
103+
* Verifies the metric is a gauge
102104
*
103105
* @return this
104106
*/
@@ -108,7 +110,7 @@ public MetricAssert isGauge() {
108110

109111
info.description("gauge expected for metric '%s'", actual.getName());
110112
objects.assertEqual(info, actual.hasGauge(), true);
111-
strictCheck("type", /* expectedValue= */ false, typeChecked);
113+
strictCheck("type", /* expectedCheckStatus= */ false, typeChecked);
112114
typeChecked = true;
113115
return this;
114116
}
@@ -135,16 +137,21 @@ private MetricAssert hasSum(boolean monotonic) {
135137
public MetricAssert isCounter() {
136138
// counters have a monotonic sum as their value can't decrease
137139
hasSum(true);
138-
strictCheck("type", /* expectedValue= */ false, typeChecked);
140+
strictCheck("type", /* expectedCheckStatus= */ false, typeChecked);
139141
typeChecked = true;
140142
return this;
141143
}
142144

145+
/**
146+
* Verifies the metric is an up-down counter
147+
*
148+
* @return this
149+
*/
143150
@CanIgnoreReturnValue
144151
public MetricAssert isUpDownCounter() {
145152
// up down counters are non-monotonic as their value can increase & decrease
146153
hasSum(false);
147-
strictCheck("type", /* expectedValue= */ false, typeChecked);
154+
strictCheck("type", /* expectedCheckStatus= */ false, typeChecked);
148155
typeChecked = true;
149156
return this;
150157
}
@@ -169,7 +176,7 @@ public MetricAssert hasDataPointsWithoutAttributes() {
169176
@CanIgnoreReturnValue
170177
private MetricAssert checkDataPoints(Consumer<List<NumberDataPoint>> listConsumer) {
171178
// in practice usually one set of data points is provided but the
172-
// protobuf does not enforce that so we have to ensure checking at least one
179+
// protobuf does not enforce that, so we have to ensure checking at least one
173180
int count = 0;
174181
if (actual.hasGauge()) {
175182
count++;
@@ -182,7 +189,7 @@ private MetricAssert checkDataPoints(Consumer<List<NumberDataPoint>> listConsume
182189
info.description("at least one set of data points expected for metric '%s'", actual.getName());
183190
integers.assertGreaterThan(info, count, 0);
184191

185-
strictCheck("data point attributes", /* expectedValue= */ false, dataPointAttributesChecked);
192+
strictCheck("data point attributes", /* expectedCheckStatus= */ false, dataPointAttributesChecked);
186193
dataPointAttributesChecked = true;
187194
return this;
188195
}
@@ -295,7 +302,7 @@ public final MetricAssert hasDataPointsAttributes(Map<String, String>... attribu
295302
}
296303

297304
/**
298-
* map equality utility
305+
* Map equality utility
299306
*
300307
* @param m1 first map
301308
* @param m2 second map

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ static void assertSum(
4141
assertThat(metric.hasSum()).isTrue();
4242
assertThat(metric.getSum().getIsMonotonic()).isEqualTo(isMonotonic);
4343

44-
assertThat(metric)
45-
.hasDescription(description)
46-
.hasUnit(unit)
47-
.hasDataPointsWithoutAttributes();
44+
assertThat(metric).hasDescription(description).hasUnit(unit).hasDataPointsWithoutAttributes();
4845
}
4946

5047
@SafeVarargs
@@ -86,9 +83,7 @@ static void assertSumWithAttributesMultiplePoints(
8683
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
8784

8885
assertThat(metric.getName()).isEqualTo(name);
89-
assertThat(metric)
90-
.hasDescription(description)
91-
.hasUnit(unit);
86+
assertThat(metric).hasDescription(description).hasUnit(unit);
9287

9388
assertThat(metric.hasSum()).isTrue();
9489
assertThat(metric.getSum().getIsMonotonic()).isEqualTo(isMonotonic);
@@ -104,10 +99,7 @@ static void assertGaugeWithAttributes(
10499
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
105100
assertThat(metric.getName()).isEqualTo(name);
106101

107-
assertThat(metric)
108-
.hasDescription(description)
109-
.hasUnit(unit)
110-
.isGauge();
102+
assertThat(metric).hasDescription(description).hasUnit(unit).isGauge();
111103

112104
assertAttributedPoints(metric.getGauge().getDataPointsList(), attributeGroupAssertions);
113105
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private MetricsVerifier() {}
2929
/**
3030
* Create instance of MetricsVerifier configured to fail verification if any metric was not
3131
* verified because there is no assertion defined for it. This behavior can be changed by calling
32-
* allowingExtraMetrics() method.
32+
* {@link #disableStrictMode()} method.
3333
*
3434
* @return new instance of MetricsVerifier
3535
* @see #disableStrictMode()
@@ -38,12 +38,29 @@ public static MetricsVerifier create() {
3838
return new MetricsVerifier();
3939
}
4040

41+
/**
42+
* Disable strict checks of metric assertions. It means that all metrics checks added after
43+
* calling this method will not enforce asserting all metric properties and will not detect
44+
* duplicate property assertions. Also, there will be no error reported if any of metrics was
45+
* skipped because no assertion was added for it.
46+
*
47+
* @return this
48+
* @see #verify(List)
49+
* @see #add(String, Consumer)
50+
*/
4151
@CanIgnoreReturnValue
4252
public MetricsVerifier disableStrictMode() {
4353
strictMode = false;
4454
return this;
4555
}
4656

57+
/**
58+
* Add assertion for given metric
59+
*
60+
* @param metricName name of metric to be verified by provided assertion
61+
* @param assertion an assertion to verify properties of the metric
62+
* @return this
63+
*/
4764
@CanIgnoreReturnValue
4865
public MetricsVerifier add(String metricName, Consumer<MetricAssert> assertion) {
4966
assertions.put(
@@ -57,6 +74,15 @@ public MetricsVerifier add(String metricName, Consumer<MetricAssert> assertion)
5774
return this;
5875
}
5976

77+
/**
78+
* Execute all defined assertions against provided list of metrics. Error is reported if any of
79+
* defined assertions failed. Error is also reported if any of expected metrics was not present in
80+
* the metrics list, unless strict mode is disabled with {@link #disableStrictMode()}.
81+
*
82+
* @param metrics list of metrics to be verified
83+
* @see #add(String, Consumer)
84+
* @see #disableStrictMode()
85+
*/
6086
public void verify(List<Metric> metrics) {
6187
verifyAllExpectedMetricsWereReceived(metrics);
6288

@@ -78,7 +104,6 @@ public void verify(List<Metric> metrics) {
78104
}
79105
}
80106

81-
@SuppressWarnings("SystemOut")
82107
private void verifyAllExpectedMetricsWereReceived(List<Metric> metrics) {
83108
Set<String> receivedMetricNames =
84109
metrics.stream().map(Metric::getName).collect(Collectors.toSet());

0 commit comments

Comments
 (0)