1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/* =========================================================================================
* 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 metric
import java.util.regex.Pattern
import com.typesafe.config.Config
object Filter {
def fromConfig(config: Config): Filter = {
val filtersConfig = config.getConfig("kamon.metric.filters")
val acceptUnmatched = filtersConfig.getBoolean("accept-unmatched")
val perMetricFilter = filtersConfig.firstLevelKeys.filter(_ != "accept-unmatched") map { metricName: String ⇒
val includes = readFilters(filtersConfig, s"$metricName.includes")
val excludes = readFilters(filtersConfig, s"$metricName.excludes")
(metricName, 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(perMetricFilter: Map[String, NameFilter], acceptUnmatched: Boolean) {
def accept(metricName: String, pattern: String): Boolean =
perMetricFilter
.get(metricName)
.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)
}
}
|