metrics icon indicating copy to clipboard operation
metrics copied to clipboard

How can I update a Gauge?

Open danielamorais opened this issue 5 years ago • 1 comments

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
            }
        });

danielamorais avatar Jun 21 '19 04:06 danielamorais

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() {
 
        }
    }

ralf0131 avatar Jun 25 '19 03:06 ralf0131