Spring Boot 应用程序中的 com.codahale.metrics.Gauges

问题描述

我想添加指标来跟踪存储在服务值中的操作数量站点数量的大小:

import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.stereotype.Service;

@Service
public class TrackerService {

    private final AtomicInteger sitesInProgress  = new AtomicInteger(0);
    private static AtomicInteger amountOfExecutingOperations = new AtomicInteger(0);

    public AtomicInteger getSitesInProgress() {
        return sitesInProgress;
    }
    
    public static AtomicInteger getAmountOfExecutingOperations() {
        return amountOfExecutingOperations;
    }
}

仅仅创建一个这样的服务来跟踪这些值就足够了吗?

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.codahale.metrics.MetricRegistry;

@Service
public class GaugeMetricService {
    
    @Autowired
    public GaugeMetricService(final MetricRegistry metricRegistry,final TrackerService trackerService) {
        metricRegistry.gauge(MetricRegistry.name("tracker","sitesInProgress"),() -> () -> trackerServicerackerService.getSitesInProgress());
        metricRegistry.gauge(MetricRegistry.name("deviceoperationtracker","numberOfExecutingOperations"),() -> () -> trackerServicerackerService.getAmountOfExecutingOperations());
    }

}

提前致谢!

解决方法

而且这个源代码运行正常!

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...