diff --git a/jmx_prometheus_javaagent_java6/src/main/java/io/prometheus/jmx/JavaAgent.java b/jmx_prometheus_javaagent_java6/src/main/java/io/prometheus/jmx/JavaAgent.java index 74cbcbf8..1aa7b9e7 100644 --- a/jmx_prometheus_javaagent_java6/src/main/java/io/prometheus/jmx/JavaAgent.java +++ b/jmx_prometheus_javaagent_java6/src/main/java/io/prometheus/jmx/JavaAgent.java @@ -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) { @@ -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); @@ -69,7 +75,7 @@ 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 { @@ -77,12 +83,14 @@ static class Config { 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; } } } diff --git a/jmx_prometheus_javaagent_java6/src/test/java/io/prometheus/jmx/TestJavaAgent.java b/jmx_prometheus_javaagent_java6/src/test/java/io/prometheus/jmx/TestJavaAgent.java index 12610210..da61f049 100644 --- a/jmx_prometheus_javaagent_java6/src/test/java/io/prometheus/jmx/TestJavaAgent.java +++ b/jmx_prometheus_javaagent_java6/src/test/java/io/prometheus/jmx/TestJavaAgent.java @@ -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); } /**