AnnotatedNumberMonitor(MonitorConfig config, Object object, AccessibleObject field) { super(config); this.object = object; this.field = field; if ("COUNTER".equals(config.getTags().getValue("type"))) { SpectatorContext.polledGauge(config) .monitorMonotonicCounter(this, m -> m.getValue(0).longValue()); } else { SpectatorContext.polledGauge(config) .monitorValue(this, m -> m.getValue(0).doubleValue()); } }
@Inject public CassMonitorMetrics(Registry registry) { cassStop = registry.gauge(Metrics.METRIC_PREFIX + "cass.stop"); cassStart = registry.gauge(Metrics.METRIC_PREFIX + "cass.start"); cassAutoStart = registry.gauge(Metrics.METRIC_PREFIX + "cass.auto.start"); getSeedsCnt = PolledMeter.using(registry) .withName(Metrics.METRIC_PREFIX + "cass.getSeedCnt") .monitorMonotonicCounter(new AtomicLong(0)); getTokenCnt = PolledMeter.using(registry) .withName(Metrics.METRIC_PREFIX + "cass.getTokenCnt") .monitorMonotonicCounter(new AtomicLong(0)); getReplacedIpCnt = PolledMeter.using(registry) .withName(Metrics.METRIC_PREFIX + "cass.getReplacedIpCnt") .monitorMonotonicCounter(new AtomicLong(0)); doubleRingCnt = PolledMeter.using(registry) .withName(Metrics.METRIC_PREFIX + "cass.doubleRingCnt") .monitorMonotonicCounter(new AtomicLong(0)); }
private static void monitorMonitonicValues(Monitor<?> monitor) { if (!(monitor instanceof SpectatorMonitor)) { if (monitor instanceof CompositeMonitor<?>) { CompositeMonitor<?> cm = (CompositeMonitor<?>) monitor; for (Monitor<?> m : cm.getMonitors()) { monitorMonitonicValues(m); } } else if (isCounter(monitor.getConfig())) { polledGauge(monitor.getConfig()) .monitorMonotonicCounter(monitor, m -> ((Number) m.getValue()).longValue()); } } }
.withName(TASK_COUNT) .withTag(idTag) .monitorMonotonicCounter(threadPool, ThreadPoolExecutor::getTaskCount); PolledMeter.using(registry) .withName(COMPLETED_TASK_COUNT) .withTag(idTag) .monitorMonotonicCounter(threadPool, ThreadPoolExecutor::getCompletedTaskCount); PolledMeter.using(registry) .withName(CURRENT_THREADS_BUSY)
.withName(TASK_COUNT) .withTag(idTag) .monitorMonotonicCounter(threadPool, ThreadPoolExecutor::getTaskCount); PolledMeter.using(registry) .withName(COMPLETED_TASK_COUNT) .withTag(idTag) .monitorMonotonicCounter(threadPool, ThreadPoolExecutor::getCompletedTaskCount); PolledMeter.using(registry) .withName(CURRENT_THREADS_BUSY)
@Test public void nonMonotonicUpdates() { AtomicLong count = new AtomicLong(); AtomicLong c = PolledMeter.using(registry).withId(id).monitorMonotonicCounter(count); Counter counter = registry.counter(id); update(); Assertions.assertEquals(0L, counter.count()); c.set(42L); update(); Assertions.assertEquals(42L, counter.count()); // Should not update the counter because it is lower, but must update // the previous recorded value c.set(21L); update(); Assertions.assertEquals(42L, counter.count()); // Make sure a subsequent increase is detected c.set(23L); update(); Assertions.assertEquals(44L, counter.count()); }
/** * Poll the value of the provided {@link Number} and update a counter with the delta * since the last time the value was sampled. The implementation provided must * be thread safe. The most common usages of this are to monitor instances of * {@link java.util.concurrent.atomic.AtomicInteger}, * {@link java.util.concurrent.atomic.AtomicLong}, or * {@link java.util.concurrent.atomic.LongAdder}. For more information see * {@link #monitorMonotonicCounter(Object, ToLongFunction)}. * * @param number * Thread-safe implementation of {@link Number} used to access the value. * @return * The number that was passed in to allow the builder to be used inline as part * of an assignment. */ public <T extends Number> T monitorMonotonicCounter(T number) { return monitorMonotonicCounter(number, Number::longValue); }
/** * Poll the value of the provided {@link Number} and update a counter with the delta * since the last time the value was sampled. The implementation provided must * be thread safe. The most common usages of this are to monitor instances of * {@link java.util.concurrent.atomic.AtomicInteger}, * {@link java.util.concurrent.atomic.AtomicLong}, or * {@link java.util.concurrent.atomic.LongAdder}. For more information see * {@link #monitorMonotonicCounter(Object, ToLongFunction)}. * * @param number * Thread-safe implementation of {@link Number} used to access the value. * @return * The number that was passed in to allow the builder to be used inline as part * of an assignment. */ public <T extends Number> T monitorMonotonicCounter(T number) { return monitorMonotonicCounter(number, Number::longValue); }
@Test public void usingLongAdder() { LongAdder count = new LongAdder(); LongAdder c = PolledMeter.using(registry).withId(id).monitorMonotonicCounter(count); Assertions.assertSame(count, c); Counter counter = registry.counter(id); update(); Assertions.assertEquals(0L, counter.count()); c.increment(); update(); Assertions.assertEquals(1L, counter.count()); c.add(42); update(); Assertions.assertEquals(43L, counter.count()); }
@Test public void usingAtomicLong() { AtomicLong count = new AtomicLong(); AtomicLong c = PolledMeter.using(registry).withId(id).monitorMonotonicCounter(count); Assertions.assertSame(count, c); Counter counter = registry.counter(id); update(); Assertions.assertEquals(0L, counter.count()); c.incrementAndGet(); update(); Assertions.assertEquals(1L, counter.count()); c.addAndGet(42); update(); Assertions.assertEquals(43L, counter.count()); }
private static void monitorMonitonicValues(Monitor<?> monitor) { if (!(monitor instanceof SpectatorMonitor)) { if (monitor instanceof CompositeMonitor<?>) { CompositeMonitor<?> cm = (CompositeMonitor<?>) monitor; for (Monitor<?> m : cm.getMonitors()) { monitorMonitonicValues(m); } } else if (isCounter(monitor.getConfig())) { polledGauge(monitor.getConfig()) .monitorMonotonicCounter(monitor, m -> ((Number) m.getValue()).longValue()); } } }
AnnotatedNumberMonitor(MonitorConfig config, Object object, AccessibleObject field) { super(config); this.object = object; this.field = field; if ("COUNTER".equals(config.getTags().getValue("type"))) { SpectatorContext.polledGauge(config) .monitorMonotonicCounter(this, m -> m.getValue(0).longValue()); } else { SpectatorContext.polledGauge(config) .monitorValue(this, m -> m.getValue(0).doubleValue()); } }
@Test public void removeGauge() throws Exception { LongAdder v = PolledMeter.using(registry).withId(id).monitorMonotonicCounter(new LongAdder()); Assertions.assertEquals(1, registry.state().size()); PolledMeter.remove(registry, id); Assertions.assertEquals(0, registry.state().size()); }
@Test public void expire() throws Exception { WeakReference<LongAdder> ref = new WeakReference<>( PolledMeter.using(registry).withId(id).monitorMonotonicCounter(new LongAdder())); while (ref.get() != null) { System.gc(); } Assertions.assertEquals(1, registry.state().size()); update(); Assertions.assertEquals(0, registry.state().size()); }