Skip to content

Commit

Permalink
feat: Add subproject micrometer with generic PrefixMeterFilter (#219)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jakubjanecek authored Apr 22, 2020
1 parent 4ac6d75 commit eff6bac
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
13 changes: 13 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ lazy val root = project
jvm,
jvmMicrometer,
jvmPureConfig,
micrometer,
micrometerJmx,
micrometerJmxPureConfig,
micrometerStatsD,
Expand Down Expand Up @@ -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)
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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}

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

}
Original file line number Diff line number Diff line change
@@ -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")
}

}

0 comments on commit eff6bac

Please sign in to comment.