@Override public Iterable<Measurement> measure() { final List<Measurement> ms = new ArrayList<>(2); final long now = clock.wallTime(); final double durationSeconds = duration() / NANOS_PER_SECOND; ms.add(new Measurement(id.withTag(Statistic.duration), now, durationSeconds)); ms.add(new Measurement(id.withTag(Statistic.activeTasks), now, activeTasks())); return ms; } }
@Override public Iterable<Measurement> measure() { final List<Measurement> ms = new ArrayList<>(2); final long now = clock.wallTime(); final double durationSeconds = duration() / NANOS_PER_SECOND; ms.add(new Measurement(id.withTag(Statistic.duration), now, durationSeconds)); ms.add(new Measurement(id.withTag(Statistic.activeTasks), now, activeTasks())); return ms; } }
/** * 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; }
@Test public void stateIsPreservedAcrossGets() { long t1 = LongTaskTimer.get(registry, id).start(); long t2 = LongTaskTimer.get(registry, id).start(); Assertions.assertFalse(t1 == t2); Assertions.assertEquals(LongTaskTimer.get(registry, id).activeTasks(), 2); clock.setMonotonicTime(5L); Assertions.assertEquals(LongTaskTimer.get(registry, id).duration(), 10L); LongTaskTimer.get(registry, id).stop(t1); Assertions.assertEquals(LongTaskTimer.get(registry, id).duration(), 5L); }