Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for collectorNamePattern in config and JavaAgent #760

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 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 @@ -73,6 +74,7 @@ private static class Config {
long lastUpdate = 0L;

MatchedRulesCache rulesCache;
String collectorNamePattern = "";
}

private Config config;
Expand Down Expand Up @@ -207,6 +209,16 @@ private Config loadConfig(Map<String, Object> yamlConfig) throws MalformedObject
}
}

if (yamlConfig.containsKey("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");
for (Map<String, Object> ruleObject : configRules) {
Expand Down Expand Up @@ -275,6 +287,10 @@ private Config loadConfig(Map<String, Object> yamlConfig) throws MalformedObject
return cfg;

}

public String getCollectorNamePattern() {
return config.collectorNamePattern;
}

static String toSnakeAndLowerCase(String attrName) {
if (attrName == null || attrName.isEmpty()) {
Expand Down
19 changes: 18 additions & 1 deletion collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,24 @@ public void testDelayedStartReady() throws Exception {
Thread.sleep(2000);
assertEquals(1.0, registry.getSampleValue("boolean_Test_True", new String[]{}, new String[]{}), .001);
}


@Test
public void testCollectorNamePatternNotSet() throws Exception {
JmxCollector jc = new JmxCollector("---\n").register(registry);
assertEquals("", jc.getCollectorNamePattern());
}

@Test
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 @@ -7,6 +7,7 @@
import java.util.regex.Pattern;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Predicate;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;

Expand All @@ -26,9 +27,20 @@ public static void premain(String agentArgument, Instrumentation instrumentation
Config config = parseConfig(agentArgument, host);

new BuildInfoCollector().register();
new JmxCollector(new File(config.file), JmxCollector.Mode.AGENT).register();
final JmxCollector collector = new JmxCollector(new File(config.file), JmxCollector.Mode.AGENT).register();
DefaultExports.initialize();
server = new HTTPServer(config.socket, CollectorRegistry.defaultRegistry, true);
HTTPServer.Builder builder = new HTTPServer.Builder()
.withInetSocketAddress(config.socket)
.withRegistry(CollectorRegistry.defaultRegistry)
.withDaemonThreads(true);
if(collector.getCollectorNamePattern() != null && collector.getCollectorNamePattern().trim().length() > 0) {
builder = builder.withSampleNameFilter(new Predicate<String>() {
public boolean test(String t) {
return t.matches(collector.getCollectorNamePattern().trim());
}
});
}
server = builder.build();
}
catch (IllegalArgumentException e) {
System.err.println("Usage: -javaagent:/path/to/JavaAgent.jar=[host:]<port>:<yaml configuration file> " + e.getMessage());
Expand Down