diff options
5 files changed, 38 insertions, 30 deletions
diff --git a/kamon-core/src/main/resources/reference.conf b/kamon-core/src/main/resources/reference.conf index 51a89a9e..125a1075 100644 --- a/kamon-core/src/main/resources/reference.conf +++ b/kamon-core/src/main/resources/reference.conf @@ -20,19 +20,6 @@ kamon { metric { tick-interval = 60 seconds - filters { - - # Determines whether entities from a category that doesn't have any filtering configuration should be tracked or - # not. E.g. If there are no filter sections for the "jdbc-datasource" category and `accept-unmatched-categories` - # is set to true, all entities for that category will be accepted, otherwise all will be rejected. - # - # NOTE: Using entity filters is a commodity for modules that might potentially track thousands of unnecessary - # entities, but not all modules are required to use filters, check the your module's documentation to - # determine whether setting up filters make sense or not. - accept-unmatched = true - - } - # Thread pool size used by the metrics refresh scheduler. This pool is only used to periodically sampling # min-max-counter values. refresh-scheduler-pool-size = 2 @@ -110,4 +97,19 @@ kamon { chance = 0.01 } } + + util { + filters { + + # Determines whether entities from a category that doesn't have any filtering configuration should be tracked or + # not. E.g. If there are no filter sections for the "jdbc-datasource" category and `accept-unmatched-categories` + # is set to true, all entities for that category will be accepted, otherwise all will be rejected. + # + # NOTE: Using entity filters is a commodity for modules that might potentially track thousands of unnecessary + # entities, but not all modules are required to use filters, check the your module's documentation to + # determine whether setting up filters make sense or not. + accept-unmatched = true + + } + } }
\ No newline at end of file diff --git a/kamon-core/src/main/scala/kamon/Kamon.scala b/kamon-core/src/main/scala/kamon/Kamon.scala index 892d1c12..d7864de5 100644 --- a/kamon-core/src/main/scala/kamon/Kamon.scala +++ b/kamon-core/src/main/scala/kamon/Kamon.scala @@ -20,7 +20,7 @@ import io.opentracing.propagation.Format import io.opentracing.{ActiveSpan, Span, SpanContext} import kamon.metric._ import kamon.trace.Tracer -import kamon.util.MeasurementUnit +import kamon.util.{Filters, MeasurementUnit} import scala.concurrent.Future import java.time.Duration @@ -31,6 +31,7 @@ import io.opentracing.ActiveSpan.Continuation object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Tracer { @volatile private var _config = ConfigFactory.load() @volatile private var _environment = Environment.fromConfig(_config) + @volatile private var _filters = Filters.fromConfig(_config) private val _metrics = new MetricRegistry(_config) private val _reporters = new ReporterRegistryImpl(_metrics, _config) @@ -45,6 +46,7 @@ object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Trac def reconfigure(config: Config): Unit = synchronized { _config = config _environment = Environment.fromConfig(config) + _filters = Filters.fromConfig(config) _metrics.reconfigure(config) _reporters.reconfigure(config) } @@ -153,4 +155,6 @@ object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Trac override def stopAllReporters(): Future[Unit] = _reporters.stopAllReporters() + def filter(filterName: String, pattern: String): Boolean = + _filters.accept(filterName, pattern) } diff --git a/kamon-core/src/main/scala/kamon/util/Filters.scala b/kamon-core/src/main/scala/kamon/util/Filters.scala index 3dbdc14d..78553309 100644 --- a/kamon-core/src/main/scala/kamon/util/Filters.scala +++ b/kamon-core/src/main/scala/kamon/util/Filters.scala @@ -20,8 +20,8 @@ import java.util.regex.Pattern import com.typesafe.config.Config -object Filter { - def fromConfig(config: Config): Filter = { +object Filters { + def fromConfig(config: Config): Filters = { val filtersConfig = config.getConfig("kamon.util.filters") val acceptUnmatched = filtersConfig.getBoolean("accept-unmatched") @@ -29,23 +29,23 @@ object Filter { val includes = readFilters(filtersConfig, s"$filterName.includes") val excludes = readFilters(filtersConfig, s"$filterName.excludes") - (filterName, new IncludeExcludeNameFilter(includes, excludes)) + (filterName, new IncludeExcludeMatcher(includes, excludes)) } toMap - new Filter(perMetricFilter, acceptUnmatched) + new Filters(perMetricFilter, acceptUnmatched) } - private def readFilters(filtersConfig: Config, name: String): Seq[NameFilter] = { + private def readFilters(filtersConfig: Config, name: String): Seq[Matcher] = { import scala.collection.JavaConverters._ if(filtersConfig.hasPath(name)) - filtersConfig.getStringList(name).asScala.map(readNameFilter) + filtersConfig.getStringList(name).asScala.map(readMatcher) else Seq.empty } - private def readNameFilter(pattern: String): NameFilter = { + private def readMatcher(pattern: String): Matcher = { if(pattern.startsWith("regex:")) - new RegexNameFilter(pattern.drop(6)) + new RegexMatcher(pattern.drop(6)) else if(pattern.startsWith("glob:")) new GlobPathFilter(pattern.drop(5)) else @@ -53,7 +53,7 @@ object Filter { } } -class Filter(filters: Map[String, NameFilter], acceptUnmatched: Boolean) { +class Filters(filters: Map[String, Matcher], acceptUnmatched: Boolean) { def accept(filterName: String, pattern: String): Boolean = filters .get(filterName) @@ -61,16 +61,16 @@ class Filter(filters: Map[String, NameFilter], acceptUnmatched: Boolean) { .getOrElse(acceptUnmatched) } -trait NameFilter { +trait Matcher { def accept(name: String): Boolean } -class IncludeExcludeNameFilter(includes: Seq[NameFilter], excludes: Seq[NameFilter]) extends NameFilter { +class IncludeExcludeMatcher(includes: Seq[Matcher], excludes: Seq[Matcher]) extends Matcher { override def accept(name: String): Boolean = includes.exists(_.accept(name)) && !excludes.exists(_.accept(name)) } -class RegexNameFilter(pattern: String) extends NameFilter { +class RegexMatcher(pattern: String) extends Matcher { private val pathRegex = pattern.r override def accept(name: String): Boolean = name match { @@ -79,7 +79,7 @@ class RegexNameFilter(pattern: String) extends NameFilter { } } -class GlobPathFilter(glob: String) extends NameFilter { +class GlobPathFilter(glob: String) extends Matcher { private val globPattern = Pattern.compile("(\\*\\*?)|(\\?)|(\\\\.)|(/+)|([^*?]+)") private val compiledPattern = getGlobPattern(glob) diff --git a/kamon-core/src/test/scala/kamon/metric/GlobPathFilterSpec.scala b/kamon-core/src/test/scala/kamon/metric/GlobPathFilterSpec.scala index cb2a5d5f..c21b1256 100644 --- a/kamon-core/src/test/scala/kamon/metric/GlobPathFilterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/GlobPathFilterSpec.scala @@ -17,6 +17,7 @@ package kamon package metric +import kamon.util.GlobPathFilter import org.scalatest.{Matchers, WordSpecLike} class GlobPathFilterSpec extends WordSpecLike with Matchers { diff --git a/kamon-core/src/test/scala/kamon/metric/RegexPathFilterSpec.scala b/kamon-core/src/test/scala/kamon/metric/RegexPathFilterSpec.scala index da095b1b..f742df1d 100644 --- a/kamon-core/src/test/scala/kamon/metric/RegexPathFilterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/RegexPathFilterSpec.scala @@ -17,13 +17,14 @@ package kamon package metric +import kamon.util.RegexMatcher import org.scalatest.{Matchers, WordSpecLike} class RegexPathFilterSpec extends WordSpecLike with Matchers { "The RegexPathFilter" should { "match a single expression" in { - val filter = new RegexNameFilter("/user/actor") + val filter = new RegexMatcher("/user/actor") filter.accept("/user/actor") shouldBe true @@ -32,7 +33,7 @@ class RegexPathFilterSpec extends WordSpecLike with Matchers { } "match arbitray expressions ending with wildcard" in { - val filter = new RegexNameFilter("/user/.*") + val filter = new RegexMatcher("/user/.*") filter.accept("/user/actor") shouldBe true filter.accept("/user/otherActor") shouldBe true @@ -46,7 +47,7 @@ class RegexPathFilterSpec extends WordSpecLike with Matchers { } "match numbers" in { - val filter = new RegexNameFilter("/user/actor-\\d") + val filter = new RegexMatcher("/user/actor-\\d") filter.accept("/user/actor-1") shouldBe true filter.accept("/user/actor-2") shouldBe true |