@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); }
@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 LongTaskTimer longTaskTimer(Id id) { return com.netflix.spectator.api.patterns.LongTaskTimer.get(this, id); }
/** * 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; }
/** * Measures the time taken for long tasks. * * @param id * Identifier for the metric being registered. * @return * Timer instance with the corresponding id. * @deprecated * Use {@link com.netflix.spectator.api.patterns.LongTaskTimer#get(Registry, Id)} * instead. Scheduled to be removed in 2.0. */ @Deprecated default LongTaskTimer longTaskTimer(Id id) { // Note: this method is only included in the registry for historical reasons to // maintain compatibility. Future patterns should just use the registry not be // created by the registry. return com.netflix.spectator.api.patterns.LongTaskTimer.get(this, id); }
@Test public void patternUsingState() { LongTaskTimer t = LongTaskTimer.get(registry, registry.createId("foo")); long tid = t.start(); clock.addSeconds(60); PolledMeter.update(registry); Gauge g = registry.gauge(registry.createId("foo").withTag(Statistic.duration)); Assertions.assertEquals(60.0, g.value(), 1e-12); t.stop(tid); PolledMeter.update(registry); Assertions.assertEquals(0.0, g.value(), 1e-12); } }
@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; }
/** * Measures the time taken for long tasks. * * @param id * Identifier for the metric being registered. * @return * Timer instance with the corresponding id. * @deprecated * Use {@link com.netflix.spectator.api.patterns.LongTaskTimer#get(Registry, Id)} * instead. Scheduled to be removed in 2.0. */ @Deprecated default LongTaskTimer longTaskTimer(Id id) { // Note: this method is only included in the registry for historical reasons to // maintain compatibility. Future patterns should just use the registry not be // created by the registry. return com.netflix.spectator.api.patterns.LongTaskTimer.get(this, id); }
@Override protected LongTaskTimer newLongTaskTimer(Meter.Id id) { return new SpectatorLongTaskTimer(id, com.netflix.spectator.api.patterns.LongTaskTimer.get(registry, spectatorId(id))); }
@Test public void usingNoopRegistry() { System.setProperty("spectator.api.propagateWarnings", "true"); Registry noop = new NoopRegistry(); LongTaskTimer.get(noop, noop.createId("task")); } }
@Test public void removeOtherType() throws Exception { LongTaskTimer t = LongTaskTimer.get(registry, id); Assertions.assertEquals(3, registry.state().size()); PolledMeter.remove(registry, id); Assertions.assertEquals(3, registry.state().size()); } }
@Test public void testInit() { com.netflix.spectator.api.LongTaskTimer t = LongTaskTimer.get(registry, id); Assertions.assertEquals(t.duration(), 0L); Assertions.assertEquals(t.activeTasks(), 0L); }
@Test public void testStart() { com.netflix.spectator.api.LongTaskTimer t = LongTaskTimer.get(registry, id); long task1 = t.start(); long task2 = t.start(); Assertions.assertFalse(task1 == task2); Assertions.assertEquals(t.activeTasks(), 2); Assertions.assertEquals(t.duration(), 0L); }
@Test public void testStop() { com.netflix.spectator.api.LongTaskTimer t = LongTaskTimer.get(registry, id); long task1 = t.start(); long task2 = t.start(); Assertions.assertEquals(t.activeTasks(), 2); clock.setMonotonicTime(5L); Assertions.assertEquals(t.duration(), 10L); long elapsed1 = t.stop(task1); Assertions.assertEquals(-1L, t.stop(task1)); // second call to stop should return an error Assertions.assertEquals(elapsed1, 5L); Assertions.assertEquals(t.duration(task2), 5L); Assertions.assertEquals(t.duration(task1), -1L); // task is gone, should return default Assertions.assertEquals(t.duration(), 5L); }