7

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

4

3 回答 3

7

刚刚检查,我发现这个执行器.../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

于 2020-02-11T19:41:47.853 回答
3

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": []
}
于 2018-09-03T13:00:11.943 回答
1

不幸的是, 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 负载。

希望这可以帮助。

于 2017-03-01T15:59:31.843 回答