diff --git a/geomesa-metrics/geomesa-metrics-micrometer/pom.xml b/geomesa-metrics/geomesa-metrics-micrometer/pom.xml index 26b7594595ad..31d56cf119a9 100644 --- a/geomesa-metrics/geomesa-metrics-micrometer/pom.xml +++ b/geomesa-metrics/geomesa-metrics-micrometer/pom.xml @@ -12,10 +12,6 @@ GeoMesa Metrics Micrometer - - org.locationtech.geomesa - geomesa-utils_${scala.binary.version} - com.typesafe config @@ -28,6 +24,10 @@ io.micrometer micrometer-core + + com.typesafe.scala-logging + scala-logging_${scala.binary.version} + io.micrometer micrometer-registry-prometheus diff --git a/geomesa-metrics/geomesa-metrics-micrometer/src/main/scala/org/locationtech/geomesa/metrics/micrometer/prometheus/PrometheusFactory.scala b/geomesa-metrics/geomesa-metrics-micrometer/src/main/scala/org/locationtech/geomesa/metrics/micrometer/prometheus/PrometheusFactory.scala index 0f16cb38e6b9..545617d1b5b0 100644 --- a/geomesa-metrics/geomesa-metrics-micrometer/src/main/scala/org/locationtech/geomesa/metrics/micrometer/prometheus/PrometheusFactory.scala +++ b/geomesa-metrics/geomesa-metrics-micrometer/src/main/scala/org/locationtech/geomesa/metrics/micrometer/prometheus/PrometheusFactory.scala @@ -9,19 +9,20 @@ package org.locationtech.geomesa.metrics.micrometer package prometheus import com.typesafe.config.Config +import com.typesafe.scalalogging.LazyLogging import io.micrometer.core.instrument.{MeterRegistry, Tag} import io.micrometer.prometheusmetrics.{PrometheusMeterRegistry, PrometheusRenameFilter} import io.prometheus.metrics.exporter.httpserver.HTTPServer import io.prometheus.metrics.exporter.pushgateway.{Format, PushGateway, Scheme} -import org.locationtech.geomesa.utils.io.CloseWithLogging -import pureconfig.{ConfigReader, ConfigSource} import pureconfig.generic.semiauto.deriveReader +import pureconfig.{ConfigReader, ConfigSource} import java.io.Closeable import java.util.Locale import java.util.concurrent.atomic.AtomicReference +import scala.util.control.NonFatal -object PrometheusFactory extends RegistryFactory { +object PrometheusFactory extends RegistryFactory with LazyLogging { import scala.collection.JavaConverters._ @@ -33,7 +34,12 @@ object PrometheusFactory extends RegistryFactory { val dependentClose = new AtomicReference[Closeable]() val registry = new PrometheusMeterRegistry(k => config.properties.getOrElse(k, null)) { override def close(): Unit = { - CloseWithLogging(Option(dependentClose.get())) + val child = dependentClose.get() + if (child != null) { + try { child.close() } catch { + case NonFatal(e) => logger.error("Error on close:", e) + } + } super.close() } } diff --git a/geomesa-metrics/geomesa-metrics-micrometer/src/test/scala/org/locationtech/geomesa/metrics/micrometer/PrometheusReporterTest.scala b/geomesa-metrics/geomesa-metrics-micrometer/src/test/scala/org/locationtech/geomesa/metrics/micrometer/PrometheusReporterTest.scala index 65edc184c924..728b7f9880a8 100644 --- a/geomesa-metrics/geomesa-metrics-micrometer/src/test/scala/org/locationtech/geomesa/metrics/micrometer/PrometheusReporterTest.scala +++ b/geomesa-metrics/geomesa-metrics-micrometer/src/test/scala/org/locationtech/geomesa/metrics/micrometer/PrometheusReporterTest.scala @@ -9,10 +9,9 @@ package org.locationtech.geomesa.metrics.micrometer import com.typesafe.config.ConfigFactory +import io.micrometer.core.instrument.util.IOUtils import io.micrometer.prometheusmetrics.PrometheusMeterRegistry -import org.apache.commons.io.IOUtils import org.junit.runner.RunWith -import org.locationtech.geomesa.utils.io.WithClose import org.mortbay.jetty.handler.AbstractHandler import org.mortbay.jetty.{Request, Server} import org.specs2.mutable.Specification @@ -27,7 +26,12 @@ import scala.collection.mutable.ArrayBuffer @RunWith(classOf[JUnitRunner]) class PrometheusReporterTest extends Specification { - private def getFreePort: Int = WithClose(new ServerSocket(0))(_.getLocalPort) + private def getFreePort: Int = { + val socket = new ServerSocket(0) + try { socket.getLocalPort } finally { + socket.close() + } + } "Prometheus reporter" should { "expose metrics over http" in { @@ -40,12 +44,15 @@ class PrometheusReporterTest extends Specification { val metrics = ArrayBuffer.empty[String] val url = new URL(s"http://localhost:$port/metrics") - WithClose(new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) { is => - var line = is.readLine() + val reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) + try { + var line = reader.readLine() while (line != null) { metrics += line - line = is.readLine() + line = reader.readLine() } + } finally { + reader.close() } metrics must contain("foo_total 10.0") @@ -64,12 +71,15 @@ class PrometheusReporterTest extends Specification { val metrics = ArrayBuffer.empty[String] val url = new URL(s"http://localhost:$port/metrics") - WithClose(new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) { is => - var line = is.readLine() + val reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) + try { + var line = reader.readLine() while (line != null) { metrics += line - line = is.readLine() + line = reader.readLine() } + } finally { + reader.close() } metrics must contain("""foo_total{foo="bar"} 10.0""")