Skip to content

Commit 35eabed

Browse files
committed
Update DropwizardExports.java
Signed-off-by: Kinshuk Bairagi <[email protected]>
1 parent bb5ff35 commit 35eabed

File tree

1 file changed

+83
-53
lines changed
  • prometheus-metrics-instrumentation-dropwizard/src/main/java/io/prometheus/metrics/instrumentation/dropwizard

1 file changed

+83
-53
lines changed

prometheus-metrics-instrumentation-dropwizard/src/main/java/io/prometheus/metrics/instrumentation/dropwizard/DropwizardExports.java

Lines changed: 83 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
import java.util.logging.Level;
1313
import java.util.logging.Logger;
1414

15-
/**
16-
* Collect Dropwizard metrics from a MetricRegistry.
17-
*/
15+
16+
/** Collect Dropwizard metrics from a MetricRegistry. */
1817
public class DropwizardExports implements MultiCollector {
19-
private static final Logger LOGGER = Logger.getLogger(DropwizardExports.class.getName());
18+
private static final Logger logger = Logger.getLogger(DropwizardExports.class.getName());
2019
private final MetricRegistry registry;
2120
private final MetricFilter metricFilter;
2221
private final Optional<CustomLabelMapper> labelMapper;
@@ -36,7 +35,7 @@ public DropwizardExports(MetricRegistry registry) {
3635
/**
3736
* Creates a new DropwizardExports with a custom {@link MetricFilter}.
3837
*
39-
* @param registry a metric registry to export in prometheus.
38+
* @param registry a metric registry to export in prometheus.
4039
* @param metricFilter a custom metric filter.
4140
*/
4241
public DropwizardExports(MetricRegistry registry, MetricFilter metricFilter) {
@@ -46,39 +45,47 @@ public DropwizardExports(MetricRegistry registry, MetricFilter metricFilter) {
4645
}
4746

4847
/**
49-
* @param registry a metric registry to export in prometheus.
48+
* @param registry a metric registry to export in prometheus.
5049
* @param metricFilter a custom metric filter.
51-
* @param labelMapper a labelMapper to use to map labels.
50+
* @param labelMapper a labelMapper to use to map labels.
5251
*/
53-
public DropwizardExports(MetricRegistry registry, MetricFilter metricFilter, CustomLabelMapper labelMapper) {
52+
public DropwizardExports(
53+
MetricRegistry registry, MetricFilter metricFilter, CustomLabelMapper labelMapper) {
5454
this.registry = registry;
5555
this.metricFilter = metricFilter;
5656
this.labelMapper = Optional.ofNullable(labelMapper);
5757
}
5858

5959
private static String getHelpMessage(String metricName, Metric metric) {
60-
return String.format("Generated from Dropwizard metric import (metric=%s, type=%s)",
60+
return String.format(
61+
"Generated from Dropwizard metric import (metric=%s, type=%s)",
6162
metricName, metric.getClass().getName());
6263
}
6364

6465
private MetricMetadata getMetricMetaData(String metricName, Metric metric) {
6566
String name = labelMapper.isPresent() ? labelMapper.get().getName(metricName) : metricName;
66-
return new MetricMetadata(PrometheusNaming.sanitizeMetricName(name), getHelpMessage(metricName, metric));
67+
return new MetricMetadata(
68+
PrometheusNaming.sanitizeMetricName(name), getHelpMessage(metricName, metric));
6769
}
6870

6971
/**
70-
* Export counter as Prometheus <a href="https://prometheus.io/docs/concepts/metric_types/#gauge">Gauge</a>.
72+
* Export counter as Prometheus <a
73+
* href="https://prometheus.io/docs/concepts/metric_types/#gauge">Gauge</a>.
7174
*/
7275
MetricSnapshot fromCounter(String dropwizardName, Counter counter) {
7376
MetricMetadata metadata = getMetricMetaData(dropwizardName, counter);
74-
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder = CounterSnapshot.CounterDataPointSnapshot.builder().value(Long.valueOf(counter.getCount()).doubleValue());
75-
labelMapper.map(mapper -> dataPointBuilder.labels(mapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList())));
77+
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder =
78+
CounterSnapshot.CounterDataPointSnapshot.builder()
79+
.value(Long.valueOf(counter.getCount()).doubleValue());
80+
labelMapper.ifPresent(
81+
mapper ->
82+
dataPointBuilder.labels(
83+
mapper.getLabels(
84+
dropwizardName, Collections.emptyList(), Collections.emptyList())));
7685
return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
7786
}
7887

79-
/**
80-
* Export gauge as a prometheus gauge.
81-
*/
88+
/** Export gauge as a prometheus gauge. */
8289
MetricSnapshot fromGauge(String dropwizardName, Gauge<?> gauge) {
8390
Object obj = gauge.getValue();
8491
double value;
@@ -87,63 +94,87 @@ MetricSnapshot fromGauge(String dropwizardName, Gauge<?> gauge) {
8794
} else if (obj instanceof Boolean) {
8895
value = ((Boolean) obj) ? 1 : 0;
8996
} else {
90-
LOGGER.log(Level.FINE, String.format("Invalid type for Gauge %s: %s", PrometheusNaming.sanitizeMetricName(dropwizardName),
91-
obj == null ? "null" : obj.getClass().getName()));
97+
logger.log(
98+
Level.FINE,
99+
String.format(
100+
"Invalid type for Gauge %s: %s",
101+
PrometheusNaming.sanitizeMetricName(dropwizardName),
102+
obj == null ? "null" : obj.getClass().getName()));
92103
return null;
93104
}
94105
MetricMetadata metadata = getMetricMetaData(dropwizardName, gauge);
95-
GaugeSnapshot.GaugeDataPointSnapshot.Builder dataPointBuilder = GaugeSnapshot.GaugeDataPointSnapshot.builder().value(value);
96-
labelMapper.map(mapper -> dataPointBuilder.labels(mapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList())));
106+
GaugeSnapshot.GaugeDataPointSnapshot.Builder dataPointBuilder =
107+
GaugeSnapshot.GaugeDataPointSnapshot.builder().value(value);
108+
labelMapper.ifPresent(
109+
mapper ->
110+
dataPointBuilder.labels(
111+
mapper.getLabels(
112+
dropwizardName, Collections.emptyList(), Collections.emptyList())));
97113
return new GaugeSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
98114
}
99115

100116
/**
101117
* Export a histogram snapshot as a prometheus SUMMARY.
102118
*
103119
* @param dropwizardName metric name.
104-
* @param snapshot the histogram snapshot.
105-
* @param count the total sample count for this snapshot.
106-
* @param factor a factor to apply to histogram values.
120+
* @param snapshot the histogram snapshot.
121+
* @param count the total sample count for this snapshot.
122+
* @param factor a factor to apply to histogram values.
107123
*/
108-
MetricSnapshot fromSnapshotAndCount(String dropwizardName, Snapshot snapshot, long count, double factor, String helpMessage) {
109-
Quantiles quantiles = Quantiles.builder()
110-
.quantile(0.5, snapshot.getMedian() * factor)
111-
.quantile(0.75, snapshot.get75thPercentile() * factor)
112-
.quantile(0.95, snapshot.get95thPercentile() * factor)
113-
.quantile(0.98, snapshot.get98thPercentile() * factor)
114-
.quantile(0.99, snapshot.get99thPercentile() * factor)
115-
.quantile(0.999, snapshot.get999thPercentile() * factor)
116-
.build();
117-
118-
MetricMetadata metadata = new MetricMetadata(PrometheusNaming.sanitizeMetricName(dropwizardName), helpMessage);
119-
SummarySnapshot.SummaryDataPointSnapshot.Builder dataPointBuilder = SummarySnapshot.SummaryDataPointSnapshot.builder().quantiles(quantiles).count(count);
120-
labelMapper.map(mapper -> dataPointBuilder.labels(mapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList())));
124+
MetricSnapshot fromSnapshotAndCount(
125+
String dropwizardName, Snapshot snapshot, long count, double factor, String helpMessage) {
126+
Quantiles quantiles =
127+
Quantiles.builder()
128+
.quantile(0.5, snapshot.getMedian() * factor)
129+
.quantile(0.75, snapshot.get75thPercentile() * factor)
130+
.quantile(0.95, snapshot.get95thPercentile() * factor)
131+
.quantile(0.98, snapshot.get98thPercentile() * factor)
132+
.quantile(0.99, snapshot.get99thPercentile() * factor)
133+
.quantile(0.999, snapshot.get999thPercentile() * factor)
134+
.build();
135+
136+
MetricMetadata metadata =
137+
new MetricMetadata(PrometheusNaming.sanitizeMetricName(dropwizardName), helpMessage);
138+
SummarySnapshot.SummaryDataPointSnapshot.Builder dataPointBuilder =
139+
SummarySnapshot.SummaryDataPointSnapshot.builder().quantiles(quantiles).count(count);
140+
labelMapper.ifPresent(
141+
mapper ->
142+
dataPointBuilder.labels(
143+
mapper.getLabels(
144+
dropwizardName, Collections.emptyList(), Collections.emptyList())));
121145
return new SummarySnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
122146
}
123147

124-
/**
125-
* Convert histogram snapshot.
126-
*/
148+
/** Convert histogram snapshot. */
127149
MetricSnapshot fromHistogram(String dropwizardName, Histogram histogram) {
128-
return fromSnapshotAndCount(dropwizardName, histogram.getSnapshot(), histogram.getCount(), 1.0,
150+
return fromSnapshotAndCount(
151+
dropwizardName,
152+
histogram.getSnapshot(),
153+
histogram.getCount(),
154+
1.0,
129155
getHelpMessage(dropwizardName, histogram));
130156
}
131157

132-
/**
133-
* Export Dropwizard Timer as a histogram. Use TIME_UNIT as time unit.
134-
*/
158+
/** Export Dropwizard Timer as a histogram. Use TIME_UNIT as time unit. */
135159
MetricSnapshot fromTimer(String dropwizardName, Timer timer) {
136-
return fromSnapshotAndCount(dropwizardName, timer.getSnapshot(), timer.getCount(),
137-
1.0D / TimeUnit.SECONDS.toNanos(1L), getHelpMessage(dropwizardName, timer));
160+
return fromSnapshotAndCount(
161+
dropwizardName,
162+
timer.getSnapshot(),
163+
timer.getCount(),
164+
1.0D / TimeUnit.SECONDS.toNanos(1L),
165+
getHelpMessage(dropwizardName, timer));
138166
}
139167

140-
/**
141-
* Export a Meter as a prometheus COUNTER.
142-
*/
168+
/** Export a Meter as a prometheus COUNTER. */
143169
MetricSnapshot fromMeter(String dropwizardName, Meter meter) {
144170
MetricMetadata metadata = getMetricMetaData(dropwizardName + "_total", meter);
145-
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder = CounterSnapshot.CounterDataPointSnapshot.builder().value(meter.getCount());
146-
labelMapper.map(mapper -> dataPointBuilder.labels(mapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList())));
171+
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder =
172+
CounterSnapshot.CounterDataPointSnapshot.builder().value(meter.getCount());
173+
labelMapper.ifPresent(
174+
mapper ->
175+
dataPointBuilder.labels(
176+
mapper.getLabels(
177+
dropwizardName, Collections.emptyList(), Collections.emptyList())));
147178
return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
148179
}
149180

@@ -170,15 +201,15 @@ public MetricSnapshots collect() {
170201
registry.getMeters(metricFilter).forEach((name, meter) ->
171202
metricSnapshots.metricSnapshot(fromMeter(name, meter))
172203
);
173-
204+
174205
return metricSnapshots.build();
175206
}
176207

177208
public static Builder builder() {
178209
return new Builder();
179210
}
180211

181-
//Builder class for DropwizardExports
212+
// Builder class for DropwizardExports
182213
public static class Builder {
183214
private MetricRegistry registry;
184215
private MetricFilter metricFilter;
@@ -223,5 +254,4 @@ public void register(PrometheusRegistry registry) {
223254
registry.register(dropwizardExports);
224255
}
225256
}
226-
227257
}

0 commit comments

Comments
 (0)