springboot运行时加载statsd-jvm-profiler

虞俊美
2023-12-01

statsd-jvm-profiler是etsy开发的一个 JVM 代理 Profiler,其将Profile数据发送到 StatsD.

-javaagent加载

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

运行时动态加载

将该jar包拷贝到spring boot工程的main resource目录下,然后

@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/...

graphite

在graphite里头,statsd-jvm-profiler前缀的gauges就是了。

doc

 类似资料: