【问题标题】:Spring Boot Actuator to Give CPU UsageSpring Boot Actuator 提供 CPU 使用率
【发布时间】:2016-06-23 08:10:31
【问题描述】:

我想知道有没有办法通过 Spring Boot Actuator 获取 CPU 使用率指标?我能够使用 /metrics 和 /health 端点查看其他指标,但无法获得 CPU 使用率。 我想避免为了查看 CPU 使用情况而编写额外的类。 任何的想法? 谢谢

【问题讨论】:

  • /metrics 端点显示机器的平均负载。这是你想要的 ?空闲计算机的平均负载为 0。每个正在使用或等待 CPU 资源的正在运行的进程将平均负载加 1。

标签: cpu-usage metrics spring-boot-actuator


【解决方案1】:

刚查了一下,我找到了这个执行器.../actuator/metrics/process.cpu.usage

它输出以下内容:

{
    name: "process.cpu.usage",
    description: "The "recent cpu usage" for the Java Virtual Machine process",
    baseUnit: null,
    measurements: [
        {
            statistic: "VALUE",
            value: 0.0001742149747252696
        }
    ],
    availableTags: [ ]
}

当前使用 Spring Boot 版本2.2.2.RELEASE。

【讨论】:

    【解决方案2】:

    Spring Boot 2 执行器解决方案(基于 @diginoise 的代码来测量 CPU 负载),注册一个 Gauge 具有在请求时测量值的功能(无需启动线程或调度计时器):

    @Component
    public class CpuMetrics {
    
        private final static String METRICS_NAME = "process.cpu.load";
    
        @Autowired
        private MeterRegistry meterRegistry;
    
        @PostConstruct
        public void init() {
            Gauge.builder(METRICS_NAME, this, CpuMetrics::getProcessCpuLoad)
                .baseUnit("%")
                .description("CPU Load")
                .register(meterRegistry);
        }
    
        public Double getProcessCpuLoad() {
            try {
                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
                ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
                AttributeList list = mbs.getAttributes(name, new String[]{"ProcessCpuLoad"});
    
                return Optional.ofNullable(list)
                        .map(l -> l.isEmpty() ? null : l)
                        .map(List::iterator)
                        .map(Iterator::next)
                        .map(Attribute.class::cast)
                        .map(Attribute::getValue)
                        .map(Double.class::cast)
                        .orElse(null);
    
            } catch (Exception ex) {
                return null;
            }
        }
    }
    

    CPU 指标随后将在/actuator/metrics/process.cpu.load 上提供:

    {
      "name": "process.cpu.load",
      "description": "CPU Load",
      "baseUnit": "%",
      "measurements": [
        {
          "statistic": "VALUE",
          "value": 0.09767676212004521
        }
      ],
      "availableTags": []
    }
    

    【讨论】:

    • 请问"value": 0.09767676212004521这个数字代表什么?
    • @Manu 1 将是 100%,因此该值表示 9.76..%
    【解决方案3】:

    很遗憾,没有可通过 Spring Boot Actuator 获得的 CPU 指标。
    幸运的是,您可以自己编写。

    只需创建一个满足以下条件的测量 bean:

    1. 它可以访问GaugeService,因为它将跟踪一个值。

      @Autowired
      private GaugeService gaugeService;
      
    2. 创建一个调用例程的线程来测量进程的 CPU 负载:

      @PostConstruct
      public void startMeasuring() {
          new Thread() {
              @Override
              public void run() {
                  gaugeService.submit("process.cpu.load", getProcessCpuLoad());
                  Thread.sleep(2000);   //measure every 2sec.
              }
          }.start();
      }
      
    3. 有一个例程使用 MxBeans为您的进程获取 CPU 负载:

      public static double getProcessCpuLoad() throws Exception {
          MBeanServer mbs    = ManagementFactory.getPlatformMBeanServer();
          ObjectName name    = ObjectName.getInstance("java.lang:type=OperatingSystem");
          AttributeList list = mbs.getAttributes(name, new String[]{ "ProcessCpuLoad" });
      
          if (list.isEmpty())     return Double.NaN;
      
          Attribute att = (Attribute)list.get(0);
          Double value  = (Double)att.getValue();
      
          // usually takes a couple of seconds before we get real values
          if (value == -1.0)      return Double.NaN;
          // returns a percentage value with 1 decimal point precision
          return ((int)(value * 1000) / 10.0);
      }
      

    您还可以使用此方法提取系统范围的 CPU 负载。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多