GithubHelp home page GithubHelp logo

mango-monitor's Introduction

mango-monitor

一个监控 jvm 信息的 springboot 学习的 demo 项目。

实现功能

  • 监控 jvm 系统信息、类加载状态、线程状态、内存使用状态等。
  • 分析出 cpu 热点 (cpu profiler)
  • 在代码运行时动态查看方法执行耗时、堆栈、入参和返回值

使用技术

  • java agent、jmx、attach api、Byte Buddy、SpringBoot

原理

  • 监控 jvm 信息
  1. 通过 VirtualMachine.list() 获取本地 jvm 列表,主要是 pid 和 name
  2. 选择需要监控的 jvm,通过 pid 和该 jvm 建立连接,此 springboot 项目作为一个 jmx 客户端
  3. 通过建立的连接获取各种 MXBean 的信息。
  • cpu profiler

通过定时任务 dump 线程堆栈信息,然后统计汇总。

  • 动态查看方法执行耗时、堆栈、入参和返回值
  1. 定义 agentmain 方法,通过使用 java attach api 可以动态加载 agent 到 jvm 中
  2. 使用 Byte Buddy AgentBuilder 类开发 agent
  3. 开发执行耗时、堆栈、入参和返回值对应的 Advice

Demo

该项目分为 2 个分支:agent、monitor

  • agent 实现 java agent,查看运行时方法执行耗时、堆栈、入参和返回值。
  • monitor 是 springBoot 程序,提供交互接口。
  1. 将 agent 项目打包,将打成的 jar 包路径放在 monitor 项目中 application.properites 中。
  2. 启动 monitor 项目,调用 JvmController 中 getJVMList 接口

image.png

  1. 选择感兴趣的 jvm 程序,此处选择当前启动的 springBoot 程序,将该 pid 作为参数,调用 JvmController 中 attach 接口,建立与 jmx 的连接。

image.png

  1. 调用 DashboardController 中的 getOperatingSystemInfo 接口获取操作信息信息。其他 jvm 信息也是调用 DashboardController 的接口。

image.png

  1. 调用 AgentController 的 agent 接口,请求参数如下图

image.png

请求体信息封装在 AgentAction 类中 image.png

function 表示拦截的功能,方法耗时对应 time,方法堆栈对应 trace,方法入参和返回值对应 params; className 表示需要拦截类的全类名; methodName 表示需要拦截的方法名;

上面请求就是拦截 DashboardService 类的 getMemoryInfo 方法,获取方法入参和返回值。

当上面接口响应成功后,可以看到控制台的打印: image.png

  1. 调用 DashboardController 类的 heapMemoryInfo 方法,查看拦截打印的内容:

image.png

agent params method : public org.monitor.entity.info.MemoryInfo org.monitor.service.DashboardService.getMemoryInfo(int) enter params are : [1]
agent params method : public org.monitor.entity.info.MemoryInfo org.monitor.service.DashboardService.getMemoryInfo(int) return params are : MemoryInfo(init=268435456, used=183800936, committed=359137280, max=3817865216)
  1. cpuProfiler 功能需要调用 ProfilerController 的 startCpuProfiler 接口开始进行统计,一段时间后最后调用 endCpuProfiler 接口结束统计。

参考

  1. cpu profiler 参考:https://github.com/uber-common/jvm-profiler

todo

  1. 支持连接远程 jvm 进程。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.