How to clear historical statistics from Java codahale Metrics Timers

I use the Metrics API Metrics for Java.

We use the timers of our entire system to collect delays.

In the initial testing, we connect to our system through JMX to collect statistics, for example, average delays, 75 percent delays, etc.

The problem is that to delete all Metrics objects and re-create them (it seems that a lot of refactoring will be required), is there a way to delete historical data so that when we start a new test we don’t need to restart the system?

Any help would be appreciated.

+4
source share
1 answer

, resetTimers() MetricRegistry. metahalale metrics. ResettableUniformReservoir UniformReservoir. - ExponentiallyDecayingReservoir, .

public class MyRegistry extends MetricRegistry {

    private final ConcurrentMap<String, ResettableUniformReservoir> reservoirs = new ConcurrentHashMap<>();

    @Override
    public Timer timer(String name) {
         ResettableUniformReservoir reservoir = new ResettableUniformReservoir();
         reservoirs.put(name, reservoir);
         return super.register(name, new Timer(reservoir));
    }

    public void resetTimers() {
        for (ResettableUniformReservoir reservoir : reservoirs.values()) {
            reservoir.reset();
        }
    }


    static class ResettableUniformReservoir implements Reservoir {

        private static final int DEFAULT_SIZE = 1028;
        private static final int BITS_PER_LONG = 63;

        private final AtomicLong count = new AtomicLong();
        private volatile AtomicLongArray values = new AtomicLongArray(DEFAULT_SIZE);

        @Override
        public int size() {
            final long c = count.get();
            if (c > values.length()) {
                return values.length();
            }
            return (int) c;
        }

        @Override
        public void update(long value) {
            final long c = count.incrementAndGet();
            if (c <= values.length()) {
                values.set((int) c - 1, value);
            } else {
                final long r = nextLong(c);
                if (r < values.length()) {
                    values.set((int) r, value);
                }
            }
        }

        private static long nextLong(long n) {
            long bits, val;
            do {
                bits = ThreadLocalRandom.current().nextLong() & (~(1L << BITS_PER_LONG));
                val = bits % n;
            } while (bits - val + (n - 1) < 0L);
            return val;
        }

        @Override
        public Snapshot getSnapshot() {
            final int s = size();
            final List<Long> copy = new ArrayList<Long>(s);
            for (int i = 0; i < s; i++) {
                copy.add(values.get(i));
            }
            return new UniformSnapshot(copy);
        }

        public void reset() {
            count.set(0);
            values = new AtomicLongArray(DEFAULT_SIZE);
        }
    }
}
+2

Source: https://habr.com/ru/post/1547364/


All Articles