Skip to content

Commit 3c302e1

Browse files
committed
Update DropwizardExportsTest.java
Signed-off-by: Kinshuk Bairagi <[email protected]>
1 parent 24382f3 commit 3c302e1

File tree

1 file changed

+140
-150
lines changed
  • prometheus-metrics-instrumentation-dropwizard/src/test/java/io/prometheus/metrics/instrumentation/dropwizard

1 file changed

+140
-150
lines changed

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

Lines changed: 140 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.prometheus.metrics.instrumentation.dropwizard;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.data.Offset.offset;
35
import static org.junit.Assert.assertEquals;
46
import static org.junit.Assert.assertTrue;
57

@@ -13,70 +15,70 @@
1315
import java.io.IOException;
1416
import java.nio.charset.StandardCharsets;
1517
import java.util.concurrent.TimeUnit;
16-
import org.junit.Assert;
17-
import org.junit.Before;
18-
import org.junit.Test;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
1920

20-
public class DropwizardExportsTest {
21+
class DropwizardExportsTest {
2122

2223
private PrometheusRegistry registry = new PrometheusRegistry();
2324
private MetricRegistry metricRegistry;
2425

25-
@Before
26+
@BeforeEach
2627
public void setUp() {
2728
metricRegistry = new MetricRegistry();
28-
DropwizardExports.builder().dropwizardRegistry(metricRegistry).register(registry);
29+
io.prometheus.metrics.instrumentation.dropwizard.DropwizardExports.builder().dropwizardRegistry(metricRegistry).register(registry);
2930
}
3031

31-
@Test
32+
@org.junit.jupiter.api.Test
3233
public void testCounter() {
3334
metricRegistry.counter("foo.bar").inc(1);
3435
String expected =
35-
"# TYPE foo_bar counter\n"
36-
+ "# HELP foo_bar Generated from Dropwizard metric import (metric=foo.bar, type=com.codahale.metrics.Counter)\n"
37-
+ "foo_bar_total 1.0\n"
38-
+ "# EOF\n";
36+
"# TYPE foo_bar counter\n"
37+
+ "# HELP foo_bar Generated from Dropwizard metric import (metric=foo.bar, type=com.codahale.metrics.Counter)\n"
38+
+ "foo_bar_total 1.0\n"
39+
+ "# EOF\n";
3940

40-
assertEquals(expected, convertToOpenMetricsFormat());
41+
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
4142
}
4243

43-
@Test
44+
@org.junit.jupiter.api.Test
4445
public void testGauge() {
46+
// don't convert to lambda, as we need to test the type
4547
Gauge<Integer> integerGauge =
46-
new Gauge<Integer>() {
47-
@Override
48-
public Integer getValue() {
49-
return 1234;
50-
}
51-
};
48+
new Gauge<Integer>() {
49+
@Override
50+
public Integer getValue() {
51+
return 1234;
52+
}
53+
};
5254
Gauge<Double> doubleGauge =
53-
new Gauge<Double>() {
54-
@Override
55-
public Double getValue() {
56-
return 1.234D;
57-
}
58-
};
55+
new Gauge<Double>() {
56+
@Override
57+
public Double getValue() {
58+
return 1.234D;
59+
}
60+
};
5961
Gauge<Long> longGauge =
60-
new Gauge<Long>() {
61-
@Override
62-
public Long getValue() {
63-
return 1234L;
64-
}
65-
};
62+
new Gauge<Long>() {
63+
@Override
64+
public Long getValue() {
65+
return 1234L;
66+
}
67+
};
6668
Gauge<Float> floatGauge =
67-
new Gauge<Float>() {
68-
@Override
69-
public Float getValue() {
70-
return 0.1234F;
71-
}
72-
};
69+
new Gauge<Float>() {
70+
@Override
71+
public Float getValue() {
72+
return 0.1234F;
73+
}
74+
};
7375
Gauge<Boolean> booleanGauge =
74-
new Gauge<Boolean>() {
75-
@Override
76-
public Boolean getValue() {
77-
return true;
78-
}
79-
};
76+
new Gauge<Boolean>() {
77+
@Override
78+
public Boolean getValue() {
79+
return true;
80+
}
81+
};
8082

8183
metricRegistry.register("double.gauge", doubleGauge);
8284
metricRegistry.register("long.gauge", longGauge);
@@ -85,62 +87,50 @@ public Boolean getValue() {
8587
metricRegistry.register("boolean.gauge", booleanGauge);
8688

8789
String expected =
88-
"# TYPE boolean_gauge gauge\n"
89-
+ "# HELP boolean_gauge Generated from Dropwizard metric import (metric=boolean.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$5)\n"
90-
+ "boolean_gauge 1.0\n"
91-
+ "# TYPE double_gauge gauge\n"
92-
+ "# HELP double_gauge Generated from Dropwizard metric import (metric=double.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$2)\n"
93-
+ "double_gauge 1.234\n"
94-
+ "# TYPE float_gauge gauge\n"
95-
+ "# HELP float_gauge Generated from Dropwizard metric import (metric=float.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$4)\n"
96-
+ "float_gauge 0.1234000027179718\n"
97-
+ "# TYPE integer_gauge gauge\n"
98-
+ "# HELP integer_gauge Generated from Dropwizard metric import (metric=integer.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$1)\n"
99-
+ "integer_gauge 1234.0\n"
100-
+ "# TYPE long_gauge gauge\n"
101-
+ "# HELP long_gauge Generated from Dropwizard metric import (metric=long.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$3)\n"
102-
+ "long_gauge 1234.0\n"
103-
+ "# EOF\n";
90+
"# TYPE boolean_gauge gauge\n"
91+
+ "# HELP boolean_gauge Generated from Dropwizard metric import (metric=boolean.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$5)\n"
92+
+ "boolean_gauge 1.0\n"
93+
+ "# TYPE double_gauge gauge\n"
94+
+ "# HELP double_gauge Generated from Dropwizard metric import (metric=double.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$2)\n"
95+
+ "double_gauge 1.234\n"
96+
+ "# TYPE float_gauge gauge\n"
97+
+ "# HELP float_gauge Generated from Dropwizard metric import (metric=float.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$4)\n"
98+
+ "float_gauge 0.1234000027179718\n"
99+
+ "# TYPE integer_gauge gauge\n"
100+
+ "# HELP integer_gauge Generated from Dropwizard metric import (metric=integer.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$1)\n"
101+
+ "integer_gauge 1234.0\n"
102+
+ "# TYPE long_gauge gauge\n"
103+
+ "# HELP long_gauge Generated from Dropwizard metric import (metric=long.gauge, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$3)\n"
104+
+ "long_gauge 1234.0\n"
105+
+ "# EOF\n";
104106

105-
assertEquals(expected, convertToOpenMetricsFormat());
107+
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
106108
}
107109

108-
@Test
110+
@org.junit.jupiter.api.Test
109111
public void testInvalidGaugeType() {
110-
Gauge<String> invalidGauge =
111-
new Gauge<String>() {
112-
@Override
113-
public String getValue() {
114-
return "foobar";
115-
}
116-
};
112+
Gauge<String> invalidGauge = () -> "foobar";
117113

118114
metricRegistry.register("invalid_gauge", invalidGauge);
119115

120116
String expected = "# EOF\n";
121-
assertEquals(expected, convertToOpenMetricsFormat());
117+
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
122118
}
123119

124-
@Test
120+
@org.junit.jupiter.api.Test
125121
public void testGaugeReturningNullValue() {
126-
Gauge<String> invalidGauge =
127-
new Gauge<String>() {
128-
@Override
129-
public String getValue() {
130-
return null;
131-
}
132-
};
122+
Gauge<String> invalidGauge = () -> null;
133123
metricRegistry.register("invalid_gauge", invalidGauge);
134124
String expected = "# EOF\n";
135-
assertEquals(expected, convertToOpenMetricsFormat());
125+
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
136126
}
137127

138-
@Test
139-
public void testHistogram() throws IOException {
128+
@org.junit.jupiter.api.Test
129+
public void testHistogram() {
140130
// just test the standard mapper
141131
final MetricRegistry metricRegistry = new MetricRegistry();
142132
PrometheusRegistry pmRegistry = new PrometheusRegistry();
143-
DropwizardExports.builder().dropwizardRegistry(metricRegistry).register(pmRegistry);
133+
io.prometheus.metrics.instrumentation.dropwizard.DropwizardExports.builder().dropwizardRegistry(metricRegistry).register(pmRegistry);
144134

145135
Histogram hist = metricRegistry.histogram("hist");
146136
int i = 0;
@@ -181,51 +171,51 @@ public void testHistogram() throws IOException {
181171
// The following asserts the values, but allows an error of 1.0 for quantile values.
182172

183173
MetricSnapshots snapshots = pmRegistry.scrape(name -> name.equals("hist"));
184-
Assert.assertEquals(1, snapshots.size());
174+
assertThat(snapshots.size()).isOne();
185175
SummarySnapshot snapshot = (SummarySnapshot) snapshots.get(0);
186-
Assert.assertEquals("hist", snapshot.getMetadata().getName());
187-
Assert.assertEquals(
188-
"Generated from Dropwizard metric import (metric=hist, type=com.codahale.metrics.Histogram)",
189-
snapshot.getMetadata().getHelp());
190-
Assert.assertEquals(1, snapshot.getDataPoints().size());
176+
assertThat(snapshot.getMetadata().getName()).isEqualTo("hist");
177+
assertThat(snapshot.getMetadata().getHelp())
178+
.isEqualTo(
179+
"Generated from Dropwizard metric import (metric=hist, type=com.codahale.metrics.Histogram)");
180+
assertThat(snapshot.getDataPoints().size()).isOne();
191181
SummarySnapshot.SummaryDataPointSnapshot dataPoint = snapshot.getDataPoints().get(0);
192-
Assert.assertTrue(dataPoint.hasCount());
193-
Assert.assertEquals(100, dataPoint.getCount());
194-
Assert.assertFalse(dataPoint.hasSum());
182+
assertThat(dataPoint.hasCount()).isTrue();
183+
assertThat(dataPoint.getCount()).isEqualTo(100);
184+
assertThat(dataPoint.hasSum()).isFalse();
195185
Quantiles quantiles = dataPoint.getQuantiles();
196-
Assert.assertEquals(6, quantiles.size());
197-
Assert.assertEquals(0.5, quantiles.get(0).getQuantile(), 0.0);
198-
Assert.assertEquals(49.0, quantiles.get(0).getValue(), 1.0);
199-
Assert.assertEquals(0.75, quantiles.get(1).getQuantile(), 0.0);
200-
Assert.assertEquals(74.0, quantiles.get(1).getValue(), 1.0);
201-
Assert.assertEquals(0.95, quantiles.get(2).getQuantile(), 0.0);
202-
Assert.assertEquals(94.0, quantiles.get(2).getValue(), 1.0);
203-
Assert.assertEquals(0.98, quantiles.get(3).getQuantile(), 0.0);
204-
Assert.assertEquals(97.0, quantiles.get(3).getValue(), 1.0);
205-
Assert.assertEquals(0.99, quantiles.get(4).getQuantile(), 0.0);
206-
Assert.assertEquals(98.0, quantiles.get(4).getValue(), 1.0);
207-
Assert.assertEquals(0.999, quantiles.get(5).getQuantile(), 0.0);
208-
Assert.assertEquals(99.0, quantiles.get(5).getValue(), 1.0);
186+
assertThat(quantiles.size()).isEqualTo(6);
187+
assertThat(quantiles.get(0).getQuantile()).isEqualTo(0.5);
188+
assertThat(quantiles.get(0).getValue()).isCloseTo(49.0, offset(1.0));
189+
assertThat(quantiles.get(1).getQuantile()).isEqualTo(0.75);
190+
assertThat(quantiles.get(1).getValue()).isCloseTo(74.0, offset(1.0));
191+
assertThat(quantiles.get(2).getQuantile()).isEqualTo(0.95);
192+
assertThat(quantiles.get(2).getValue()).isCloseTo(94.0, offset(1.0));
193+
assertThat(quantiles.get(3).getQuantile()).isEqualTo(0.98);
194+
assertThat(quantiles.get(3).getValue()).isCloseTo(97.0, offset(1.0));
195+
assertThat(quantiles.get(4).getQuantile()).isEqualTo(0.99);
196+
assertThat(quantiles.get(4).getValue()).isCloseTo(98.0, offset(1.0));
197+
assertThat(quantiles.get(5).getQuantile()).isEqualTo(0.999);
198+
assertThat(quantiles.get(5).getValue()).isCloseTo(99.0, offset(1.0));
209199
}
210200

211-
@Test
201+
@org.junit.jupiter.api.Test
212202
public void testMeter() {
213203
Meter meter = metricRegistry.meter("meter");
214204
meter.mark();
215205
meter.mark();
216206

217207
String expected =
218-
"# TYPE meter counter\n"
219-
+ "# HELP meter Generated from Dropwizard metric import (metric=meter_total, type=com.codahale.metrics.Meter)\n"
220-
+ "meter_total 2.0\n"
221-
+ "# EOF\n";
222-
assertEquals(expected, convertToOpenMetricsFormat());
208+
"# TYPE meter counter\n"
209+
+ "# HELP meter Generated from Dropwizard metric import (metric=meter_total, type=com.codahale.metrics.Meter)\n"
210+
+ "meter_total 2.0\n"
211+
+ "# EOF\n";
212+
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
223213
}
224214

225-
@Test
215+
@org.junit.jupiter.api.Test
226216
public void testTimer() throws InterruptedException {
227217
final MetricRegistry metricRegistry = new MetricRegistry();
228-
DropwizardExports exports = new DropwizardExports(metricRegistry);
218+
io.prometheus.metrics.instrumentation.dropwizard.DropwizardExports exports = new DropwizardExports(metricRegistry);
229219
Timer t = metricRegistry.timer("timer");
230220
Timer.Context time = t.time();
231221
Thread.sleep(100L);
@@ -234,18 +224,18 @@ public void testTimer() throws InterruptedException {
234224
System.out.println(timeSpentMillis);
235225

236226
SummarySnapshot.SummaryDataPointSnapshot dataPointSnapshot =
237-
(SummarySnapshot.SummaryDataPointSnapshot)
238-
exports.collect().stream().flatMap(i -> i.getDataPoints().stream()).findFirst().get();
227+
(SummarySnapshot.SummaryDataPointSnapshot)
228+
exports.collect().stream().flatMap(i -> i.getDataPoints().stream()).findFirst().get();
239229
// We slept for 1Ms so we ensure that all timers are above 1ms:
240-
assertTrue(dataPointSnapshot.getQuantiles().size() > 1);
230+
assertThat(dataPointSnapshot.getQuantiles().size()).isGreaterThan(1);
241231
dataPointSnapshot
242-
.getQuantiles()
243-
.forEach(
244-
i -> {
245-
System.out.println(i.getQuantile() + " : " + i.getValue());
246-
assertTrue(i.getValue() > timeSpentMillis / 1000d);
247-
});
248-
assertEquals(1, dataPointSnapshot.getCount());
232+
.getQuantiles()
233+
.forEach(
234+
i -> {
235+
System.out.println(i.getQuantile() + " : " + i.getValue());
236+
assertThat(i.getValue()).isGreaterThan(timeSpentMillis / 1000d);
237+
});
238+
assertThat(dataPointSnapshot.getCount()).isOne();
249239
}
250240

251241
@Test
@@ -258,35 +248,35 @@ public void testThatMetricHelpUsesOriginalDropwizardName() {
258248
metricRegistry.register("my.application.namedGauge1", new ExampleDoubleGauge());
259249

260250
String expected =
261-
"# TYPE my_application_namedCounter1 counter\n"
262-
+ "# HELP my_application_namedCounter1 Generated from Dropwizard metric import (metric=my.application.namedCounter1, type=com.codahale.metrics.Counter)\n"
263-
+ "my_application_namedCounter1_total 0.0\n"
264-
+ "# TYPE my_application_namedGauge1 gauge\n"
265-
+ "# HELP my_application_namedGauge1 Generated from Dropwizard metric import (metric=my.application.namedGauge1, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$ExampleDoubleGauge)\n"
266-
+ "my_application_namedGauge1 0.0\n"
267-
+ "# TYPE my_application_namedHistogram1 summary\n"
268-
+ "# HELP my_application_namedHistogram1 Generated from Dropwizard metric import (metric=my.application.namedHistogram1, type=com.codahale.metrics.Histogram)\n"
269-
+ "my_application_namedHistogram1{quantile=\"0.5\"} 0.0\n"
270-
+ "my_application_namedHistogram1{quantile=\"0.75\"} 0.0\n"
271-
+ "my_application_namedHistogram1{quantile=\"0.95\"} 0.0\n"
272-
+ "my_application_namedHistogram1{quantile=\"0.98\"} 0.0\n"
273-
+ "my_application_namedHistogram1{quantile=\"0.99\"} 0.0\n"
274-
+ "my_application_namedHistogram1{quantile=\"0.999\"} 0.0\n"
275-
+ "my_application_namedHistogram1_count 0\n"
276-
+ "# TYPE my_application_namedMeter1 counter\n"
277-
+ "# HELP my_application_namedMeter1 Generated from Dropwizard metric import (metric=my.application.namedMeter1_total, type=com.codahale.metrics.Meter)\n"
278-
+ "my_application_namedMeter1_total 0.0\n"
279-
+ "# TYPE my_application_namedTimer1 summary\n"
280-
+ "# HELP my_application_namedTimer1 Generated from Dropwizard metric import (metric=my.application.namedTimer1, type=com.codahale.metrics.Timer)\n"
281-
+ "my_application_namedTimer1{quantile=\"0.5\"} 0.0\n"
282-
+ "my_application_namedTimer1{quantile=\"0.75\"} 0.0\n"
283-
+ "my_application_namedTimer1{quantile=\"0.95\"} 0.0\n"
284-
+ "my_application_namedTimer1{quantile=\"0.98\"} 0.0\n"
285-
+ "my_application_namedTimer1{quantile=\"0.99\"} 0.0\n"
286-
+ "my_application_namedTimer1{quantile=\"0.999\"} 0.0\n"
287-
+ "my_application_namedTimer1_count 0\n"
288-
+ "# EOF\n";
289-
assertEquals(expected, convertToOpenMetricsFormat());
251+
"# TYPE my_application_namedCounter1 counter\n"
252+
+ "# HELP my_application_namedCounter1 Generated from Dropwizard metric import (metric=my.application.namedCounter1, type=com.codahale.metrics.Counter)\n"
253+
+ "my_application_namedCounter1_total 0.0\n"
254+
+ "# TYPE my_application_namedGauge1 gauge\n"
255+
+ "# HELP my_application_namedGauge1 Generated from Dropwizard metric import (metric=my.application.namedGauge1, type=io.prometheus.metrics.instrumentation.dropwizard.DropwizardExportsTest$ExampleDoubleGauge)\n"
256+
+ "my_application_namedGauge1 0.0\n"
257+
+ "# TYPE my_application_namedHistogram1 summary\n"
258+
+ "# HELP my_application_namedHistogram1 Generated from Dropwizard metric import (metric=my.application.namedHistogram1, type=com.codahale.metrics.Histogram)\n"
259+
+ "my_application_namedHistogram1{quantile=\"0.5\"} 0.0\n"
260+
+ "my_application_namedHistogram1{quantile=\"0.75\"} 0.0\n"
261+
+ "my_application_namedHistogram1{quantile=\"0.95\"} 0.0\n"
262+
+ "my_application_namedHistogram1{quantile=\"0.98\"} 0.0\n"
263+
+ "my_application_namedHistogram1{quantile=\"0.99\"} 0.0\n"
264+
+ "my_application_namedHistogram1{quantile=\"0.999\"} 0.0\n"
265+
+ "my_application_namedHistogram1_count 0\n"
266+
+ "# TYPE my_application_namedMeter1 counter\n"
267+
+ "# HELP my_application_namedMeter1 Generated from Dropwizard metric import (metric=my.application.namedMeter1_total, type=com.codahale.metrics.Meter)\n"
268+
+ "my_application_namedMeter1_total 0.0\n"
269+
+ "# TYPE my_application_namedTimer1 summary\n"
270+
+ "# HELP my_application_namedTimer1 Generated from Dropwizard metric import (metric=my.application.namedTimer1, type=com.codahale.metrics.Timer)\n"
271+
+ "my_application_namedTimer1{quantile=\"0.5\"} 0.0\n"
272+
+ "my_application_namedTimer1{quantile=\"0.75\"} 0.0\n"
273+
+ "my_application_namedTimer1{quantile=\"0.95\"} 0.0\n"
274+
+ "my_application_namedTimer1{quantile=\"0.98\"} 0.0\n"
275+
+ "my_application_namedTimer1{quantile=\"0.99\"} 0.0\n"
276+
+ "my_application_namedTimer1{quantile=\"0.999\"} 0.0\n"
277+
+ "my_application_namedTimer1_count 0\n"
278+
+ "# EOF\n";
279+
assertThat(convertToOpenMetricsFormat()).isEqualTo(expected);
290280
}
291281

292282
private static class ExampleDoubleGauge implements Gauge<Double> {

0 commit comments

Comments
 (0)