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

Commit

Permalink
Make Tracer extends Closable. (#329)
Browse files Browse the repository at this point in the history
* Make Tracer extends Closable.
  • Loading branch information
carlosalberto authored Mar 1, 2019
1 parent 748a229 commit 8176208
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
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.
* 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();

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

0 comments on commit 8176208

Please sign in to comment.