PrometheusReporter implements MetricReporter { @VisibleForTesting static io.prometheus.client.Gauge.Child gaugeFrom(Gauge gauge) { return new io.prometheus.client.Gauge.Child() { @Override public double get() { final Object value = gauge.getValue(); if (value == null) { LOG.debug("Gauge {} is null-valued, defaulting to 0.", gauge); return 0; } if (value instanceof Double) { return (double) value; } if (value instanceof Number) { return ((Number) value).doubleValue(); } if (value instanceof Boolean) { return ((Boolean) value) ? 1 : 0; } LOG.debug("Invalid type for Gauge {}: {}, only number types and booleans are supported by this reporter.", gauge, value.getClass().getName()); return 0; } }; }  @Override void open(MetricConfig config); @Override void close(); @Override void notifyOfAddedMetric(final Metric metric, final String metricName, final MetricGroup group); @Override void notifyOfRemovedMetric(final Metric metric, final String metricName, final MetricGroup group);  }
@Test public void doubleGaugeIsConvertedCorrectly() { assertThat(PrometheusReporter.gaugeFrom(new Gauge<Double>() { @Override public Double getValue() { return 3.14; } }).get(), equalTo(3.14)); }
@Test public void shortGaugeIsConvertedCorrectly() { assertThat(PrometheusReporter.gaugeFrom(new Gauge<Short>() { @Override public Short getValue() { return 13; } }).get(), equalTo(13.)); }
@Test public void booleanGaugeIsConvertedCorrectly() { assertThat(PrometheusReporter.gaugeFrom(new Gauge<Boolean>() { @Override public Boolean getValue() { return true; } }).get(), equalTo(1.)); }
@Test public void stringGaugeCannotBeConverted() { assertThat(PrometheusReporter.gaugeFrom(new Gauge<String>() { @Override public String getValue() { return "I am not a number"; } }).get(), equalTo(0.)); }