Skip to content

Commit 657788c

Browse files
committed
cleanup
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 9fda943 commit 657788c

File tree

1 file changed

+98
-91
lines changed
  • prometheus-metrics-instrumentation-dropwizard/src/test/java/io/prometheus/metrics/instrumentation/dropwizard

1 file changed

+98
-91
lines changed

prometheus-metrics-instrumentation-dropwizard/src/test/java/io/prometheus/metrics/instrumentation/dropwizard/DropwizardExportsTest.java

Lines changed: 98 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class DropwizardExportsTest {
1919

20-
private PrometheusRegistry registry = new PrometheusRegistry();
20+
private final PrometheusRegistry registry = new PrometheusRegistry();
2121
private MetricRegistry metricRegistry;
2222

2323
@BeforeEach
@@ -39,20 +39,20 @@ public void testBuilderThrowsErrorOnNullRegistry() {
3939
@Test
4040
public void testBuilderCreatesOkay() {
4141
assertThatCode(
42-
() -> {
43-
DropwizardExports.builder().dropwizardRegistry(metricRegistry).register(registry);
44-
})
42+
() -> DropwizardExports.builder().dropwizardRegistry(metricRegistry).register(registry))
4543
.doesNotThrowAnyException();
4644
}
4745

4846
@Test
4947
public void testCounter() {
5048
metricRegistry.counter("foo.bar").inc(1);
5149
String expected =
52-
"# TYPE foo_bar counter\n"
53-
+ "# HELP foo_bar Generated from Dropwizard metric import (metric=foo.bar, type=com.codahale.metrics.Counter)\n"
54-
+ "foo_bar_total 1.0\n"
55-
+ "# EOF\n";
50+
"""
51+
# TYPE foo_bar counter
52+
# HELP foo_bar Generated from Dropwizard metric import (metric=foo.bar, type=com.codahale.metrics.Counter)
53+
foo_bar_total 1.0
54+
# EOF
55+
""";
5656

5757
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
5858
}
@@ -103,22 +103,24 @@ public Boolean getValue() {
103103
metricRegistry.register("boolean.gauge", booleanGauge);
104104

105105
String expected =
106-
"# TYPE boolean_gauge gauge\n"
107-
+ "# HELP boolean_gauge Generated from Dropwizard metric import (metric=boolean.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$5)\n"
108-
+ "boolean_gauge 1.0\n"
109-
+ "# TYPE double_gauge gauge\n"
110-
+ "# HELP double_gauge Generated from Dropwizard metric import (metric=double.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$2)\n"
111-
+ "double_gauge 1.234\n"
112-
+ "# TYPE float_gauge gauge\n"
113-
+ "# HELP float_gauge Generated from Dropwizard metric import (metric=float.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$4)\n"
114-
+ "float_gauge 0.1234000027179718\n"
115-
+ "# TYPE integer_gauge gauge\n"
116-
+ "# HELP integer_gauge Generated from Dropwizard metric import (metric=integer.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$1)\n"
117-
+ "integer_gauge 1234.0\n"
118-
+ "# TYPE long_gauge gauge\n"
119-
+ "# HELP long_gauge Generated from Dropwizard metric import (metric=long.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$3)\n"
120-
+ "long_gauge 1234.0\n"
121-
+ "# EOF\n";
106+
"""
107+
# TYPE boolean_gauge gauge
108+
# HELP boolean_gauge Generated from Dropwizard metric import (metric=boolean.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$5)
109+
boolean_gauge 1.0
110+
# TYPE double_gauge gauge
111+
# HELP double_gauge Generated from Dropwizard metric import (metric=double.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$2)
112+
double_gauge 1.234
113+
# TYPE float_gauge gauge
114+
# HELP float_gauge Generated from Dropwizard metric import (metric=float.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$4)
115+
float_gauge 0.1234000027179718
116+
# TYPE integer_gauge gauge
117+
# HELP integer_gauge Generated from Dropwizard metric import (metric=integer.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$1)
118+
integer_gauge 1234.0
119+
# TYPE long_gauge gauge
120+
# HELP long_gauge Generated from Dropwizard metric import (metric=long.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$3)
121+
long_gauge 1234.0
122+
# EOF
123+
""";
122124

123125
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
124126
}
@@ -157,30 +159,34 @@ public void testHistogram() {
157159

158160
// The result should look like this
159161
String expected1 =
160-
"# TYPE hist summary\n"
161-
+ "# HELP hist Generated from Dropwizard metric import (metric=hist, type=com.codahale.metrics.Histogram)\n"
162-
+ "hist{quantile=\"0.5\"} 49.0\n"
163-
+ "hist{quantile=\"0.75\"} 74.0\n"
164-
+ "hist{quantile=\"0.95\"} 94.0\n"
165-
+ "hist{quantile=\"0.98\"} 97.0\n"
166-
+ "hist{quantile=\"0.99\"} 98.0\n"
167-
+ "hist{quantile=\"0.999\"} 99.0\n"
168-
+ "hist_count 100\n"
169-
+ "# EOF\n";
162+
"""
163+
# TYPE hist summary
164+
# HELP hist Generated from Dropwizard metric import (metric=hist, type=com.codahale.metrics.Histogram)
165+
hist{quantile="0.5"} 49.0
166+
hist{quantile="0.75"} 74.0
167+
hist{quantile="0.95"} 94.0
168+
hist{quantile="0.98"} 97.0
169+
hist{quantile="0.99"} 98.0
170+
hist{quantile="0.999"} 99.0
171+
hist_count 100
172+
# EOF
173+
""";
170174

171175
// However, Dropwizard uses a random reservoir sampling algorithm, so the values could as well
172176
// be off-by-one
173177
String expected2 =
174-
"# TYPE hist summary\n"
175-
+ "# HELP hist Generated from Dropwizard metric import (metric=hist, type=com.codahale.metrics.Histogram)\n"
176-
+ "hist{quantile=\"0.5\"} 50.0\n"
177-
+ "hist{quantile=\"0.75\"} 75.0\n"
178-
+ "hist{quantile=\"0.95\"} 95.0\n"
179-
+ "hist{quantile=\"0.98\"} 98.0\n"
180-
+ "hist{quantile=\"0.99\"} 99.0\n"
181-
+ "hist{quantile=\"0.999\"} 99.0\n"
182-
+ "hist_count 100\n"
183-
+ "# EOF\n";
178+
"""
179+
# TYPE hist summary
180+
# HELP hist Generated from Dropwizard metric import (metric=hist, type=com.codahale.metrics.Histogram)
181+
hist{quantile="0.5"} 50.0
182+
hist{quantile="0.75"} 75.0
183+
hist{quantile="0.95"} 95.0
184+
hist{quantile="0.98"} 98.0
185+
hist{quantile="0.99"} 99.0
186+
hist{quantile="0.999"} 99.0
187+
hist_count 100
188+
# EOF
189+
""";
184190

185191
// The following asserts the values matches either of the expected value.
186192
String textFormat = convertToOpenMetricsFormat(pmRegistry);
@@ -197,10 +203,12 @@ public void testMeter() {
197203
meter.mark();
198204

199205
String expected =
200-
"# TYPE meter counter\n"
201-
+ "# HELP meter Generated from Dropwizard metric import (metric=meter_total, type=com.codahale.metrics.Meter)\n"
202-
+ "meter_total 2.0\n"
203-
+ "# EOF\n";
206+
"""
207+
# TYPE meter counter
208+
# HELP meter Generated from Dropwizard metric import (metric=meter_total, type=com.codahale.metrics.Meter)
209+
meter_total 2.0
210+
# EOF
211+
""";
204212
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
205213
}
206214

@@ -213,61 +221,60 @@ public void testTimer() throws InterruptedException {
213221
Thread.sleep(100L);
214222
long timeSpentNanos = time.stop();
215223
double timeSpentMillis = TimeUnit.NANOSECONDS.toMillis(timeSpentNanos);
216-
System.out.println(timeSpentMillis);
217224

218-
SummarySnapshot.SummaryDataPointSnapshot dataPointSnapshot =
219-
(SummarySnapshot.SummaryDataPointSnapshot)
220-
exports.collect().stream().flatMap(i -> i.getDataPoints().stream()).findFirst().get();
221-
// We slept for 1Ms so we ensure that all timers are above 1ms:
222-
assertThat(dataPointSnapshot.getQuantiles().size()).isGreaterThan(1);
223-
dataPointSnapshot
224-
.getQuantiles()
225-
.forEach(
226-
i -> {
227-
System.out.println(i.getQuantile() + " : " + i.getValue());
228-
assertThat(i.getValue()).isGreaterThan(timeSpentMillis / 1000d);
225+
assertThat(exports.collect().stream().flatMap(i1 -> i1.getDataPoints().stream()).findFirst())
226+
.containsInstanceOf(SummarySnapshot.SummaryDataPointSnapshot.class)
227+
.hasValueSatisfying(
228+
snapshot -> {
229+
var dataPointSnapshot = (SummarySnapshot.SummaryDataPointSnapshot) snapshot;
230+
// We slept for 1Ms so we ensure that all timers are above 1ms:
231+
assertThat(dataPointSnapshot.getQuantiles().size()).isGreaterThan(1);
232+
dataPointSnapshot
233+
.getQuantiles()
234+
.forEach(i -> assertThat(i.getValue()).isGreaterThan(timeSpentMillis / 1000d));
235+
assertThat(dataPointSnapshot.getCount()).isOne();
229236
});
230-
assertThat(dataPointSnapshot.getCount()).isOne();
231237
}
232238

233239
@Test
234240
public void testThatMetricHelpUsesOriginalDropwizardName() {
235-
236241
metricRegistry.timer("my.application.namedTimer1");
237242
metricRegistry.counter("my.application.namedCounter1");
238243
metricRegistry.meter("my.application.namedMeter1");
239244
metricRegistry.histogram("my.application.namedHistogram1");
240245
metricRegistry.register("my.application.namedGauge1", new ExampleDoubleGauge());
241246

242247
String expected =
243-
"# TYPE my_application_namedCounter1 counter\n"
244-
+ "# HELP my_application_namedCounter1 Generated from Dropwizard metric import (metric=my.application.namedCounter1, type=com.codahale.metrics.Counter)\n"
245-
+ "my_application_namedCounter1_total 0.0\n"
246-
+ "# TYPE my_application_namedGauge1 gauge\n"
247-
+ "# HELP my_application_namedGauge1 Generated from Dropwizard metric import (metric=my.application.namedGauge1, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$ExampleDoubleGauge)\n"
248-
+ "my_application_namedGauge1 0.0\n"
249-
+ "# TYPE my_application_namedHistogram1 summary\n"
250-
+ "# HELP my_application_namedHistogram1 Generated from Dropwizard metric import (metric=my.application.namedHistogram1, type=com.codahale.metrics.Histogram)\n"
251-
+ "my_application_namedHistogram1{quantile=\"0.5\"} 0.0\n"
252-
+ "my_application_namedHistogram1{quantile=\"0.75\"} 0.0\n"
253-
+ "my_application_namedHistogram1{quantile=\"0.95\"} 0.0\n"
254-
+ "my_application_namedHistogram1{quantile=\"0.98\"} 0.0\n"
255-
+ "my_application_namedHistogram1{quantile=\"0.99\"} 0.0\n"
256-
+ "my_application_namedHistogram1{quantile=\"0.999\"} 0.0\n"
257-
+ "my_application_namedHistogram1_count 0\n"
258-
+ "# TYPE my_application_namedMeter1 counter\n"
259-
+ "# HELP my_application_namedMeter1 Generated from Dropwizard metric import (metric=my.application.namedMeter1_total, type=com.codahale.metrics.Meter)\n"
260-
+ "my_application_namedMeter1_total 0.0\n"
261-
+ "# TYPE my_application_namedTimer1 summary\n"
262-
+ "# HELP my_application_namedTimer1 Generated from Dropwizard metric import (metric=my.application.namedTimer1, type=com.codahale.metrics.Timer)\n"
263-
+ "my_application_namedTimer1{quantile=\"0.5\"} 0.0\n"
264-
+ "my_application_namedTimer1{quantile=\"0.75\"} 0.0\n"
265-
+ "my_application_namedTimer1{quantile=\"0.95\"} 0.0\n"
266-
+ "my_application_namedTimer1{quantile=\"0.98\"} 0.0\n"
267-
+ "my_application_namedTimer1{quantile=\"0.99\"} 0.0\n"
268-
+ "my_application_namedTimer1{quantile=\"0.999\"} 0.0\n"
269-
+ "my_application_namedTimer1_count 0\n"
270-
+ "# EOF\n";
248+
"""
249+
# TYPE my_application_namedCounter1 counter
250+
# HELP my_application_namedCounter1 Generated from Dropwizard metric import (metric=my.application.namedCounter1, type=com.codahale.metrics.Counter)
251+
my_application_namedCounter1_total 0.0
252+
# TYPE my_application_namedGauge1 gauge
253+
# HELP my_application_namedGauge1 Generated from Dropwizard metric import (metric=my.application.namedGauge1, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$ExampleDoubleGauge)
254+
my_application_namedGauge1 0.0
255+
# TYPE my_application_namedHistogram1 summary
256+
# HELP my_application_namedHistogram1 Generated from Dropwizard metric import (metric=my.application.namedHistogram1, type=com.codahale.metrics.Histogram)
257+
my_application_namedHistogram1{quantile="0.5"} 0.0
258+
my_application_namedHistogram1{quantile="0.75"} 0.0
259+
my_application_namedHistogram1{quantile="0.95"} 0.0
260+
my_application_namedHistogram1{quantile="0.98"} 0.0
261+
my_application_namedHistogram1{quantile="0.99"} 0.0
262+
my_application_namedHistogram1{quantile="0.999"} 0.0
263+
my_application_namedHistogram1_count 0
264+
# TYPE my_application_namedMeter1 counter
265+
# HELP my_application_namedMeter1 Generated from Dropwizard metric import (metric=my.application.namedMeter1_total, type=com.codahale.metrics.Meter)
266+
my_application_namedMeter1_total 0.0
267+
# TYPE my_application_namedTimer1 summary
268+
# HELP my_application_namedTimer1 Generated from Dropwizard metric import (metric=my.application.namedTimer1, type=com.codahale.metrics.Timer)
269+
my_application_namedTimer1{quantile="0.5"} 0.0
270+
my_application_namedTimer1{quantile="0.75"} 0.0
271+
my_application_namedTimer1{quantile="0.95"} 0.0
272+
my_application_namedTimer1{quantile="0.98"} 0.0
273+
my_application_namedTimer1{quantile="0.99"} 0.0
274+
my_application_namedTimer1{quantile="0.999"} 0.0
275+
my_application_namedTimer1_count 0
276+
# EOF
277+
""";
271278
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
272279
}
273280

@@ -283,7 +290,7 @@ private String convertToOpenMetricsFormat(PrometheusRegistry _registry) {
283290
OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(true, true);
284291
try {
285292
writer.write(out, _registry.scrape());
286-
return out.toString(StandardCharsets.UTF_8.name());
293+
return out.toString(StandardCharsets.UTF_8);
287294
} catch (IOException e) {
288295
throw new RuntimeException(e);
289296
}

0 commit comments

Comments
 (0)