@Override public ConcurrentMap<Id, Object> state() { return composite.state(); }
@Override public ConcurrentMap<Id, Object> state() { return impl.state(); }
@Override public ConcurrentMap<Id, Object> state() { return impl.state(); }
/** * Explicitly disable polling for the meter registered with {@code id}. This is optional * and is mostly used if it is desirable for the meter to go away immediately. The polling * will stop automatically when the referred object is garbage collected. See * {@link Builder#monitorValue(Object, ToDoubleFunction)} for more information. */ public static void remove(Registry registry, Id id) { Object obj = registry.state().get(id); if (obj instanceof AbstractMeterState) { registry.state().remove(id, obj); } }
/** * Explicitly disable polling for the meter registered with {@code id}. This is optional * and is mostly used if it is desirable for the meter to go away immediately. The polling * will stop automatically when the referred object is garbage collected. See * {@link Builder#monitorValue(Object, ToDoubleFunction)} for more information. */ public static void remove(Registry registry, Id id) { Object obj = registry.state().get(id); if (obj instanceof AbstractMeterState) { registry.state().remove(id, obj); } }
/** Force the polling of all meters associated with the registry. */ public static void update(Registry registry) { Iterator<Map.Entry<Id, Object>> iter = registry.state().entrySet().iterator(); while (iter.hasNext()) { Map.Entry<Id, Object> entry = iter.next(); if (entry.getValue() instanceof AbstractMeterState) { AbstractMeterState tuple = (AbstractMeterState) entry.getValue(); tuple.doUpdate(registry); if (tuple.hasExpired()) { iter.remove(); } } } }
/** Force the polling of all meters associated with the registry. */ public static void update(Registry registry) { Iterator<Map.Entry<Id, Object>> iter = registry.state().entrySet().iterator(); while (iter.hasNext()) { Map.Entry<Id, Object> entry = iter.next(); if (entry.getValue() instanceof AbstractMeterState) { AbstractMeterState tuple = (AbstractMeterState) entry.getValue(); tuple.doUpdate(registry); if (tuple.hasExpired()) { iter.remove(); } } } }
/** * Update the registry if this meter is not expired, otherwise cleanup any state * associated with this meter. */ @SuppressWarnings("PMD.AvoidCatchingThrowable") void doUpdate(Registry registry) { if (hasExpired()) { registry.state().remove(id()); } else { try { update(registry); } catch (Throwable t) { LOGGER.trace("uncaught exception from gauge function for [{}]", id(), t); throw t; } } }
/** * Update the registry if this meter is not expired, otherwise cleanup any state * associated with this meter. */ @SuppressWarnings("PMD.AvoidCatchingThrowable") void doUpdate(Registry registry) { if (hasExpired()) { registry.state().remove(id()); } else { try { update(registry); } catch (Throwable t) { LOGGER.trace("uncaught exception from gauge function for [{}]", id(), t); throw t; } } }
/** * Provided for backwards compatibility to support the {@link Registry#register(Meter)} * method. Use the builder created with {@link #using(Registry)} instead. * * @deprecated This method only exists to allow for backwards compatibility and should * be considered an internal detail. Scheduled to be removed in 2.0. */ @Deprecated public static void monitorMeter(Registry registry, Meter meter) { ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, meter.id(), MeterState::new); if (!(c instanceof MeterState)) { Utils.propagateTypeError(registry, meter.id(), MeterState.class, c.getClass()); } else { MeterState t = (MeterState) c; t.add(meter); long delay = registry.config().gaugePollingFrequency().toMillis(); t.schedule(registry, null, delay); } }
/** * Provided for backwards compatibility to support the {@link Registry#register(Meter)} * method. Use the builder created with {@link #using(Registry)} instead. * * @deprecated This method only exists to allow for backwards compatibility and should * be considered an internal detail. Scheduled to be removed in 2.0. */ @Deprecated public static void monitorMeter(Registry registry, Meter meter) { ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, meter.id(), MeterState::new); if (!(c instanceof MeterState)) { Utils.propagateTypeError(registry, meter.id(), MeterState.class, c.getClass()); } else { MeterState t = (MeterState) c; t.add(meter); long delay = registry.config().gaugePollingFrequency().toMillis(); t.schedule(registry, null, delay); } }
/** * Create a new instance. * * @param registry * Registry to use. * @param id * Identifier for the metric being registered. * @return * Counter instance. */ public static IntervalCounter get(Registry registry, Id id) { ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, id, i -> new IntervalCounter(registry, i)); if (!(c instanceof IntervalCounter)) { Utils.propagateTypeError(registry, id, IntervalCounter.class, c.getClass()); c = new IntervalCounter(new NoopRegistry(), id); } return (IntervalCounter) c; }
/** * Create a new instance. * * @param registry * Registry to use. * @param id * Identifier for the metric being registered. * @return * Counter instance. */ public static IntervalCounter get(Registry registry, Id id) { ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, id, i -> new IntervalCounter(registry, i)); if (!(c instanceof IntervalCounter)) { Utils.propagateTypeError(registry, id, IntervalCounter.class, c.getClass()); c = new IntervalCounter(new NoopRegistry(), id); } return (IntervalCounter) c; }
@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()); }
final ValueState<T> tuple = new ValueState<>(gauge); ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, id, i -> tuple); if (!(c instanceof ValueState)) {
final ValueState<T> tuple = new ValueState<>(gauge); ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, id, i -> tuple); if (!(c instanceof ValueState)) {
/** * Creates a timer for tracking long running tasks. * * @param registry * Registry to use. * @param id * Identifier for the metric being registered. * @return * Timer instance. */ public static LongTaskTimer get(Registry registry, Id id) { ConcurrentMap<Id, Object> state = registry.state(); Object obj = Utils.computeIfAbsent(state, id, i -> { LongTaskTimer timer = new LongTaskTimer(registry, id); PolledMeter.using(registry) .withId(id) .withTag(Statistic.activeTasks) .monitorValue(timer, LongTaskTimer::activeTasks); PolledMeter.using(registry) .withId(id) .withTag(Statistic.duration) .monitorValue(timer, t -> t.duration() / NANOS_PER_SECOND); return timer; }); if (!(obj instanceof LongTaskTimer)) { Utils.propagateTypeError(registry, id, LongTaskTimer.class, obj.getClass()); obj = new LongTaskTimer(new NoopRegistry(), id); } return (LongTaskTimer) obj; }
/** * Creates a timer for tracking long running tasks. * * @param registry * Registry to use. * @param id * Identifier for the metric being registered. * @return * Timer instance. */ public static LongTaskTimer get(Registry registry, Id id) { ConcurrentMap<Id, Object> state = registry.state(); Object obj = Utils.computeIfAbsent(state, id, i -> { LongTaskTimer timer = new LongTaskTimer(registry, id); PolledMeter.using(registry) .withId(id) .withTag(Statistic.activeTasks) .monitorValue(timer, LongTaskTimer::activeTasks); PolledMeter.using(registry) .withId(id) .withTag(Statistic.duration) .monitorValue(timer, t -> t.duration() / NANOS_PER_SECOND); return timer; }); if (!(obj instanceof LongTaskTimer)) { Utils.propagateTypeError(registry, id, LongTaskTimer.class, obj.getClass()); obj = new LongTaskTimer(new NoopRegistry(), id); } return (LongTaskTimer) obj; }