Skip to content

Commit 99fd8f3

Browse files
committed
nullaway
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 697effc commit 99fd8f3

File tree

1 file changed

+34
-36
lines changed
  • prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5

1 file changed

+34
-36
lines changed

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

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
2424
import java.util.Collections;
2525
import java.util.Map;
26-
import java.util.Optional;
2726
import java.util.concurrent.TimeUnit;
2827
import java.util.function.BiFunction;
2928
import java.util.logging.Level;
3029
import java.util.logging.Logger;
30+
import javax.annotation.Nullable;
3131

3232
/** Collect Dropwizard metrics from a MetricRegistry. */
3333
public class DropwizardExports implements MultiCollector {
3434
private static final Logger logger = Logger.getLogger(DropwizardExports.class.getName());
3535
private final MetricRegistry registry;
3636
private final MetricFilter metricFilter;
37-
private final Optional<CustomLabelMapper> labelMapper;
37+
@Nullable private final CustomLabelMapper labelMapper;
3838
private final InvalidMetricHandler invalidMetricHandler;
3939

4040
/**
@@ -46,7 +46,7 @@ public DropwizardExports(MetricRegistry registry) {
4646
super();
4747
this.registry = registry;
4848
this.metricFilter = MetricFilter.ALL;
49-
this.labelMapper = Optional.empty();
49+
this.labelMapper = null;
5050
this.invalidMetricHandler = InvalidMetricHandler.ALWAYS_THROW;
5151
}
5252

@@ -59,7 +59,7 @@ public DropwizardExports(MetricRegistry registry) {
5959
public DropwizardExports(MetricRegistry registry, MetricFilter metricFilter) {
6060
this.registry = registry;
6161
this.metricFilter = metricFilter;
62-
this.labelMapper = Optional.empty();
62+
this.labelMapper = null;
6363
this.invalidMetricHandler = InvalidMetricHandler.ALWAYS_THROW;
6464
}
6565

@@ -69,10 +69,12 @@ public DropwizardExports(MetricRegistry registry, MetricFilter metricFilter) {
6969
* @param labelMapper a labelMapper to use to map labels.
7070
*/
7171
public DropwizardExports(
72-
MetricRegistry registry, MetricFilter metricFilter, CustomLabelMapper labelMapper) {
72+
MetricRegistry registry,
73+
MetricFilter metricFilter,
74+
@Nullable CustomLabelMapper labelMapper) {
7375
this.registry = registry;
7476
this.metricFilter = metricFilter;
75-
this.labelMapper = Optional.ofNullable(labelMapper);
77+
this.labelMapper = labelMapper;
7678
this.invalidMetricHandler = InvalidMetricHandler.ALWAYS_THROW;
7779
}
7880

@@ -84,11 +86,11 @@ public DropwizardExports(
8486
private DropwizardExports(
8587
MetricRegistry registry,
8688
MetricFilter metricFilter,
87-
CustomLabelMapper labelMapper,
89+
@Nullable CustomLabelMapper labelMapper,
8890
InvalidMetricHandler invalidMetricHandler) {
8991
this.registry = registry;
9092
this.metricFilter = metricFilter;
91-
this.labelMapper = Optional.ofNullable(labelMapper);
93+
this.labelMapper = labelMapper;
9294
this.invalidMetricHandler = invalidMetricHandler;
9395
}
9496

@@ -99,7 +101,7 @@ private static String getHelpMessage(String metricName, Metric metric) {
99101
}
100102

101103
private MetricMetadata getMetricMetaData(String metricName, Metric metric) {
102-
String name = labelMapper.isPresent() ? labelMapper.get().getName(metricName) : metricName;
104+
String name = labelMapper != null ? labelMapper.getName(metricName) : metricName;
103105
return new MetricMetadata(
104106
PrometheusNaming.sanitizeMetricName(name), getHelpMessage(metricName, metric));
105107
}
@@ -113,15 +115,15 @@ MetricSnapshot fromCounter(String dropwizardName, Counter counter) {
113115
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder =
114116
CounterSnapshot.CounterDataPointSnapshot.builder()
115117
.value(Long.valueOf(counter.getCount()).doubleValue());
116-
labelMapper.ifPresent(
117-
mapper ->
118-
dataPointBuilder.labels(
119-
mapper.getLabels(
120-
dropwizardName, Collections.emptyList(), Collections.emptyList())));
118+
if (labelMapper != null) {
119+
dataPointBuilder.labels(
120+
labelMapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList()));
121+
}
121122
return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
122123
}
123124

124125
/** Export gauge as a prometheus gauge. */
126+
@Nullable
125127
MetricSnapshot fromGauge(String dropwizardName, Gauge<?> gauge) {
126128
Object obj = gauge.getValue();
127129
double value;
@@ -141,11 +143,10 @@ MetricSnapshot fromGauge(String dropwizardName, Gauge<?> gauge) {
141143
MetricMetadata metadata = getMetricMetaData(dropwizardName, gauge);
142144
GaugeSnapshot.GaugeDataPointSnapshot.Builder dataPointBuilder =
143145
GaugeSnapshot.GaugeDataPointSnapshot.builder().value(value);
144-
labelMapper.ifPresent(
145-
mapper ->
146-
dataPointBuilder.labels(
147-
mapper.getLabels(
148-
dropwizardName, Collections.emptyList(), Collections.emptyList())));
146+
if (labelMapper != null) {
147+
dataPointBuilder.labels(
148+
labelMapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList()));
149+
}
149150
return new GaugeSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
150151
}
151152

@@ -169,17 +170,15 @@ MetricSnapshot fromSnapshotAndCount(
169170
.quantile(0.999, snapshot.get999thPercentile() * factor)
170171
.build();
171172

172-
String name =
173-
labelMapper.isPresent() ? labelMapper.get().getName(dropwizardName) : dropwizardName;
173+
String name = labelMapper != null ? labelMapper.getName(dropwizardName) : dropwizardName;
174174
MetricMetadata metadata =
175175
new MetricMetadata(PrometheusNaming.sanitizeMetricName(name), helpMessage);
176176
SummarySnapshot.SummaryDataPointSnapshot.Builder dataPointBuilder =
177177
SummarySnapshot.SummaryDataPointSnapshot.builder().quantiles(quantiles).count(count);
178-
labelMapper.ifPresent(
179-
mapper ->
180-
dataPointBuilder.labels(
181-
mapper.getLabels(
182-
dropwizardName, Collections.emptyList(), Collections.emptyList())));
178+
if (labelMapper != null) {
179+
dataPointBuilder.labels(
180+
labelMapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList()));
181+
}
183182
return new SummarySnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
184183
}
185184

@@ -208,11 +207,10 @@ MetricSnapshot fromMeter(String dropwizardName, Meter meter) {
208207
MetricMetadata metadata = getMetricMetaData(dropwizardName + "_total", meter);
209208
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder =
210209
CounterSnapshot.CounterDataPointSnapshot.builder().value(meter.getCount());
211-
labelMapper.ifPresent(
212-
mapper ->
213-
dataPointBuilder.labels(
214-
mapper.getLabels(
215-
dropwizardName, Collections.emptyList(), Collections.emptyList())));
210+
if (labelMapper != null) {
211+
dataPointBuilder.labels(
212+
labelMapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList()));
213+
}
216214
return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
217215
}
218216

@@ -229,10 +227,10 @@ public MetricSnapshots collect() {
229227

230228
private <T> void collectMetricKind(
231229
MetricSnapshots.Builder builder,
232-
Map<MetricName, T> metric,
230+
Map<String, T> metric,
233231
BiFunction<String, T, MetricSnapshot> toSnapshot) {
234-
for (Map.Entry<MetricName, T> entry : metric.entrySet()) {
235-
String metricName = entry.getKey().getKey();
232+
for (Map.Entry<String, T> entry : metric.entrySet()) {
233+
String metricName = entry.getKey();
236234
try {
237235
MetricSnapshot snapshot = toSnapshot.apply(metricName, entry.getValue());
238236
if (snapshot != null) {
@@ -252,9 +250,9 @@ public static Builder builder() {
252250

253251
// Builder class for DropwizardExports
254252
public static class Builder {
255-
private MetricRegistry registry;
253+
@Nullable private MetricRegistry registry;
256254
private MetricFilter metricFilter;
257-
private CustomLabelMapper labelMapper;
255+
@Nullable private CustomLabelMapper labelMapper;
258256
private InvalidMetricHandler invalidMetricHandler;
259257

260258
private Builder() {

0 commit comments

Comments
 (0)