From eff6bac3c96f26a6df395e543b48608e0378afde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jane=C4=8Dek?= Date: Wed, 22 Apr 2020 16:43:24 +0200 Subject: [PATCH] feat: Add subproject micrometer with generic PrefixMeterFilter (#219) * feat: Add subproject micrometer with generic PrefixMeterFilter feat: Add more configuration options to MicrometerStatsDModule fix: prefix was not what it was meant to be * fix: compilation issue --- build.sbt | 13 ++++++++++ .../statsd/MicrometerStatsDConfig.scala | 3 ++- .../statsd/MicrometerStatsDModule.scala | 26 ++++++++++++++++--- .../sst/micrometer/PrefixMeterFilter.scala | 10 +++++++ .../micrometer/PrefixMeterFilterTest.scala | 16 ++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 micrometer/src/main/scala/com/avast/sst/micrometer/PrefixMeterFilter.scala create mode 100644 micrometer/src/test/scala/com/avast/sst/micrometer/PrefixMeterFilterTest.scala diff --git a/build.sbt b/build.sbt index 2f0db0bc9..fc9e36dcc 100644 --- a/build.sbt +++ b/build.sbt @@ -36,6 +36,7 @@ lazy val root = project jvm, jvmMicrometer, jvmPureConfig, + micrometer, micrometerJmx, micrometerJmxPureConfig, micrometerStatsD, @@ -296,6 +297,17 @@ lazy val jvmPureConfig = project libraryDependencies += Dependencies.pureConfig ) +lazy val micrometer = project + .in(file("micrometer")) + .settings(BuildSettings.common) + .settings( + name := "sst-micrometer", + libraryDependencies ++= Seq( + Dependencies.micrometerCore, + Dependencies.jsr305 // required because of Scala compiler + ) + ) + lazy val micrometerJmx = project .in(file("micrometer-jmx")) .settings(BuildSettings.common) @@ -318,6 +330,7 @@ lazy val micrometerJmxPureConfig = project lazy val micrometerStatsD = project .in(file("micrometer-statsd")) + .dependsOn(micrometer) .settings(BuildSettings.common) .settings( name := "sst-micrometer-statsd", diff --git a/micrometer-statsd/src/main/scala/com/avast/sst/micrometer/statsd/MicrometerStatsDConfig.scala b/micrometer-statsd/src/main/scala/com/avast/sst/micrometer/statsd/MicrometerStatsDConfig.scala index dafd4d234..05f082b48 100644 --- a/micrometer-statsd/src/main/scala/com/avast/sst/micrometer/statsd/MicrometerStatsDConfig.scala +++ b/micrometer-statsd/src/main/scala/com/avast/sst/micrometer/statsd/MicrometerStatsDConfig.scala @@ -17,5 +17,6 @@ final case class MicrometerStatsDConfig( step: Duration = Duration(1, TimeUnit.MINUTES), publishUnchangedMeters: Boolean = true, buffered: Boolean = true, - prefix: String = "statsd" + prefix: String = "", + commonTags: Map[String, String] = Map.empty ) diff --git a/micrometer-statsd/src/main/scala/com/avast/sst/micrometer/statsd/MicrometerStatsDModule.scala b/micrometer-statsd/src/main/scala/com/avast/sst/micrometer/statsd/MicrometerStatsDModule.scala index 06162f72b..fac1bcc5a 100644 --- a/micrometer-statsd/src/main/scala/com/avast/sst/micrometer/statsd/MicrometerStatsDModule.scala +++ b/micrometer-statsd/src/main/scala/com/avast/sst/micrometer/statsd/MicrometerStatsDModule.scala @@ -3,7 +3,9 @@ package com.avast.sst.micrometer.statsd import java.time.Duration import cats.effect.{Resource, Sync} +import com.avast.sst.micrometer.PrefixMeterFilter import io.micrometer.core.instrument.Clock +import io.micrometer.core.instrument.config.{MeterFilter, NamingConvention} import io.micrometer.core.instrument.util.HierarchicalNameMapper import io.micrometer.statsd.{StatsdConfig, StatsdFlavor, StatsdMeterRegistry, StatsdProtocol} @@ -13,24 +15,40 @@ object MicrometerStatsDModule { def make[F[_]: Sync]( config: MicrometerStatsDConfig, clock: Clock = Clock.SYSTEM, - nameMapper: HierarchicalNameMapper = HierarchicalNameMapper.DEFAULT + nameMapper: HierarchicalNameMapper = HierarchicalNameMapper.DEFAULT, + namingConvention: Option[NamingConvention] = None, + meterFilter: Option[MeterFilter] = None ): Resource[F, StatsdMeterRegistry] = { Resource .make { Sync[F].delay { - StatsdMeterRegistry + val registry = StatsdMeterRegistry .builder(new CustomStatsdConfig(config)) .clock(clock) .nameMapper(nameMapper) .build + + namingConvention.foreach(registry.config().namingConvention) + + if (config.prefix.nonEmpty) { + registry.config().meterFilter(new PrefixMeterFilter(config.prefix)) + } + + meterFilter.foreach(registry.config().meterFilter) + + val preprocessedTags = config.commonTags.foldRight(List.empty[String]) { + case (tag, acc) => + tag._1 :: tag._2 :: acc + } + registry.config().commonTags(preprocessedTags: _*) + + registry } }(registry => Sync[F].delay(registry.close())) } private class CustomStatsdConfig(c: MicrometerStatsDConfig) extends StatsdConfig { - override val prefix: String = c.prefix - override val flavor: StatsdFlavor = c.flavor override val enabled: Boolean = c.enabled diff --git a/micrometer/src/main/scala/com/avast/sst/micrometer/PrefixMeterFilter.scala b/micrometer/src/main/scala/com/avast/sst/micrometer/PrefixMeterFilter.scala new file mode 100644 index 000000000..5fc9f6975 --- /dev/null +++ b/micrometer/src/main/scala/com/avast/sst/micrometer/PrefixMeterFilter.scala @@ -0,0 +1,10 @@ +package com.avast.sst.micrometer + +import io.micrometer.core.instrument.Meter +import io.micrometer.core.instrument.config.MeterFilter + +class PrefixMeterFilter(prefix: String) extends MeterFilter { + + override def map(id: Meter.Id): Meter.Id = id.withName(s"$prefix${id.getName}") + +} diff --git a/micrometer/src/test/scala/com/avast/sst/micrometer/PrefixMeterFilterTest.scala b/micrometer/src/test/scala/com/avast/sst/micrometer/PrefixMeterFilterTest.scala new file mode 100644 index 000000000..c47ec01c6 --- /dev/null +++ b/micrometer/src/test/scala/com/avast/sst/micrometer/PrefixMeterFilterTest.scala @@ -0,0 +1,16 @@ +package com.avast.sst.micrometer + +import io.micrometer.core.instrument.simple.SimpleMeterRegistry +import org.scalatest.funsuite.AnyFunSuite + +class PrefixMeterFilterTest extends AnyFunSuite { + + test("prefixing") { + val registry = new SimpleMeterRegistry + registry.config().meterFilter(new PrefixMeterFilter("this.is.prefix.")) + val counter = registry.counter("test") + + assert(counter.getId.getName === "this.is.prefix.test") + } + +}