aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/util
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2017-06-14 15:01:23 +0200
committerIvan Topolnjak <ivantopo@gmail.com>2017-06-14 15:01:23 +0200
commit8e6c6737f97314d1cacf6d6ad50a984ad21438ca (patch)
tree66d2492e3c95fba4523797c90d9bdadfd1afbbe2 /kamon-core/src/main/scala/kamon/util
parent148d2de7157244332314e8d5c29e5e84dff57dd2 (diff)
downloadKamon-8e6c6737f97314d1cacf6d6ad50a984ad21438ca.tar.gz
Kamon-8e6c6737f97314d1cacf6d6ad50a984ad21438ca.tar.bz2
Kamon-8e6c6737f97314d1cacf6d6ad50a984ad21438ca.zip
move filters to util package, expose them from Kamon
Diffstat (limited to 'kamon-core/src/main/scala/kamon/util')
-rw-r--r--kamon-core/src/main/scala/kamon/util/Filters.scala126
1 files changed, 126 insertions, 0 deletions
diff --git a/kamon-core/src/main/scala/kamon/util/Filters.scala b/kamon-core/src/main/scala/kamon/util/Filters.scala
new file mode 100644
index 00000000..3dbdc14d
--- /dev/null
+++ b/kamon-core/src/main/scala/kamon/util/Filters.scala
@@ -0,0 +1,126 @@
+/* =========================================================================================
+ * Copyright © 2013-2017 the kamon project <http://kamon.io/>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language governing permissions
+ * and limitations under the License.
+ * =========================================================================================
+ */
+
+package kamon
+package util
+
+import java.util.regex.Pattern
+
+import com.typesafe.config.Config
+
+object Filter {
+ def fromConfig(config: Config): Filter = {
+ val filtersConfig = config.getConfig("kamon.util.filters")
+ val acceptUnmatched = filtersConfig.getBoolean("accept-unmatched")
+
+ val perMetricFilter = filtersConfig.firstLevelKeys.filter(_ != "accept-unmatched") map { filterName: String ⇒
+ val includes = readFilters(filtersConfig, s"$filterName.includes")
+ val excludes = readFilters(filtersConfig, s"$filterName.excludes")
+
+ (filterName, new IncludeExcludeNameFilter(includes, excludes))
+ } toMap
+
+ new Filter(perMetricFilter, acceptUnmatched)
+ }
+
+ private def readFilters(filtersConfig: Config, name: String): Seq[NameFilter] = {
+ import scala.collection.JavaConverters._
+ if(filtersConfig.hasPath(name))
+ filtersConfig.getStringList(name).asScala.map(readNameFilter)
+ else
+ Seq.empty
+ }
+
+ private def readNameFilter(pattern: String): NameFilter = {
+ if(pattern.startsWith("regex:"))
+ new RegexNameFilter(pattern.drop(6))
+ else if(pattern.startsWith("glob:"))
+ new GlobPathFilter(pattern.drop(5))
+ else
+ new GlobPathFilter(pattern)
+ }
+}
+
+class Filter(filters: Map[String, NameFilter], acceptUnmatched: Boolean) {
+ def accept(filterName: String, pattern: String): Boolean =
+ filters
+ .get(filterName)
+ .map(_.accept(pattern))
+ .getOrElse(acceptUnmatched)
+}
+
+trait NameFilter {
+ def accept(name: String): Boolean
+}
+
+class IncludeExcludeNameFilter(includes: Seq[NameFilter], excludes: Seq[NameFilter]) extends NameFilter {
+ override def accept(name: String): Boolean =
+ includes.exists(_.accept(name)) && !excludes.exists(_.accept(name))
+}
+
+class RegexNameFilter(pattern: String) extends NameFilter {
+ private val pathRegex = pattern.r
+
+ override def accept(name: String): Boolean = name match {
+ case pathRegex(_*) ⇒ true
+ case _ ⇒ false
+ }
+}
+
+class GlobPathFilter(glob: String) extends NameFilter {
+ private val globPattern = Pattern.compile("(\\*\\*?)|(\\?)|(\\\\.)|(/+)|([^*?]+)")
+ private val compiledPattern = getGlobPattern(glob)
+
+ override def accept(name: String): Boolean =
+ compiledPattern.matcher(name).matches()
+
+ private def getGlobPattern(glob: String) = {
+ val patternBuilder = new StringBuilder
+ val matcher = globPattern.matcher(glob)
+ while (matcher.find()) {
+ val (grp1, grp2, grp3, grp4) = (matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4))
+ if (grp1 != null) {
+ // match a * or **
+ if (grp1.length == 2) {
+ // it's a *workers are able to process multiple metrics*
+ patternBuilder.append(".*")
+ }
+ else {
+ // it's a *
+ patternBuilder.append("[^/]*")
+ }
+ }
+ else if (grp2 != null) {
+ // match a '?' glob pattern; any non-slash character
+ patternBuilder.append("[^/]")
+ }
+ else if (grp3 != null) {
+ // backslash-escaped value
+ patternBuilder.append(Pattern.quote(grp3.substring(1)))
+ }
+ else if (grp4 != null) {
+ // match any number of / chars
+ patternBuilder.append("/+")
+ }
+ else {
+ // some other string
+ patternBuilder.append(Pattern.quote(matcher.group))
+ }
+ }
+
+ Pattern.compile(patternBuilder.toString)
+ }
+}
+