metrics
metrics copied to clipboard
How can I update a Gauge?
The listener receives a Gauge<Float> and is necessary to persist in MetricManager. How can I update a Gauge<Float> inside MetricManager? At MetricRegistryImpl#register an exception "A metric named ... already exists" is thrown if I try to call MetricManager.register with the same metricName.
CpuUsageService cpuUsageService = new CpuUsageServiceImpl(100L, 500L);
cpuUsageService.addListener("foo.bar", cpu -> {
SortedMap<MetricName, Gauge> gauges = MetricManager.getIMetricManager().getGauges(DUBBO_GROUP, MetricFilter.ALL);
Gauge<Float> cpuUser = gauges.get(new MetricName("dubbo.cpu." + invoker.getUrl().getHost(), MetricLevel.MAJOR));
if (cpuUser == null) {
MetricName metricName = new MetricName("dubbo.cpu." + invoker.getUrl().getHost(), MetricLevel.MAJOR);
MetricManager.register(DUBBO_GROUP, metricName, cpu);
} else {
//TODO: I need to update the Gauge here
}
});
If you want to update gauge, you need to keep the reference to that gauge, and provide a dedicate method to update it.
For example,
class TestGauge implement Gauge {
private int data;
public void update() {
// update here
}
public int getValue() {
}
}