Package io.opentelemetry.sdk.common
Interface Clock
@ThreadSafe
public interface Clock
Interface for getting the current time.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ClockReturns a defaultClockwhich reads from system time.longnanoTime()Returns a time measurement with nanosecond precision that can only be used to calculate elapsed time.longnow()Returns the current epoch timestamp in nanos from this clock.default longnow(boolean highPrecision) Returns the current epoch timestamp in nanos from this clock.
-
Method Details
-
getDefault
Returns a defaultClockwhich reads from system time. -
now
long now()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 usingnanoTime(). 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
now(boolean)withhighPrecision=true. -
now
default long now(boolean highPrecision) Returns the current epoch timestamp in nanos from this clock.This overload of
now()includes ahighPrecisionargument 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 ofSystem.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 ofhighPrecision.See
now()javadoc for details on usage.- Since:
- 1.38.0
-
nanoTime
long nanoTime()Returns a time measurement with nanosecond precision that can only be used to calculate elapsed time.
-