diff options
author | Diego <diegolparra@gmail.com> | 2014-12-21 14:05:44 -0300 |
---|---|---|
committer | Diego <diegolparra@gmail.com> | 2014-12-24 18:58:12 -0300 |
commit | 5635149af76a85b167ca6f6ad45767bc45da31ba (patch) | |
tree | 650e0bae447016fa74f3e7681f1f61bea60b3cc1 | |
parent | 8e62230ad7250b522997dc910aa46de5ce99768a (diff) | |
download | Kamon-5635149af76a85b167ca6f6ad45767bc45da31ba.tar.gz Kamon-5635149af76a85b167ca6f6ad45767bc45da31ba.tar.bz2 Kamon-5635149af76a85b167ca6f6ad45767bc45da31ba.zip |
+ core: introduce simplified version of GlobPathFilter and resolve exact actor metric filter issue
5 files changed, 89 insertions, 146 deletions
diff --git a/kamon-core/src/main/java/kamon/util/Example.java b/kamon-core/src/main/java/kamon/util/Example.java deleted file mode 100644 index a5031182..00000000 --- a/kamon-core/src/main/java/kamon/util/Example.java +++ /dev/null @@ -1,8 +0,0 @@ -package kamon.util; - -public class Example { - - public static void main(String args[]) { - - } -} diff --git a/kamon-core/src/main/java/kamon/util/GlobPathFilter.java b/kamon-core/src/main/java/kamon/util/GlobPathFilter.java deleted file mode 100644 index a000e2a0..00000000 --- a/kamon-core/src/main/java/kamon/util/GlobPathFilter.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * ========================================================================================= - * Copyright 2013 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. - * ========================================================================================= - */ - -// This file was copied from: https://github.com/jboss-modules/jboss-modules/blob/master/src/main/java/org/jboss/modules/filter/GlobPathFilter.java -package kamon.util; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** -* Default implementation of PathFilter. Uses glob based includes and excludes to determine whether to export. -* -* @author John E. Bailey -* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a> -*/ -public final class GlobPathFilter { - private static final Pattern GLOB_PATTERN = Pattern.compile("(\\*\\*?)|(\\?)|(\\\\.)|(/+)|([^*?]+)"); - - private final String glob; - private final Pattern pattern; - - /** -* Construct a new instance. -* -* @param glob the path glob to match -*/ - public GlobPathFilter(final String glob) { - pattern = getGlobPattern(glob); - this.glob = glob; - } - - /** -* Determine whether a path should be accepted. -* -* @param path the path to check -* @return true if the path should be accepted, false if not -*/ - public boolean accept(final String path) { - return pattern.matcher(path).matches(); - } - - /** - * Get a regular expression pattern which accept any path names which match the given glob. The glob patterns - * function similarly to {@code ant} file patterns. Valid metacharacters in the glob pattern include: - * <ul> - * <li><code>"\"</code> - escape the next character (treat it literally, even if it is itself a recognized metacharacter)</li> - * <li><code>"?"</code> - match any non-slash character</li> - * <li><code>"*"</code> - match zero or more non-slash characters</li> - * <li><code>"**"</code> - match zero or more characters, including slashes</li> - * <li><code>"/"</code> - match one or more slash characters. Consecutive {@code /} characters are collapsed down into one.</li> - * </ul> - * In addition, any glob pattern matches all subdirectories thereof. A glob pattern ending in {@code /} is equivalent - * to a glob pattern ending in <code>/**</code> in that the named directory is not itself included in the glob. - * <p/> - * <b>See also:</b> <a href="http://ant.apache.org/manual/dirtasks.html#patterns">"Patterns" in the Ant Manual</a> - * - * @param glob the glob to match - * - * @return the pattern - */ - private static Pattern getGlobPattern(final String glob) { - StringBuilder patternBuilder = new StringBuilder(); - final Matcher m = GLOB_PATTERN.matcher(glob); - boolean lastWasSlash = false; - while (m.find()) { - lastWasSlash = false; - String grp; - if ((grp = m.group(1)) != null) { - // match a * or ** - if (grp.length() == 2) { - // it's a ** - patternBuilder.append(".*"); - } else { - // it's a * - patternBuilder.append("[^/]*"); - } - } else if ((grp = m.group(2)) != null) { - // match a '?' glob pattern; any non-slash character - patternBuilder.append("[^/]"); - } else if ((grp = m.group(3)) != null) { - // backslash-escaped value - patternBuilder.append(Pattern.quote(m.group().substring(1))); - } else if ((grp = m.group(4)) != null) { - // match any number of / chars - patternBuilder.append("/+"); - lastWasSlash = true; - } else { - // some other string - patternBuilder.append(Pattern.quote(m.group())); - } - } - if (lastWasSlash) { - // ends in /, append ** - patternBuilder.append(".*"); - } else { - patternBuilder.append("(?:/.*)?"); - } - return Pattern.compile(patternBuilder.toString()); - } - - public int hashCode() { - return glob.hashCode() + 13; - } - - public boolean equals(final Object obj) { - return obj instanceof GlobPathFilter && equals((GlobPathFilter) obj); - } - - public boolean equals(final GlobPathFilter obj) { - return obj != null && obj.pattern.equals(pattern); - } - - public String toString() { - final StringBuilder b = new StringBuilder(); - b.append("match "); - if (glob != null) { - b.append('"').append(glob).append('"'); - } else { - b.append('/').append(pattern).append('/'); - } - return b.toString(); - } -}
\ No newline at end of file diff --git a/kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala b/kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala index ed55ab06..88802d52 100644 --- a/kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala +++ b/kamon-core/src/main/scala/kamon/metric/MetricsExtension.scala @@ -88,8 +88,8 @@ class MetricsExtension(system: ExtendedActorSystem) extends Kamon.Extension { val key = entry.getKey val keyBasedConfig = entry.getValue.atKey(key) - val includes = keyBasedConfig.getStringList(s"$key.includes").asScala.map(inc ⇒ new GlobPathFilter(inc)).toList - val excludes = keyBasedConfig.getStringList(s"$key.excludes").asScala.map(exc ⇒ new GlobPathFilter(exc)).toList + val includes = keyBasedConfig.getStringList(s"$key.includes").asScala.map(inc ⇒ GlobPathFilter(inc)).toList + val excludes = keyBasedConfig.getStringList(s"$key.excludes").asScala.map(exc ⇒ GlobPathFilter(exc)).toList (key, MetricGroupFilter(includes, excludes)) } diff --git a/kamon-core/src/main/scala/kamon/util/GlobPathFilter.scala b/kamon-core/src/main/scala/kamon/util/GlobPathFilter.scala new file mode 100644 index 00000000..a980773f --- /dev/null +++ b/kamon-core/src/main/scala/kamon/util/GlobPathFilter.scala @@ -0,0 +1,44 @@ +/* + * ========================================================================================= + * Copyright © 2013-2014 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.util + +import scala.util.matching.Regex + +class GlobPathFilter(glob: String) { + + val regex = globAsRegex(glob) + + def accept(path: String): Boolean = path.matches(regex.toString()) + + private def globAsRegex(glob: String): Regex = { + val regexStr = new StringBuilder('^') + + glob.foreach { + case '.' ⇒ regexStr append """\.""" + case '*' ⇒ regexStr append """[^/]*""" + case '?' ⇒ regexStr append """.""" + case ch ⇒ regexStr append ch + } + regexStr append """/?$""" + + new Regex(regexStr.toString()) + } +} + +object GlobPathFilter { + def apply(glob: String): GlobPathFilter = new GlobPathFilter(glob) +} diff --git a/kamon-core/src/test/scala/kamon/util/GlobPathFilterSpec.scala b/kamon-core/src/test/scala/kamon/util/GlobPathFilterSpec.scala new file mode 100644 index 00000000..0611e169 --- /dev/null +++ b/kamon-core/src/test/scala/kamon/util/GlobPathFilterSpec.scala @@ -0,0 +1,43 @@ +/* + * ========================================================================================= + * Copyright © 2013-2014 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.util + +import org.scalatest.{Matchers, WordSpecLike} + +class GlobPathFilterSpec extends WordSpecLike with Matchers { + "The GlobPathFilter" should { + + "match a single expression" in { + val filter = GlobPathFilter("/user/actor") + + filter.accept("/user/actor") should be (true) + + filter.accept("/user/actor/something") should be (false) + filter.accept("/user/actor/somethingElse") should be (false) + } + + "match all expressions in the same level" in { + val filter = GlobPathFilter("/user/*") + + filter.accept("/user/actor") should be (true) + filter.accept("/user/otherActor") should be (true) + + filter.accept("/user/something/actor") should be (false) + filter.accept("/user/something/otherActor") should be (false) + } + } +} |