Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaVersionSpecific returns millisecond precision for currentTimeNanos() #6233

Open
scottsue opened this issue Feb 16, 2024 · 6 comments
Open
Labels
Bug Something isn't working

Comments

@scottsue
Copy link

scottsue commented Feb 16, 2024

From a Java 8 JVM perspective there are limitations to the precision of time, however looking at the implementation of JavaVersionSpecific.currentTimeNanos() for Java 8, this should return as close as possible, the nanosecond precision since epoch, however it is currently only to millisecond precision.

Steps to reproduce
Create two Spans in the following order

  1. Create and start Span 1 - startEpochNanos is set to millisecond precision
  2. End Span 1 - endEpochNanos is set to nanosecond precision
  3. Create and start Span 2 - startEpochNanos is set to millisecond precision
  4. End Span 2 - endEpochNanos is set to nanosecond precision

It looks like a new AnchoredClock is created for each Span if it is not inheriting from a parentSpan, therefore if the spans are created in less than 1 millisecond of each other, there is a potential the Span1.endEpochNanos is greater than Span2.startEpochNanos

What did you expect to see?
I would expect Span.startEpochNanos to be at nanosecond precision

What did you see instead?
Span.startEpochNanos is at millisecond precision

What version and what artifacts are you using?
Artifacts: opentelemetry-sdk-common
Version: 1.35

Environment
OS: Centos
Runtime: Java 8 OpenJdk

@scottsue scottsue added the Bug Something isn't working label Feb 16, 2024
@breedx-splk
Copy link
Contributor

@scottsue I'm not aware of any reliable way of getting nanosecond precision wall time on Java 8. Do you know of something?

@scottsue
Copy link
Author

@breedx-splk correct, unfortunately not a reliable way. However AnchoredClock does look to attempt to get a close approximation of this. So my thoughts was to perform something similar when calculating Span.startEpochNanos

@breedx-splk
Copy link
Contributor

@breedx-splk correct, unfortunately not a reliable way. However AnchoredClock does look to attempt to get a close approximation of this. So my thoughts was to perform something similar when calculating Span.startEpochNanos

Would the idea then be to anchor to some arbitrary previous time then?

@scottsue
Copy link
Author

scottsue commented Feb 21, 2024

I was just playing around to see how this could work and I created my own version of JavaVersionSpecific. The implementation requiring the following

private static final NanosecondClock SYSTEM_EPOCH_NANO_CLOCK = new NanosecondClock(null);

  public long currentTimeNanos() {
    return SYSTEM_EPOCH_NANO_CLOCK.getNowSinceEpochInNano();
  }

This utilises a static NanoClock which gives the ability to return the nanos since epoch. In essence, this is what AnchoredClock does as well, only that it seems AnchoredClock looks to be recreated if it is not inheriting from a parentSpan? I would say it is possibly feasible to have a static instance of AnchoredClock in my above implementation to give the same functionality?

@breedx-splk
Copy link
Contributor

This utilises a static NanoClock which gives the ability to return the nanos since epoch.

Yeah, but you haven't shared the implementation of that class. Where is that from?

@scottsue
Copy link
Author

The implementation of the NanosecondClock is something that I had to hand quickly internally. However after using this, I suspect that creating a static AnchoredClock could work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants