问题描述
我想添加指标来跟踪存储在服务值中的操作数量和站点数量的大小:
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());
}
}
提前致谢!
解决方法
而且这个源代码运行正常!