private static AtomicLong getGauge(String name, String... additionalTags) { String key = className + "." + name + "." + Joiner.on(",").join(additionalTags); return gauges.computeIfAbsent(key, pollTimer -> { Id id = registry.createId(name, getTags(additionalTags)); return PolledMeter.using(registry) .withId(id) .monitorValue(new AtomicLong(0)); }); }
@Override public void registerGauge(String id, Supplier<Number> supplier, String... tagNameValuePairs) { Id metricId = suffixBaseId(id).withTags(tagNameValuePairs); PolledMeter.remove(registry, metricId); PolledMeter.using(registry) .withId(metricId) .monitorValue(supplier, ignore -> supplier.get().doubleValue()); }
/** Create builder for a polled gauge based on the config. */ public static PolledMeter.Builder polledGauge(MonitorConfig config) { long delayMillis = Math.max(Pollers.getPollingIntervals().get(0) - 1000, 5000); Id id = createId(config); PolledMeter.remove(registry, id); return PolledMeter.using(registry) .withId(id) .withDelay(Duration.ofMillis(delayMillis)) .scheduleOn(gaugePool()); }
private static AtomicLong getGauge(String name, String... additionalTags) { String key = className + "." + name + "." + Joiner.on(",").join(additionalTags); return gauges.computeIfAbsent(key, pollTimer -> { Id id = registry.createId(name, getTags(additionalTags)); return PolledMeter.using(registry) .withId(id) .monitorValue(new AtomicLong(0)); }); }
/** * Tells the registry to regularly poll the value of a {@link java.lang.Number} and report * it as a gauge. See {@link #gauge(Id, Number)} for more information. * * @param id * Identifier for the metric being registered. * @param number * Thread-safe implementation of {@link Number} used to access the value. * @return * The number that was passed in so the registration can be done as part of an assignment * statement. * @deprecated * Use {@link PolledMeter} instead. This method has been deprecated to help * reduce confusion between active gauges that are explicitly updated by the * user and passive gauges that are polled in the background. Going forward * the registry methods will only be used for the core types directly updated * by the user. Other patterns such as {@link PolledMeter}s will be handled * separately. Scheduled to be removed in 2.0. */ @Deprecated default <T extends Number> T gauge(Id id, T number) { return PolledMeter.using(this).withId(id).monitorValue(number); }
/** * See {@link #gauge(Id, Object, ToDoubleFunction)} for more information. * * @param id * Identifier for the metric being registered. * @param obj * Object used to compute a value. * @param f * Function that is applied on the value for the number. * @return * The object that was passed in so the registration can be done as part of an assignment * statement. * @deprecated * Use {@link PolledMeter} instead. This method has been deprecated to help * reduce confusion between active gauges that are explicitly updated by the * user and passive gauges that are polled in the background. Going forward * the registry methods will only be used for the core types directly updated * by the user. Other patterns such as {@link PolledMeter}s will be handled * separately. Scheduled to be removed in 2.0. */ @Deprecated default <T> T gauge(Id id, T obj, ToDoubleFunction<T> f) { return PolledMeter.using(this).withId(id).monitorValue(obj, f); }
/** Create builder for a polled gauge based on the config. */ public static PolledMeter.Builder polledGauge(MonitorConfig config) { long delayMillis = Math.max(Pollers.getPollingIntervals().get(0) - 1000, 5000); Id id = createId(config); PolledMeter.remove(registry, id); return PolledMeter.using(registry) .withId(id) .withDelay(Duration.ofMillis(delayMillis)) .scheduleOn(gaugePool()); }
/** Create a new instance. */ Stats(Registry registry, String id) { this.registry = registry; activeCount = PolledMeter.using(registry) .withId(newId(registry, id, "activeThreads")) .monitorValue(new AtomicInteger()); taskExecutionTime = registry.timer(newId(registry, id, "taskExecutionTime")); taskExecutionDelay = registry.timer(newId(registry, id, "taskExecutionDelay")); skipped = registry.counter(newId(registry, id, "skipped")); uncaughtExceptionsId = newId(registry, id, "uncaughtExceptions"); }
/** Create a new instance. */ Stats(Registry registry, String id) { this.registry = registry; activeCount = PolledMeter.using(registry) .withId(newId(registry, id, "activeThreads")) .monitorValue(new AtomicInteger()); taskExecutionTime = registry.timer(newId(registry, id, "taskExecutionTime")); taskExecutionDelay = registry.timer(newId(registry, id, "taskExecutionDelay")); skipped = registry.counter(newId(registry, id, "skipped")); uncaughtExceptionsId = newId(registry, id, "uncaughtExceptions"); }
/** * Create a new IntervalCounter using the given registry and base id. */ IntervalCounter(Registry registry, Id id) { this.clock = registry.clock(); this.id = id; this.counter = registry.counter(id.withTag(Statistic.count)); this.lastUpdated = PolledMeter.using(registry) .withId(id) .withTag(Statistic.duration) .monitorValue(new AtomicLong(0L), Functions.age(clock)); }
/** * Create a new IntervalCounter using the given registry and base id. */ IntervalCounter(Registry registry, Id id) { this.clock = registry.clock(); this.id = id; this.counter = registry.counter(id.withTag(Statistic.count)); this.lastUpdated = PolledMeter.using(registry) .withId(id) .withTag(Statistic.duration) .monitorValue(new AtomicLong(0L), Functions.age(clock)); }
private void recordClientMetrics() { Id clientCall = createCallId(IpcMetric.clientCall.metricName()); PercentileTimer.builder(registry) .withId(clientCall) .build() .record(getLatency(), TimeUnit.NANOSECONDS); }
private void recordClientMetrics() { Id clientCall = createCallId(IpcMetric.clientCall.metricName()); PercentileTimer.builder(registry) .withId(clientCall) .build() .record(getLatency(), TimeUnit.NANOSECONDS); }
private void recordServerMetrics() { Id serverCall = createCallId(IpcMetric.serverCall.metricName()); PercentileTimer.builder(registry) .withId(serverCall) .build() .record(getLatency(), TimeUnit.NANOSECONDS); }
private void recordServerMetrics() { Id serverCall = createCallId(IpcMetric.serverCall.metricName()); PercentileTimer.builder(registry) .withId(serverCall) .build() .record(getLatency(), TimeUnit.NANOSECONDS); }
@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()); }
@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 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()); }
@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()); }
@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()); }