-
Notifications
You must be signed in to change notification settings - Fork 917
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
#13295: Instrumenter.getNanos() returns non-epoch timestamp. #13535
Open
wdengw
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
wdengw:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,20 +25,23 @@ | |
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import io.opentelemetry.context.propagation.TextMapGetter; | ||
import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil; | ||
import io.opentelemetry.instrumentation.api.internal.SchemaUrlProvider; | ||
import io.opentelemetry.instrumentation.api.internal.SpanKey; | ||
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider; | ||
import io.opentelemetry.sdk.common.InstrumentationScopeInfo; | ||
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; | ||
import io.opentelemetry.sdk.trace.data.LinkData; | ||
import io.opentelemetry.sdk.trace.data.StatusData; | ||
import java.time.Instant; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import javax.annotation.Nullable; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
@@ -581,6 +584,45 @@ void instrumentationVersion_default() { | |
span -> span.hasName("span").hasInstrumentationScopeInfo(expectedLibraryInfo))); | ||
} | ||
|
||
@Test | ||
void instrumenterGetNanosWithNullArgument() { | ||
AtomicReference<Long> endTime = new AtomicReference<>(); | ||
OperationListener operationListener = | ||
new OperationListener() { | ||
@Override | ||
public Context onStart(Context context, Attributes startAttributes, long startNanos) { | ||
return context; | ||
} | ||
|
||
@Override | ||
public void onEnd(Context context, Attributes endAttributes, long endNanos) { | ||
endTime.set(endNanos); | ||
} | ||
}; | ||
|
||
InstrumenterBuilder<Map<String, String>, Map<String, String>> builder = | ||
Instrumenter.builder( | ||
otelTesting.getOpenTelemetry(), "test-instrumentation", name -> "span"); | ||
builder.addOperationListener(operationListener); | ||
InstrumenterUtil.propagateOperationListenersToOnEnd(builder); | ||
|
||
Instant startTime = Instant.now(); | ||
Instrumenter<Map<String, String>, Map<String, String>> instrumenter = | ||
builder.buildInstrumenter(); | ||
InstrumenterUtil.startAndEnd( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
instrumenter, Context.root(), emptyMap(), emptyMap(), null, startTime, null); | ||
|
||
otelTesting | ||
.assertTraces() | ||
.hasTracesSatisfyingExactly( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly(span -> span.hasName("span").startsAt(startTime))); | ||
System.out.printf( | ||
"Epoche second of start: %d and end: %d\n", | ||
startTime.getEpochSecond(), endTime.get() / 1_000_000_000); | ||
Assertions.assertThat(endTime.get() / 1_000_000_000 == startTime.getEpochSecond()).isTrue(); | ||
} | ||
|
||
@Test | ||
void instrumentationVersion_custom() { | ||
Instrumenter<Map<String, String>, Map<String, String>> instrumenter = | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time measurements should use a monotonic time source,
Instant.now
is not monotonicThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a return statement. It is to replace the null input arguement with the same Instant object with current time, then goes to line 272 to calculate the a monotonic time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you have misunderstood what monotonic means here. The problem with using the "wall clock" time for like
Instant.now
for measuring elapsed time is that it is affected by system time adjustments. If system time is adjusted either by user or ntp between start or stop then stop can be before start which would result in a negative duration. Monotonic time only goes forward and is not adjusted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If system time is adjusted either by user or ntp between start or stop, then your same argument applies to line 272, where the input argument time is not null. Are we saying that we should not use the input argument time as all? If so, the whole logic in this getNanos() should be changed accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a private method it seems to be working correctly for the desired public and internal API behaviors. If it helps, maybe just add a comment
This must be called for start/end times either both null or both non-null
without other changes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would apply if both the start and end instances were constructed by calling
Instant.now
, but that is not what we do. Have a look at https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/Timer.java