Skip to content

Commit

Permalink
fix nullpointer exception
Browse files Browse the repository at this point in the history
Signed-off-by: Yaolong Liu <[email protected]>
  • Loading branch information
codings-dan committed Oct 18, 2021
1 parent 3ff7064 commit 14337f7
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,25 @@ MetricFamilySamples fromCounter(String dropwizardName, Counter counter) {
* Export gauge as a prometheus gauge.
*/
MetricFamilySamples fromGauge(String dropwizardName, Gauge gauge) {
Object obj = gauge.getValue();
double value;
if (obj instanceof Number) {
value = ((Number) obj).doubleValue();
} else if (obj instanceof Boolean) {
value = ((Boolean) obj) ? 1 : 0;
} else {
LOGGER.log(Level.FINE, String.format("Invalid type for Gauge %s: %s", sanitizeMetricName(dropwizardName),
obj == null ? "null" : obj.getClass().getName()));
return null;
try {
Object obj = gauge.getValue();
double value;
if (obj instanceof Number) {
value = ((Number) obj).doubleValue();
} else if (obj instanceof Boolean) {
value = ((Boolean) obj) ? 1 : 0;
} else {
LOGGER.log(Level.FINE, String.format("Invalid type for Gauge %s: %s", sanitizeMetricName(dropwizardName),
obj == null ? "null" : obj.getClass().getName()));
return null;
}
MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "",
new ArrayList<String>(), new ArrayList<String>(), value);
return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, gauge), Arrays.asList(sample));
} catch (NullPointerException e) {
e.printStackTrace();
}
MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "",
new ArrayList<String>(), new ArrayList<String>(), value);
return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, gauge), Arrays.asList(sample));
return null;
}

/**
Expand Down

0 comments on commit 14337f7

Please sign in to comment.