Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

[jaeger-client-java] Span class does not report multiple time on multiple invocations of finish() #299

Merged
merged 2 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions jaeger-core/src/main/java/com/uber/jaeger/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Span implements io.opentracing.Span {
private final List<Reference> references;
private SpanContext context;
private List<LogData> logs;
private boolean finished = false; // to prevent the same span from getting reported multiple times

Span(
Tracer tracer,
Expand Down Expand Up @@ -162,6 +163,11 @@ public void finish(long finishMicros) {

private void finishWithDuration(long durationMicros) {
synchronized (this) {
if (finished) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to log a warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - I think it does. It seems important to know that the subsequent calls to finish(), will have no real effect. Making the change.

}
finished = true;

this.durationMicroseconds = durationMicros;
}

Expand Down
16 changes: 16 additions & 0 deletions jaeger-core/src/test/java/com/uber/jaeger/SpanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ private void testWithTimestamp(boolean accurate) {
assertEquals(999 - 567, span.getDuration());
}

@Test
public void testMultipleSpanFinishDoesNotCauseMultipleReportCalls() {
Span span = (Span) tracer.buildSpan("test-service-name").startManual();
span.finish();

assertEquals(1, reporter.getSpans().size());

Span reportedSpan = reporter.getSpans().get(0);

// new finish calls will not affect size of reporter.getSpans()
span.finish();

assertEquals(1, reporter.getSpans().size());
assertEquals(reportedSpan, reporter.getSpans().get(0));
}

@Test
public void testWithoutTimestampsAccurateClock() {
when(clock.isMicrosAccurate()).thenReturn(true);
Expand Down