Skip to content

Commit 14337f7

Browse files
committed
fix nullpointer exception
Signed-off-by: Yaolong Liu <lylpysz1@163.com>
1 parent 3ff7064 commit 14337f7

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

simpleclient_dropwizard/src/main/java/io/prometheus/client/dropwizard/DropwizardExports.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,25 @@ MetricFamilySamples fromCounter(String dropwizardName, Counter counter) {
9393
* Export gauge as a prometheus gauge.
9494
*/
9595
MetricFamilySamples fromGauge(String dropwizardName, Gauge gauge) {
96-
Object obj = gauge.getValue();
97-
double value;
98-
if (obj instanceof Number) {
99-
value = ((Number) obj).doubleValue();
100-
} else if (obj instanceof Boolean) {
101-
value = ((Boolean) obj) ? 1 : 0;
102-
} else {
103-
LOGGER.log(Level.FINE, String.format("Invalid type for Gauge %s: %s", sanitizeMetricName(dropwizardName),
104-
obj == null ? "null" : obj.getClass().getName()));
105-
return null;
96+
try {
97+
Object obj = gauge.getValue();
98+
double value;
99+
if (obj instanceof Number) {
100+
value = ((Number) obj).doubleValue();
101+
} else if (obj instanceof Boolean) {
102+
value = ((Boolean) obj) ? 1 : 0;
103+
} else {
104+
LOGGER.log(Level.FINE, String.format("Invalid type for Gauge %s: %s", sanitizeMetricName(dropwizardName),
105+
obj == null ? "null" : obj.getClass().getName()));
106+
return null;
107+
}
108+
MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "",
109+
new ArrayList<String>(), new ArrayList<String>(), value);
110+
return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, gauge), Arrays.asList(sample));
111+
} catch (NullPointerException e) {
112+
e.printStackTrace();
106113
}
107-
MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "",
108-
new ArrayList<String>(), new ArrayList<String>(), value);
109-
return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, gauge), Arrays.asList(sample));
114+
return null;
110115
}
111116

112117
/**

0 commit comments

Comments
 (0)