Skip to content

Age Gauge

See Age Gauge for the concept.

Java does not have a dedicated Age Gauge meter. Use a Polled Meter together with the Functions.AGE helper, which computes the age in seconds from a wall-time millis timestamp stored in an AtomicLong:

public class Job {

  private final AtomicLong lastSuccess;

  @Inject
  public Job(Registry registry) {
    lastSuccess = PolledMeter.using(registry)
        .withName("time.sinceLastSuccess")
        .monitorValue(new AtomicLong(System.currentTimeMillis()), Functions.AGE);
  }

  public void onSuccess() {
    lastSuccess.set(System.currentTimeMillis());
  }
}

This reports the seconds elapsed since the last successful run, which is the basis for the Time Since Last Success alerting pattern.

For tests that need to control time, use Functions.age(Clock) with a ManualClock (typically the same clock used by the test Registry):

ManualClock clock = new ManualClock();
Registry registry = new DefaultRegistry(clock);

PolledMeter.using(registry)
    .withName("time.sinceLastSuccess")
    .monitorValue(new AtomicLong(clock.wallTime()), Functions.age(clock));