Skip to content

Commit

Permalink
Simplify span end time.
Browse files Browse the repository at this point in the history
The only reason to have now() returned in getEndEpochNanos seems to be
that it kinda makes sense for getLatencyMs, but that method is
package-private and never used.

See also
open-telemetry/opentelemetry-specification#373 (comment).
  • Loading branch information
Oberon00 committed Dec 12, 2019
1 parent 9afb1e4 commit 4582da8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ public InstrumentationLibraryInfo getInstrumentationLibraryInfo() {
}

/**
* Returns the end nano time (see {@link System#nanoTime()}). If the current {@code Span} is not
* ended then returns {@link Clock#nanoTime()}.
* Returns the end nano time (see {@link System#nanoTime()}) or zero if the current {@code Span}
* is not ended.
*
* @return the end nano time.
*/
private long getEndEpochNanos() {
synchronized (lock) {
return getEndNanoTimeInternal();
return endEpochNanos;
}
}

Expand Down Expand Up @@ -291,24 +291,6 @@ Map<String, AttributeValue> getAttributes() {
}
}

/**
* Returns the latency of the {@code Span} in nanos. If still active then returns now() - start
* time.
*
* @return the latency of the {@code Span} in nanos.
*/
long getLatencyNs() {
synchronized (lock) {
return getEndNanoTimeInternal() - startEpochNanos;
}
}

// Use getEndNanoTimeInternal to avoid over-locking.
@GuardedBy("lock")
private long getEndNanoTimeInternal() {
return hasBeenEnded ? endEpochNanos : clock.now();
}

/**
* Returns the kind of this {@code Span}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void toSpanData_ActiveSpan() {
Collections.singletonList(link),
SPAN_NEW_NAME,
startEpochNanos,
testClock.now(),
0,
Status.OK);
} finally {
span.end();
Expand Down Expand Up @@ -243,32 +243,6 @@ public void getAndUpdateSpanName() {
}
}

@Test
public void getLatencyNs_ActiveSpan() {
RecordEventsReadableSpan span = createTestSpan(Kind.INTERNAL);
try {
testClock.advanceMillis(MILLIS_PER_SECOND);
long elapsedTimeNanos1 = testClock.now() - startEpochNanos;
assertThat(span.getLatencyNs()).isEqualTo(elapsedTimeNanos1);
testClock.advanceMillis(MILLIS_PER_SECOND);
long elapsedTimeNanos2 = testClock.now() - startEpochNanos;
assertThat(span.getLatencyNs()).isEqualTo(elapsedTimeNanos2);
} finally {
span.end();
}
}

@Test
public void getLatencyNs_EndedSpan() {
RecordEventsReadableSpan span = createTestSpan(Kind.INTERNAL);
testClock.advanceMillis(MILLIS_PER_SECOND);
span.end();
long elapsedTimeNanos = testClock.now() - startEpochNanos;
assertThat(span.getLatencyNs()).isEqualTo(elapsedTimeNanos);
testClock.advanceMillis(MILLIS_PER_SECOND);
assertThat(span.getLatencyNs()).isEqualTo(elapsedTimeNanos);
}

@Test
public void setAttribute() {
RecordEventsReadableSpan span = createTestRootSpan();
Expand Down

0 comments on commit 4582da8

Please sign in to comment.