springboot運行時加載statsd-jvm-profiler

statsd-jvm-profiler是etsy開發的一個 JVM 代理 Profiler,其將Profile數據發送到 StatsD.java

-javaagent加載

-javaagent:/usr/etsy/statsd-jvm-profiler/statsd-jvm-profiler.jar=server=statsdHost,port=statsdPort

運行時動態加載

將該jar包拷貝到spring boot工程的main resource目錄下,而後git

@Bean
    public CommandLineRunner loadProfilerAgent(){
        return new CommandLineRunner() {
            @Override
            public void run(String... strings) throws Exception {
                ApplicationPid pid = new ApplicationPid();
                try{
                    com.sun.tools.attach.VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(pid.toString());
                    ClassLoader classLoader = getClass().getClassLoader();
                    String profilerJarPath = classLoader.getResource("statsd-jvm-profiler-2.0.0.jar").getPath();
                    String agentArgs = "server="+statsdHost+",port="+statsdPort;
                    vm.loadAgent(profilerJarPath, agentArgs);
                    vm.detach();
                    System.out.println("Dynamically loaded StatsD JVM Profiler Agent...");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
    }

內置httpServer

該agent內置了一個http server,默認端口爲5005,能夠訪問以下幾個path
http://localhost:5005/errors
http://localhost:5005/isRunning
http://localhost:5005/profilers
http://localhost:5005/disable...
http://localhost:5005/status/...github

graphite

在graphite裏頭,statsd-jvm-profiler前綴的gauges就是了。spring

doc

相關文章
相關標籤/搜索