Package io.opentelemetry.sdk.common
Interface Clock
@ThreadSafe
public interface Clock
Interface for getting the current time.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Clock
Returns a defaultClock
which reads from system time.long
nanoTime()
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.
-
Method Details
-
getDefault
Returns a defaultClock
which 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;
-
nanoTime
long nanoTime()Returns a time measurement with nanosecond precision that can only be used to calculate elapsed time.
-