-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollback GraphiteSanitize to replacing whitespaces (#1099)
As reported in #1098 the current sanitizer is too aggressive. It strips dots which play an important role in the Graphite metrics name convention. Until we develop a better algorithm, let's do only basic sanitization.
- Loading branch information
Showing
6 changed files
with
25 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 8 additions & 63 deletions
71
metrics-graphite/src/main/java/com/codahale/metrics/graphite/GraphiteSanitize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,16 @@ | ||
package com.codahale.metrics.graphite; | ||
|
||
class GraphiteSanitize { | ||
/** Replaces all characters from a given string that are not ascii and not alphanumeric | ||
* with a dash */ | ||
static String sanitize(String string, char replacement) { | ||
String replaced = replaceFrom(string, replacement); | ||
|
||
// Consolidate multiple dashes into a single one | ||
String result = replaced.replace("--", "-"); | ||
while (!result.equals(replaced)) { | ||
replaced = result; | ||
result = replaced.replace("--", "-"); | ||
} | ||
|
||
// Remove any leading or trailing dashes | ||
return strip(result, replacement); | ||
} | ||
|
||
/** A char matches when it is a letter or digit and it is ASCII, in Guava terminology, | ||
* this would be CharMatcher.ASCII.and(CharMatcher.JAVA_LETTER_OR_DIGIT).negate() */ | ||
private static boolean matches(char c) { | ||
return !(Character.isLetterOrDigit(c) && c <= '\u007f'); | ||
} | ||
import java.util.regex.Pattern; | ||
|
||
/** Replace all characters that we're interested in with a replacement character, | ||
* heavily inspired by the same code in Guava's CharMatcher */ | ||
private static String replaceFrom(String string, char replacement) { | ||
int pos = indexIn(string, 0); | ||
if (pos == -1) { | ||
return string; | ||
} | ||
char[] chars = string.toCharArray(); | ||
chars[pos] = replacement; | ||
for (int i = pos + 1; i < chars.length; i++) { | ||
if (matches(chars[i])) { | ||
chars[i] = replacement; | ||
} | ||
} | ||
return new String(chars).trim(); | ||
} | ||
class GraphiteSanitize { | ||
|
||
/** Finds the first index (or -1) of a character we're interested in */ | ||
private static int indexIn(String sequence, int start) { | ||
int length = sequence.length(); | ||
for (int i = start; i < length; i++) { | ||
if (matches(sequence.charAt(i))) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
private static final Pattern WHITESPACE = Pattern.compile("[\\s]+"); | ||
private static final String DASH = "-"; | ||
|
||
/** Strips a given character from the beginning and end of a string, | ||
* heavily inspired by Apache's StringUtils.strip | ||
/** | ||
* Trims the string and peplaces all whitespace characters with the provided symbol | ||
*/ | ||
private static String strip(String str, char strip) { | ||
int strLen = str.length(); | ||
int start = 0; | ||
int end = strLen - 1; | ||
while (start != strLen && str.charAt(start) == strip) { | ||
start++; | ||
} | ||
|
||
while (end > start && str.charAt(end) == strip) { | ||
end--; | ||
} | ||
|
||
return start != 0 || end != strLen - 1 ? str.substring(start, end + 1) : str; | ||
static String sanitize(String string) { | ||
return WHITESPACE.matcher(string.trim()).replaceAll(DASH); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters