Permalink
Browse files

Move the body of to as is only invoked at JVM startup. The is always …

…invoked, also in the case of the agent is loaded afetr JVM startup
  • Loading branch information...
stoader committed Apr 14, 2018
1 parent 0b490c1 commit e83a7f123a983402aac2d831a716da4f4cd1ed5d
@@ -81,7 +81,7 @@
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>io.prometheus.jmx.shaded.io.prometheus.jmx.JavaAgent</Premain-Class>
<Agent-Class>io.prometheus.jmx.shaded.io.prometheus.jmx.JavaAgent</Agent-Class>
</manifestEntries>
</transformer>
</transformers>
@@ -8,44 +8,48 @@
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;


public class JavaAgent {

static HTTPServer server;

public static void premain(String agentArgument, Instrumentation instrumentation) throws Exception {
// Bind to all interfaces by default (this includes IPv6).
String host = "0.0.0.0";

// If we have IPv6 address in square brackets, extract it first and then
// remove it from arguments to prevent confusion from too namy colons.
Integer indexOfClosingSquareBracket = agentArgument.indexOf("]:");
if (indexOfClosingSquareBracket >= 0) {
host = agentArgument.substring(0, indexOfClosingSquareBracket + 1);
agentArgument = agentArgument.substring(indexOfClosingSquareBracket + 2);
}

String[] args = agentArgument.split(":");
if (args.length < 2 || args.length > 3) {
System.err.println("Usage: -javaagent:/path/to/JavaAgent.jar=[host:]<port>:<yaml configuration file>");
System.exit(1);
}

int port;
String file;
InetSocketAddress socket;

if (args.length == 3) {
port = Integer.parseInt(args[1]);
socket = new InetSocketAddress(args[0], port);
file = args[2];
} else {
port = Integer.parseInt(args[0]);
socket = new InetSocketAddress(host, port);
file = args[1];
}

new JmxCollector(new File(file)).register();
DefaultExports.initialize();
server = new HTTPServer(socket, CollectorRegistry.defaultRegistry, true);
}

public static void agentmain(String agentArgument, Instrumentation inst) throws Exception {
// Bind to all interfaces by default (this includes IPv6).
String host = "0.0.0.0";

// If we have IPv6 address in square brackets, extract it first and then
// remove it from arguments to prevent confusion from too namy colons.
Integer indexOfClosingSquareBracket = agentArgument.indexOf("]:");
if (indexOfClosingSquareBracket >= 0) {
host = agentArgument.substring(0, indexOfClosingSquareBracket + 1);
agentArgument = agentArgument.substring(indexOfClosingSquareBracket + 2);
}

String[] args = agentArgument.split(":");
if (args.length < 2 || args.length > 3) {
System.err.println("Usage: -javaagent:/path/to/JavaAgent.jar=[host:]<port>:<yaml configuration file>");
System.exit(1);
}

int port;
String file;
InetSocketAddress socket;

if (args.length == 3) {
port = Integer.parseInt(args[1]);
socket = new InetSocketAddress(args[0], port);
file = args[2];
} else {
port = Integer.parseInt(args[0]);
socket = new InetSocketAddress(host, port);
file = args[1];
}

new JmxCollector(new File(file)).register();
DefaultExports.initialize();
server = new HTTPServer(socket, CollectorRegistry.defaultRegistry, true);
}
}


0 comments on commit e83a7f1

Please sign in to comment.