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

Optionally disable all default exports for JavaAgent #698

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ 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();
DefaultExports.initialize();
new JmxCollector(new File(config.file)).register();

if (!config.disableDefaultExports) {
DefaultExports.initialize();
}

server = new HTTPServer(config.socket, CollectorRegistry.defaultRegistry, true);
}
catch (IllegalArgumentException e) {
Expand All @@ -45,18 +49,20 @@ public static void premain(String agentArgument, Instrumentation instrumentation
*/
public static Config parseConfig(String args, String ifc) {
Pattern pattern = Pattern.compile(
"^(?:((?:[\\w.-]+)|(?:\\[.+])):)?" + // host name, or ipv4, or ipv6 address in brackets
"(\\d{1,5}):" + // port
"(.+)"); // config file
"^(disableDefaultExports:)?" + // optional flag to disable default exports
"(?:((?:[\\w.]+)|(?:\\[.+])):)?" + // host name, or ipv4, or ipv6 address in brackets
"(\\d{1,5}):" + // port
"(.+)"); // config file

Matcher matcher = pattern.matcher(args);
if (!matcher.matches()) {
throw new IllegalArgumentException("Malformed arguments - " + args);
}

String givenHost = matcher.group(1);
String givenPort = matcher.group(2);
String givenConfigFile = matcher.group(3);
boolean disableDefaultExports = "disableDefaultExports:".equals(matcher.group(1));
String givenHost = matcher.group(2);
String givenPort = matcher.group(3);
String givenConfigFile = matcher.group(4);

int port = Integer.parseInt(givenPort);

Expand All @@ -69,20 +75,22 @@ public static Config parseConfig(String args, String ifc) {
givenHost = ifc;
}

return new Config(givenHost, port, givenConfigFile, socket);
return new Config(givenHost, port, givenConfigFile, socket, disableDefaultExports);
}

static class Config {
String host;
int port;
String file;
InetSocketAddress socket;
boolean disableDefaultExports;

Config(String host, int port, String file, InetSocketAddress socket) {
Config(String host, int port, String file, InetSocketAddress socket, boolean disableDefaultExports) {
this.host = host;
this.port = port;
this.file = file;
this.socket = socket;
this.disableDefaultExports = disableDefaultExports;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,64 @@ public void testAgentStringParsing() {
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertFalse(config.disableDefaultExports);

config = JavaAgent.parseConfig("8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertFalse(config.disableDefaultExports);

config = JavaAgent.parseConfig("google.com:8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals("google.com", config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("my-host:8080:config.yaml", DEFAULT_HOST);
Assert.assertEquals("my-host", config.host);
Assert.assertEquals("config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertFalse(config.disableDefaultExports);

config = JavaAgent.parseConfig("127.0.0.1:8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals("127.0.0.1", config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertFalse(config.disableDefaultExports);

config = JavaAgent.parseConfig("8080:\\Windows\\Local\\Drive\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("\\Windows\\Local\\Drive\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertFalse(config.disableDefaultExports);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertFalse(config.disableDefaultExports);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("google.com:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("google.com", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertFalse(config.disableDefaultExports);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("127.0.0.1:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("127.0.0.1", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertFalse(config.disableDefaultExports);

config = JavaAgent.parseConfig("disableDefaultExports:127.0.0.1:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("127.0.0.1", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertTrue(config.disableDefaultExports);

config = JavaAgent.parseConfig("disableDefaultExports:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
Assert.assertTrue(config.disableDefaultExports);
}

/**
Expand Down