001package io.prometheus.jmx;
002
003import java.io.File;
004import java.lang.instrument.Instrumentation;
005import java.net.InetSocketAddress;
006
007import io.prometheus.client.CollectorRegistry;
008import io.prometheus.client.exporter.HTTPServer;
009import io.prometheus.client.hotspot.DefaultExports;
010
011public class JavaAgent {
012
013   static HTTPServer server;
014
015   public static void premain(String agentArgument, Instrumentation instrumentation) throws Exception {
016     String[] args = agentArgument.split(":");
017     if (args.length < 2 || args.length > 3) {
018       System.err.println("Usage: -javaagent:/path/to/JavaAgent.jar=[host:]<port>:<yaml configuration file>");
019       System.exit(1);
020     }
021
022     int port;
023     String file;
024     InetSocketAddress socket;
025
026     if (args.length == 3) {
027       port = Integer.parseInt(args[1]);
028       socket = new InetSocketAddress(args[0], port);
029       file = args[2];
030     } else {
031       port = Integer.parseInt(args[0]);
032       socket = new InetSocketAddress(port);
033       file = args[1];
034     }
035
036     new JmxCollector(new File(file)).register();
037     DefaultExports.initialize();
038     server = new HTTPServer(socket, CollectorRegistry.defaultRegistry, true);
039   }
040}