[英]Monitoring a heavily loaded Java application
我想知道最好使用Java获取应用程序性能指标的标准做法。 当前,我们有一个定期计划的任务,用于收集系统指标。 通常,此任务未按时计划,从而导致度量标准在该时间不可用,从而导致监视仪表板损坏(在折线图的情况下会出现间隙)。
通常,当应用程序运行不佳时,那就是我们希望所有指标都可用的时候。 但是我们已经注意到,那时是我们无法收集任何指标的时间(因为应用程序非常繁忙)
您可以使用一个叫做top-threads的工具,可以在这里找到: https : //bitbucket.org/pjtr/topthreads
它的作用是为您提供有关目标JVM加载的每个类和线程的所有使用详细信息(RAM,CPU等)。
上面的页面提供了此用法
您可以使用位于Java lib目录中Tools.jar文件中的Sun's Library将代理加载到目标VM中。
加载代理如下所示:
/**
* Hooks/Attaches to the process's VM with a given PID
*
* @return true if the hook/attach was successful
*/
public boolean hook() {
try {
return (vm = VirtualMachine.attach(Long.toString(pid))) != null;
} catch (AttachNotSupportedException | IOException e) {
e.printStackTrace();
return false;
}
}
/**
* Loads a thread agent which can debug the running threads and classes of this process
* @param agent - the location of the agent jar file
* @param options - the options/arguments to pass into the agents main method
*/
public boolean loadAgent(String agent, String options) {
try {
vm.loadAgent(agent, options);
return true;
} catch (AgentLoadException | AgentInitializationException | IOException e) {
e.printStackTrace();
return false;
}
}
Agent Main Class看起来像这样...
另外,请注意:创建代理jar时,必须在清单文件中指定Agent-main,并在其中包含要加载代理的包含agentmain方法的类的位置。
public class Agent {
/**
* An Object Lock for thread sync's if neccessary */
public static final Object LOCK = new Object();
/**
* Starts the agent with this agent main
*
* @param agentArgs
* - the agent args being passed into this method
* @param inst
* - the instrumentation instrument that is passed into this
* method
*/
public static void agentmain(String agentArgs, Instrumentation inst) {
//Do whatever you want to the target VM here, hacky, but eh, use at your own risk, it is included in java itself...
}
}
代理清单文件的描述
Manifest-Version: 1.0
Agent-Class: packageNameThatWillBeDifferent.Agent
Created-By: 1.8.0_101 (Oracle Corporation)
无论这是“企业”应用程序还是在JVM中提供服务的小型服务。 您需要深入了解此垃圾收集运行时的基本运行状况详细信息。 那就是内存池(堆,堆外),GC统计最突出。
仅从主机视图收集过程指标(CPU使用率,居民集大小(RSS),IO)并不能帮助您真正了解JVM的功能以及代码中的热点。
如果您无权访问代码,则代理可能是获得JVM见解的唯一机会。 否则,您实际上应该使用上面提到的一些杰出的Java指标库来对JVM进行检测。
在我看来,最广泛使用的Java指标库是Dropwizard Metrics 。
尽管我有些偏见,但我还是建议您看一下Micrometer项目。 您可以通过几行代码来配置一组基本的JVM运行时指标,以基本了解JVM的运行时行为。 完成后,您可以通过定时对代码中的热点进行检测。 千分尺为各种已建立的监控系统提供了大量的公制输出。 (Prometheus,InfluxDb,石墨...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.