Skip to content

Commit a2e759f

Browse files
author
Liam Clarke
committed
Removed metric name validation when using a CustomSampleMappingBuilder to allow usage on arbitrarily named metrics
Signed-off-by: Liam Clarke <[email protected]>
1 parent ef16c43 commit a2e759f

File tree

4 files changed

+59
-62
lines changed

4 files changed

+59
-62
lines changed

simpleclient_dropwizard/src/main/java/io/prometheus/client/dropwizard/samplebuilder/GraphiteNamePattern.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.regex.Matcher;
66
import java.util.regex.Pattern;
77

8-
import static io.prometheus.client.dropwizard.samplebuilder.MapperConfig.METRIC_GLOB_REGEX;
98

109
/**
1110
* GraphiteNamePattern is initialised with a simplified glob pattern that only allows '*' as special character.
@@ -20,7 +19,6 @@
2019
* It contains logic to match a metric name and to extract named parameters from it.
2120
*/
2221
class GraphiteNamePattern {
23-
private static final Pattern VALIDATION_PATTERN = Pattern.compile(METRIC_GLOB_REGEX);
2422

2523
private Pattern pattern;
2624
private String patternStr;
@@ -30,10 +28,7 @@ class GraphiteNamePattern {
3028
*
3129
* @param pattern The glob style pattern to be used.
3230
*/
33-
GraphiteNamePattern(final String pattern) throws IllegalArgumentException {
34-
if (!VALIDATION_PATTERN.matcher(pattern).matches()) {
35-
throw new IllegalArgumentException(String.format("Provided pattern [%s] does not matches [%s]", pattern, METRIC_GLOB_REGEX));
36-
}
31+
GraphiteNamePattern(final String pattern) {
3732
initializePattern(pattern);
3833
}
3934

simpleclient_dropwizard/src/main/java/io/prometheus/client/dropwizard/samplebuilder/MapperConfig.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919
* Dropwizard metrics that match the "match" pattern will be further processed to have a new name and new labels based on this config.
2020
*/
2121
public class MapperConfig {
22-
// each part of the metric name between dots
23-
private static final String METRIC_PART_REGEX = "[a-zA-Z_0-9](-?[a-zA-Z0-9_])+";
24-
// Simplified GLOB: we can have "*." at the beginning and "*" only at the end
25-
static final String METRIC_GLOB_REGEX = "^(\\*\\.|" + METRIC_PART_REGEX + "\\.)+(\\*|" + METRIC_PART_REGEX + ")$";
22+
2623
// Labels validation.
2724
private static final String LABEL_REGEX = "^[a-zA-Z_][a-zA-Z0-9_]+$";
28-
private static final Pattern MATCH_EXPRESSION_PATTERN = Pattern.compile(METRIC_GLOB_REGEX);
2925
private static final Pattern LABEL_PATTERN = Pattern.compile(LABEL_REGEX);
3026

3127
/**
@@ -74,13 +70,11 @@ public MapperConfig() {
7470

7571
// for tests
7672
MapperConfig(final String match) {
77-
validateMatch(match);
7873
this.match = match;
7974
}
8075

8176
public MapperConfig(final String match, final String name, final Map<String, String> labels) {
8277
this.name = name;
83-
validateMatch(match);
8478
this.match = match;
8579
validateLabels(labels);
8680
this.labels = labels;
@@ -96,7 +90,6 @@ public String getMatch() {
9690
}
9791

9892
public void setMatch(final String match) {
99-
validateMatch(match);
10093
this.match = match;
10194
}
10295

@@ -118,13 +111,6 @@ public void setLabels(final Map<String, String> labels) {
118111
this.labels = labels;
119112
}
120113

121-
private void validateMatch(final String match)
122-
{
123-
if (!MATCH_EXPRESSION_PATTERN.matcher(match).matches()) {
124-
throw new IllegalArgumentException(String.format("Match expression [%s] does not match required pattern %s", match, MATCH_EXPRESSION_PATTERN));
125-
}
126-
}
127-
128114
private void validateLabels(final Map<String, String> labels)
129115
{
130116
if (labels != null) {

simpleclient_dropwizard/src/test/java/io/prometheus/client/dropwizard/samplebuilder/GraphiteNamePatternTest.java

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,10 @@
88
import java.util.HashMap;
99
import java.util.List;
1010
import java.util.Map;
11+
import java.util.Map.Entry;
1112

1213
public class GraphiteNamePatternTest {
1314

14-
@Test(expected = IllegalArgumentException.class)
15-
public void createNew_WHEN_InvalidPattern_THEN_ShouldThrowException() {
16-
final List<String> invalidPatterns = Arrays.asList(
17-
"",
18-
"a",
19-
"1org",
20-
"1org.",
21-
"org.",
22-
"org.**",
23-
"org.**",
24-
"org.company-",
25-
"org.company-.",
26-
"org.company-*",
27-
"org.company.**",
28-
"org.company.**-",
29-
"org.com*pany.*",
30-
"org.test.contr.oller.gather.status..400",
31-
"org.test.controller.gather.status..400"
32-
);
33-
final GraphiteNamePattern graphiteNamePattern = new GraphiteNamePattern("");
34-
for (String pattern : invalidPatterns) {
35-
try {
36-
new GraphiteNamePattern(pattern);
37-
38-
Assertions.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
39-
} catch (IllegalArgumentException e) {
40-
Assertions.assertThat(e).hasMessageContaining(pattern);
41-
}
42-
}
43-
}
44-
4515
@Test
4616
public void createNew_WHEN_ValidPattern_THEN_ShouldCreateThePatternSuccessfully() {
4717
final List<String> validPatterns = Arrays.asList(
@@ -58,6 +28,7 @@ public void createNew_WHEN_ValidPattern_THEN_ShouldCreateThePatternSuccessfully(
5828
}
5929
}
6030

31+
6132
@Test
6233
public void createNew_WHEN_ValidPattern_THEN_ShouldInitInternalPatternSuccessfully() {
6334
final Map<String, String> validPatterns = new HashMap<String, String>();
@@ -72,6 +43,7 @@ public void createNew_WHEN_ValidPattern_THEN_ShouldInitInternalPatternSuccessful
7243
}
7344
}
7445

46+
7547
@Test
7648
public void match_WHEN_NotMatchingMetricNameProvided_THEN_ShouldNotMatch() {
7749
final GraphiteNamePattern pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
@@ -86,6 +58,7 @@ public void match_WHEN_NotMatchingMetricNameProvided_THEN_ShouldNotMatch() {
8658
}
8759
}
8860

61+
8962
@Test
9063
public void match_WHEN_MatchingMetricNameProvided_THEN_ShouldMatch() {
9164
final GraphiteNamePattern pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
@@ -102,6 +75,28 @@ public void match_WHEN_MatchingMetricNameProvided_THEN_ShouldMatch() {
10275
}
10376
}
10477

78+
79+
@Test
80+
public void match_WHEN_onlyGlob_THEN_ShouldMatchAny() {
81+
GraphiteNamePattern pattern = new GraphiteNamePattern("*");
82+
Assertions.assertThat(pattern.matches("foo")).isTrue();
83+
Assertions.assertThat(pattern.matches("bar")).isTrue();
84+
}
85+
86+
87+
@Test
88+
public void match_WHEN_varyingFormats_THEN_ShouldMatchRegardless() {
89+
Map<String, String> metricsToPatterns = new HashMap<String, String>();
90+
metricsToPatterns.put("snake_case_example_metric", "snake_case_*_metric");
91+
metricsToPatterns.put("CamelCasedExampleMetric", "CamelCased*Metric");
92+
metricsToPatterns.put("Weird.Mixture_Of_Formats.Example_metric", "Weird.Mixture_Of_Formats.*_metric");
93+
94+
for (Entry<String, String> metricToPattern : metricsToPatterns.entrySet()) {
95+
Assertions.assertThat(new GraphiteNamePattern(metricToPattern.getValue()).matches(metricToPattern.getKey())).isTrue();
96+
}
97+
}
98+
99+
105100
@Test
106101
public void extractParameters() {
107102
GraphiteNamePattern pattern;
@@ -110,17 +105,18 @@ public void extractParameters() {
110105
expected.put("${1}", "400");
111106
pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
112107
Assertions.assertThat(pattern.extractParameters("org.test.controller.gather.status.400"))
113-
.isEqualTo(expected);
108+
.isEqualTo(expected);
114109

115110
expected = new HashMap<String, String>();
116111
expected.put("${0}", "org");
117112
expected.put("${1}", "gather");
118113
expected.put("${2}", "400");
119114
pattern = new GraphiteNamePattern("*.test.controller.*.status.*");
120115
Assertions.assertThat(pattern.extractParameters("org.test.controller.gather.status.400"))
121-
.isEqualTo(expected);
116+
.isEqualTo(expected);
122117
}
123118

119+
124120
@Test
125121
public void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldReturnEmptyString() {
126122
GraphiteNamePattern pattern;
@@ -129,16 +125,42 @@ public void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldRet
129125
expected.put("${1}", "400");
130126
pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
131127
Assertions.assertThat(pattern.extractParameters("org.test.controller..status.400"))
132-
.isEqualTo(expected);
128+
.isEqualTo(expected);
133129

134130
}
135131

132+
136133
@Test
137134
public void extractParameters_WHEN_moreDots_THEN_ShouldReturnNoMatches() {
138135
GraphiteNamePattern pattern;
139136
pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
140137
Assertions.assertThat(pattern.extractParameters("org.test.controller...status.400"))
141-
.isEqualTo(Collections.emptyMap());
138+
.isEqualTo(Collections.emptyMap());
139+
140+
}
142141

142+
@Test
143+
public void extractParameters_WHEN_onlyGlob_THEN_ShouldExtractEntireMetric() {
144+
String metric = "http_requests";
145+
GraphiteNamePattern pattern = new GraphiteNamePattern("*");
146+
Map<String, String> expected = new HashMap<String, String>();
147+
expected.put("${0}", metric);
148+
Assertions.assertThat(pattern.extractParameters(metric)).isEqualTo(expected);
149+
}
150+
151+
152+
@Test
153+
public void extractParameters_WHEN_varyingFormats_THEN_ShouldExtractRegardless() {
154+
Map<String, String> metricsToPatterns = new HashMap<String, String>();
155+
metricsToPatterns.put("snake_case_example_metric", "snake_case_*_metric");
156+
metricsToPatterns.put("CamelCasedExampleMetric", "CamelCased*Metric");
157+
metricsToPatterns.put("Weird.Mixture_Of_Formats.Example_metric", "Weird.Mixture_Of_Formats.*_metric");
158+
159+
for (Entry<String, String> metricToPattern : metricsToPatterns.entrySet()) {
160+
GraphiteNamePattern graphiteNamePattern = new GraphiteNamePattern(metricToPattern.getValue());
161+
Entry<String, String> actual = graphiteNamePattern.extractParameters(metricToPattern.getKey()).entrySet().iterator().next();
162+
Assertions.assertThat(actual.getKey()).isEqualTo("${0}");
163+
Assertions.assertThat(actual.getValue()).isEqualToIgnoringCase("example");
164+
}
143165
}
144166
}

simpleclient_dropwizard/src/test/java/io/prometheus/client/dropwizard/samplebuilder/MapperConfigTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ public void setMatch_WHEN_ExpressionMatchesPattern_AllGood() {
1717
assertEquals("com.company.meter.*", mapperConfig.getMatch());
1818
}
1919

20-
@Test(expected = IllegalArgumentException.class)
21-
public void setMatch_WHEN_ExpressionDoesnNotMatchPattern_ThrowException() {
22-
final MapperConfig mapperConfig = new MapperConfig();
23-
mapperConfig.setMatch("com.company.meter.**.yay");
24-
}
25-
2620
@Test
2721
public void setLabels_WHEN_ExpressionMatchesPattern_AllGood() {
2822
final MapperConfig mapperConfig = new MapperConfig();

0 commit comments

Comments
 (0)