Class SystemClock

java.lang.Object
io.opentelemetry.sdk.common.SystemClock
All Implemented Interfaces:
Clock

@ThreadSafe final class SystemClock extends Object implements Clock
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final SystemClock
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static Clock
    Returns a SystemClock.
    long
    Returns a time measurement with nanosecond precision that can only be used to calculate elapsed time.
    long
    now()
    Returns the current epoch timestamp in nanos from this clock.
    long
    now(boolean highPrecision)
    Returns the current epoch timestamp in nanos from this clock.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • SystemClock

      private SystemClock()
  • Method Details

    • getInstance

      static Clock getInstance()
      Returns a SystemClock.
    • now

      public long now()
      Description copied from interface: Clock
      Returns the current epoch timestamp in nanos from this clock. This timestamp should only be used to compute a current time. To compute a duration, timestamps should always be obtained using Clock.nanoTime(). For example, this usage is correct.
      
       long startNanos = clock.nanoTime();
       // Spend time...
       long durationNanos = clock.nanoTime() - startNanos;
       

      This usage is NOT correct.

      
       long startNanos = clock.now();
       // Spend time...
       long durationNanos = clock.now() - startNanos;
       

      Calling this is equivalent to calling Clock.now(boolean) with highPrecision=true.

      Specified by:
      now in interface Clock
    • now

      public long now(boolean highPrecision)
      Description copied from interface: Clock
      Returns the current epoch timestamp in nanos from this clock.

      This overload of Clock.now() includes a highPrecision argument which specifies whether the implementation should attempt to resolve higher precision at the potential expense of performance. For example, in java 9+ its sometimes possible to resolve ns precision higher than the ms precision of System.currentTimeMillis(), but doing so incurs a performance penalty which some callers may wish to avoid. In contrast, we don't currently know if resolving ns precision is possible in java 8, regardless of the value of highPrecision.

      See Clock.now() javadoc for details on usage.

      Specified by:
      now in interface Clock
    • nanoTime

      public long nanoTime()
      Description copied from interface: Clock
      Returns a time measurement with nanosecond precision that can only be used to calculate elapsed time.
      Specified by:
      nanoTime in interface Clock
    • toString

      public String toString()
      Overrides:
      toString in class Object