Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Make Tracer extends Closable. #329

Merged
merged 4 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 16 additions & 1 deletion opentracing-api/src/main/java/io/opentracing/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
*/
package io.opentracing;

import java.io.Closeable;

import io.opentracing.propagation.Format;
import io.opentracing.tag.Tag;

/**
* Tracer is a simple, thin interface for Span creation and propagation across arbitrary transports.
*/
public interface Tracer {
public interface Tracer extends Closeable {

/**
* @return the current {@link ScopeManager}, which may be a noop but may not be null.
Expand Down Expand Up @@ -117,6 +119,19 @@ public interface Tracer {
*/
<C> SpanContext extract(Format<C> format, C carrier);

/**
* Closes the Tracer, and tries to flush the in-memory collection to the configured persistance store.
*
* <p>
* The close method should be considered idempotent; closing an already closed Tracer should not raise an error.
* Spans that are created or finished after a Tracer has been closed may or may not be flushed.
Copy link
Member

Choose a reason for hiding this comment

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

this text implies that while spans may not be flushed, the tracer must still create them like business as usual, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I'd say so (as mentioned in the spec RFC) - an alternative is to NOT allow Span objects to be created (from where? from the builder? from start?)

* Calling the close method should be considered a synchronous operation. Observe this call may block for
* a relatively long period of time, depending on the internal shutdown.
* <p>
* For stateless tracers, this can be a no-op.
*/
@Override
void close();
Copy link

Choose a reason for hiding this comment

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

Should this @Override?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done ;)


interface SpanBuilder {

Expand Down
10 changes: 10 additions & 0 deletions opentracing-mock/src/main/java/io/opentracing/mock/MockTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class MockTracer implements Tracer {
private final List<MockSpan> finishedSpans = new ArrayList<>();
private final Propagator propagator;
private final ScopeManager scopeManager;
private boolean isClosed;

public MockTracer() {
this(new ThreadLocalScopeManager(), Propagator.TEXT_MAP);
Expand Down Expand Up @@ -280,7 +281,16 @@ public Scope activateSpan(Span span) {
return this.scopeManager.activate(span);
}

@Override
public synchronized void close() {
this.isClosed = true;
this.finishedSpans.clear();
}

synchronized void appendFinishedSpan(MockSpan mockSpan) {
if (isClosed)
return;

this.finishedSpans.add(mockSpan);
this.onSpanFinished(mockSpan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,16 @@ public void testChildOfWithNullParentDoesNotThrowException() {
Span span = tracer.buildSpan("foo").asChildOf(parent).start();
span.finish();
}

@Test
public void testClose() {
MockTracer mockTracer = new MockTracer();
mockTracer.buildSpan("foo").start().finish();

mockTracer.close();
assertEquals(0, mockTracer.finishedSpans().size());

mockTracer.buildSpan("foo").start().finish();
assertEquals(0, mockTracer.finishedSpans().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {}
@Override
public <C> SpanContext extract(Format<C> format, C carrier) { return NoopSpanContextImpl.INSTANCE; }

@Override
public void close() {}

@Override
public String toString() { return NoopTracer.class.getSimpleName(); }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ public Scope activateSpan(Span span) {
return tracer.activateSpan(span);
}

@Override
public void close() {
tracer.close();
}

@Override
public String toString() {
return GlobalTracer.class.getSimpleName() + '{' + tracer + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static org.junit.Assert.fail;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

Expand Down Expand Up @@ -225,6 +226,16 @@ public void testDelegation_extract() {
verifyNoMoreInteractions(mockTracer, mockFormat, mockCarrier);
}

@Test
public void testDelegation_close() {
Tracer mockTracer = mock(Tracer.class);
GlobalTracer.register(mockTracer);
GlobalTracer.get().close();

verify(mockTracer, times(1)).close();
verifyNoMoreInteractions(mockTracer);
}

@Test
public void concurrencyTest() throws InterruptedException, ExecutionException {
final int threadCount = 10;
Expand Down