diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2017-06-16 00:06:00 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2017-06-16 00:06:00 +0200 |
commit | dd645df1b7c462418c01074d0e137982d2f270b7 (patch) | |
tree | 39f4bad09cfef315da2fd2cb425e6f55cacf6055 /kamon-core/src | |
parent | bc481389e427c83069b092e24200dbe960aaeb67 (diff) | |
download | Kamon-dd645df1b7c462418c01074d0e137982d2f270b7.tar.gz Kamon-dd645df1b7c462418c01074d0e137982d2f270b7.tar.bz2 Kamon-dd645df1b7c462418c01074d0e137982d2f270b7.zip |
handle filters with quoted names
Diffstat (limited to 'kamon-core/src')
-rw-r--r-- | kamon-core/src/main/scala/kamon/util/Filters.scala | 23 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/metric/FilterSpec.scala | 127 |
2 files changed, 77 insertions, 73 deletions
diff --git a/kamon-core/src/main/scala/kamon/util/Filters.scala b/kamon-core/src/main/scala/kamon/util/Filters.scala index ab505ca7..9f015e99 100644 --- a/kamon-core/src/main/scala/kamon/util/Filters.scala +++ b/kamon-core/src/main/scala/kamon/util/Filters.scala @@ -18,27 +18,28 @@ package util import java.util.regex.Pattern -import com.typesafe.config.Config +import com.typesafe.config.{Config, ConfigUtil} object Filters { def fromConfig(config: Config): Filters = { val filtersConfig = config.getConfig("kamon.util.filters") - val acceptUnmatched = filtersConfig.getBoolean("accept-unmatched") - val perMetricFilter = filtersConfig.topLevelKeys.filter(_ != "accept-unmatched") map { filterName: String ⇒ - val includes = readFilters(filtersConfig, s"$filterName.includes") - val excludes = readFilters(filtersConfig, s"$filterName.excludes") + val perMetricFilter = filtersConfig.topLevelKeys map { filterName: String ⇒ + val includes = readFilters(filtersConfig, filterName, "includes") + val excludes = readFilters(filtersConfig, filterName, "excludes") (filterName, new IncludeExcludeMatcher(includes, excludes)) } toMap - new Filters(perMetricFilter, acceptUnmatched) + new Filters(perMetricFilter) } - private def readFilters(filtersConfig: Config, name: String): Seq[Matcher] = { + private def readFilters(filtersConfig: Config, name: String, key: String): Seq[Matcher] = { import scala.collection.JavaConverters._ - if(filtersConfig.hasPath(name)) - filtersConfig.getStringList(name).asScala.map(readMatcher) + val configKey = ConfigUtil.joinPath(name, key) + + if(filtersConfig.hasPath(configKey)) + filtersConfig.getStringList(configKey).asScala.map(readMatcher) else Seq.empty } @@ -53,12 +54,12 @@ object Filters { } } -class Filters(filters: Map[String, Matcher], acceptUnmatched: Boolean) { +class Filters(filters: Map[String, Matcher]) { def accept(filterName: String, pattern: String): Boolean = filters .get(filterName) .map(_.accept(pattern)) - .getOrElse(acceptUnmatched) + .getOrElse(false) } trait Matcher { diff --git a/kamon-core/src/test/scala/kamon/metric/FilterSpec.scala b/kamon-core/src/test/scala/kamon/metric/FilterSpec.scala index c0f0e2e1..cda76dc2 100644 --- a/kamon-core/src/test/scala/kamon/metric/FilterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/FilterSpec.scala @@ -5,65 +5,68 @@ import com.typesafe.config.ConfigFactory import org.scalatest.{Matchers, WordSpec} -//class FilterSpec extends WordSpec with Matchers { -// val testConfig = ConfigFactory.parseString( -// """ -// |kamon.metric.filters { -// | accept-unmatched-categories = false -// | -// | some-category { -// | includes = ["**"] -// | excludes = ["not-me"] -// | } -// | -// | only-includes { -// | includes = ["only-me"] -// | } -// | -// | only-excludes { -// | excludes = ["not-me"] -// | } -// | -// | specific-rules { -// | includes = ["glob:/user/**", "regex:test-[0-5]"] -// | } -// |} -// """.stripMargin -// ) -// -// "the entity filters" should { -// "use the accept-unmatched-categories setting when there is no configuration for a given category" in { -// val acceptUnmatched = Filter.fromConfig(ConfigFactory.parseString("kamon.metric.filters.accept-unmatched-categories=true")) -// val rejectUnmatched = Filter.fromConfig(ConfigFactory.parseString("kamon.metric.filters.accept-unmatched-categories=false")) -// -// acceptUnmatched.accept(Entity("a", "b", Map.empty)) shouldBe true -// rejectUnmatched.accept(Entity("a", "b", Map.empty)) shouldBe false -// } -// -// "accept entities that are matched by any include and none exclude filters" in { -// val entityFilter = Filter.fromConfig(testConfig) -// -// entityFilter.accept(Entity("anything", "anything", Map.empty)) shouldBe false -// entityFilter.accept(Entity("anything", "some-category", Map.empty)) shouldBe true -// entityFilter.accept(Entity("not-me", "some-category", Map.empty)) shouldBe false -// } -// -// "allow configuring includes only or excludes only for any category" in { -// val entityFilter = Filter.fromConfig(testConfig) -// -// entityFilter.accept(Entity("only-me", "only-includes", Map.empty)) shouldBe true -// entityFilter.accept(Entity("anything", "only-includes", Map.empty)) shouldBe false -// entityFilter.accept(Entity("any-other", "only-excludes", Map.empty)) shouldBe false -// entityFilter.accept(Entity("not-me", "only-excludes", Map.empty)) shouldBe false -// } -// -// "allow to explicitly decide whether patterns are treated as Glob or Regex" in { -// val entityFilter = Filter.fromConfig(testConfig) -// -// entityFilter.accept(Entity("/user/accepted", "specific-rules", Map.empty)) shouldBe true -// entityFilter.accept(Entity("/other/rejected/", "specific-rules", Map.empty)) shouldBe false -// entityFilter.accept(Entity("test-5", "specific-rules", Map.empty)) shouldBe true -// entityFilter.accept(Entity("test-6", "specific-rules", Map.empty)) shouldBe false -// } -// } -//}
\ No newline at end of file +class FilterSpec extends WordSpec with Matchers { + val testConfig = ConfigFactory.parseString( + """ + |kamon.util.filters { + | + | some-filter { + | includes = ["**"] + | excludes = ["not-me"] + | } + | + | only-includes { + | includes = ["only-me"] + | } + | + | only-excludes { + | excludes = ["not-me"] + | } + | + | specific-rules { + | includes = ["glob:/user/**", "regex:test-[0-5]"] + | } + | + | "filter.with.quotes" { + | includes = ["**"] + | excludes = ["not-me"] + | } + |} + """.stripMargin + ) + + Kamon.reconfigure(testConfig.withFallback(Kamon.config())) + + "the entity filters" should { + "reject anything that doesn't match any configured filter" in { + Kamon.filter("not-a-filter", "hello") shouldBe false + } + + "evaluate patterns for filters with includes and excludes" in { + Kamon.filter("some-filter", "anything") shouldBe true + Kamon.filter("some-filter", "some-other") shouldBe true + Kamon.filter("some-filter", "not-me") shouldBe false + } + + "allow configuring includes only or excludes only for any filter" in { + Kamon.filter("only-includes", "only-me") shouldBe true + Kamon.filter("only-includes", "anything") shouldBe false + Kamon.filter("only-excludes", "any-other") shouldBe false + Kamon.filter("only-excludes", "not-me") shouldBe false + } + + "allow to explicitly decide whether patterns are treated as Glob or Regex" in { + Kamon.filter("specific-rules", "/user/accepted") shouldBe true + Kamon.filter("specific-rules", "/other/rejected/") shouldBe false + Kamon.filter("specific-rules", "test-5") shouldBe true + Kamon.filter("specific-rules", "test-6") shouldBe false + } + + "allow filters with quoted names" in { + Kamon.filter("filter.with.quotes", "anything") shouldBe true + Kamon.filter("filter.with.quotes", "some-other") shouldBe true + Kamon.filter("filter.with.quotes", "not-me") shouldBe false + } + + } +} |