Class AnchoredClock


  • @Immutable
    final class AnchoredClock
    extends java.lang.Object
    A utility for returning wall times anchored to a given point in time. Wall time measurements will not be taken from the system, but instead are computed by adding monotonic time to the anchor point.

    This is needed because Java has lower granularity for epoch times and tracing events are recorded more often. There is also a performance improvement in avoiding referencing the system's wall time where possible. Instead of computing a true wall time for every timestamp within a trace, we compute it once at the local root and then anchor all descendant span timestamps to this root's timestamp.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private AnchoredClock​(Clock clock, long epochNanos, long nanoTime)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static AnchoredClock create​(Clock clock)
      Returns a AnchoredClock.
      (package private) long now()
      Returns the current epoch timestamp in nanos calculated using System.nanoTime() since the reference time read in the constructor.
      (package private) long startTime()
      Returns the start time in nanos of this AnchoredClock.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • clock

        private final Clock clock
      • epochNanos

        private final long epochNanos
      • nanoTime

        private final long nanoTime
    • Constructor Detail

      • AnchoredClock

        private AnchoredClock​(Clock clock,
                              long epochNanos,
                              long nanoTime)
    • Method Detail

      • create

        public static AnchoredClock create​(Clock clock)
        Returns a AnchoredClock.
        Parameters:
        clock - the Clock to be used to read the current epoch time and nanoTime.
        Returns:
        a MonotonicClock.
      • now

        long now()
        Returns the current epoch timestamp in nanos calculated using System.nanoTime() since the reference time read in the constructor. This time can be used for computing durations.
        Returns:
        the current epoch timestamp in nanos.
      • startTime

        long startTime()
        Returns the start time in nanos of this AnchoredClock.