-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GEOMESA-3400 Configurable Micrometer metrics - separate classloading (#…
…3205) * Permits using metrics without unused dependencies on the classpath
- Loading branch information
1 parent
c4b9efe
commit 95a360e
Showing
4 changed files
with
134 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...crometer/src/main/scala/org/locationtech/geomesa/metrics/micrometer/RegistryFactory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/*********************************************************************** | ||
* Copyright (c) 2013-2024 Commonwealth Computer Research, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Apache License, Version 2.0 | ||
* which accompanies this distribution and is available at | ||
* http://www.opensource.org/licenses/apache2.0.php. | ||
***********************************************************************/ | ||
|
||
package org.locationtech.geomesa.metrics.micrometer | ||
|
||
import com.typesafe.config.Config | ||
import io.micrometer.core.instrument.MeterRegistry | ||
|
||
trait RegistryFactory { | ||
def apply(conf: Config): MeterRegistry | ||
} |
30 changes: 30 additions & 0 deletions
30
...main/scala/org/locationtech/geomesa/metrics/micrometer/cloudwatch/CloudwatchFactory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*********************************************************************** | ||
* Copyright (c) 2013-2024 Commonwealth Computer Research, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Apache License, Version 2.0 | ||
* which accompanies this distribution and is available at | ||
* http://www.opensource.org/licenses/apache2.0.php. | ||
***********************************************************************/ | ||
|
||
package org.locationtech.geomesa.metrics.micrometer | ||
package cloudwatch | ||
import com.typesafe.config.Config | ||
import io.micrometer.cloudwatch2.CloudWatchMeterRegistry | ||
import io.micrometer.core.instrument.{Clock, MeterRegistry} | ||
import pureconfig.generic.semiauto.deriveReader | ||
import pureconfig.{ConfigReader, ConfigSource} | ||
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient | ||
|
||
object CloudwatchFactory extends RegistryFactory { | ||
|
||
override def apply(conf: Config): MeterRegistry = { | ||
implicit val reader: ConfigReader[CloudwatchConfig] = deriveReader[CloudwatchConfig] | ||
val config = ConfigSource.fromConfig(conf).loadOrThrow[CloudwatchConfig] | ||
new CloudWatchMeterRegistry(k => config.properties.getOrElse(k, null), Clock.SYSTEM, CloudWatchAsyncClient.create()) | ||
} | ||
|
||
private case class CloudwatchConfig( | ||
namespace: String = "geomesa", | ||
properties: Map[String, String] = Map.empty | ||
) | ||
} |
83 changes: 83 additions & 0 deletions
83
...main/scala/org/locationtech/geomesa/metrics/micrometer/prometheus/PrometheusFactory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/*********************************************************************** | ||
* Copyright (c) 2013-2024 Commonwealth Computer Research, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Apache License, Version 2.0 | ||
* which accompanies this distribution and is available at | ||
* http://www.opensource.org/licenses/apache2.0.php. | ||
***********************************************************************/ | ||
|
||
package org.locationtech.geomesa.metrics.micrometer | ||
package prometheus | ||
import com.typesafe.config.Config | ||
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 java.io.Closeable | ||
import java.util.Locale | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
object PrometheusFactory extends RegistryFactory { | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
override def apply(conf: Config): MeterRegistry = { | ||
// noinspection ScalaUnusedSymbol | ||
implicit val gatewayReader: ConfigReader[PushGatewayConfig] = deriveReader[PushGatewayConfig] | ||
implicit val prometheusReader: ConfigReader[PrometheusConfig] = deriveReader[PrometheusConfig] | ||
val config = ConfigSource.fromConfig(conf).loadOrThrow[PrometheusConfig] | ||
val dependentClose = new AtomicReference[Closeable]() | ||
val registry = new PrometheusMeterRegistry(k => config.properties.getOrElse(k, null)) { | ||
override def close(): Unit = { | ||
CloseWithLogging(Option(dependentClose.get())) | ||
super.close() | ||
} | ||
} | ||
registry.throwExceptionOnRegistrationFailure() | ||
if (config.rename) { | ||
registry.config().meterFilter(new PrometheusRenameFilter()) | ||
} | ||
if (config.commonTags.nonEmpty) { | ||
val tags = config.commonTags.map { case (k, v) => Tag.of(k, v) } | ||
registry.config.commonTags(tags.asJava) | ||
} | ||
config.pushGateway match { | ||
case None => | ||
val server = | ||
HTTPServer.builder() | ||
.port(config.port) | ||
.registry(registry.getPrometheusRegistry) | ||
.buildAndStart() | ||
dependentClose.set(server) | ||
|
||
case Some(pg) => | ||
val builder = PushGateway.builder().registry(registry.getPrometheusRegistry).address(pg.host) | ||
pg.job.foreach(builder.job) | ||
pg.format.foreach(v => builder.format(Format.valueOf(v.toUpperCase(Locale.US)))) | ||
pg.scheme.foreach(v => builder.scheme(Scheme.fromString(v.toLowerCase(Locale.US)))) | ||
val pushGateway = builder.build() | ||
dependentClose.set(() => pushGateway.pushAdd()) | ||
} | ||
registry | ||
} | ||
|
||
private case class PrometheusConfig( | ||
rename: Boolean = false, | ||
commonTags: Map[String, String] = Map.empty, | ||
port: Int = 9090, | ||
// additional config can also be done via sys props - see https://prometheus.github.io/client_java/config/config/ | ||
properties: Map[String, String] = Map.empty, | ||
pushGateway: Option[PushGatewayConfig], | ||
) | ||
|
||
private case class PushGatewayConfig( | ||
host: String, | ||
scheme: Option[String], | ||
job: Option[String], | ||
format: Option[String], | ||
) | ||
} |