Skip to content

Commit

Permalink
Cleanup and added regex validation
Browse files Browse the repository at this point in the history
Signed-off-by: scottselikoff <[email protected]>
  • Loading branch information
Selikoff committed Dec 23, 2022
1 parent 73aee9c commit b425a01
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
11 changes: 9 additions & 2 deletions collector/src/main/java/io/prometheus/jmx/JmxCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import static java.lang.String.format;

Expand Down Expand Up @@ -209,8 +210,14 @@ private Config loadConfig(Map<String, Object> yamlConfig) throws MalformedObject
}

if (yamlConfig.containsKey("collectorNamePattern")) {
cfg.collectorNamePattern = (String)yamlConfig.get("collectorNamePattern");
}
final String pattern = (String)yamlConfig.get("collectorNamePattern");
try {
Pattern.compile(pattern);
cfg.collectorNamePattern = pattern;
} catch (PatternSyntaxException e) {
throw new IllegalArgumentException("Invalid collectNamePattern regular expression: " + pattern);
}
}

if (yamlConfig.containsKey("rules")) {
List<Map<String,Object>> configRules = (List<Map<String,Object>>) yamlConfig.get("rules");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ public void testCollectorNamePatternSet() throws Exception {
JmxCollector jc = new JmxCollector("---\ncollectorNamePattern: `jmx_collector_v1`".replace('`','"')).register(registry);
assertEquals("jmx_collector_v1", jc.getCollectorNamePattern());
}


@Test(expected=IllegalArgumentException.class)
public void testCollectorNamePatternInvalidRegex() throws Exception {
JmxCollector jc = new JmxCollector("---\ncollectorNamePattern: `(`".replace('`','"')).register(registry);
}

@Test
public void testCamelLastExchangFailureTimestamp() throws Exception{
String rulePattern =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sun.net.httpserver.HttpServer;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Predicate;
import io.prometheus.client.exporter.HTTPServer;
Expand Down

0 comments on commit b425a01

Please sign in to comment.