Skip to content

Commit

Permalink
GEOMESA-3400 Micrometer - remove geomesa-utils dependency (#3210)
Browse files Browse the repository at this point in the history
  • Loading branch information
elahrvivaz authored Oct 7, 2024
1 parent f685295 commit e966d4d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
8 changes: 4 additions & 4 deletions geomesa-metrics/geomesa-metrics-micrometer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<name>GeoMesa Metrics Micrometer</name>

<dependencies>
<dependency>
<groupId>org.locationtech.geomesa</groupId>
<artifactId>geomesa-utils_${scala.binary.version}</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
Expand All @@ -28,6 +24,10 @@
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe.scala-logging</groupId>
<artifactId>scala-logging_${scala.binary.version}</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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""")
Expand Down

0 comments on commit e966d4d

Please sign in to comment.