Skip to content
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

Implement scope.close method #83

Closed
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
20 changes: 4 additions & 16 deletions src/main/java/brave/opentracing/BraveScopeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,20 @@ public final class BraveScopeManager implements ScopeManager {

/**
* This api's only purpose is to retrieve the {@link Scope#span() span}.
*
* Calling {@link Scope#close() close } on the returned scope has no effect on the active span
*/
@Override public Scope active() {
BraveSpan span = currentSpan();
if (span == null) return null;
return new Scope() {
@Override public void close() {
// no-op
}

@Override public Span span() {
return span;
}
};
return currentSpan();
}

/** Attempts to get a span from the current api, falling back to brave's native one */
BraveSpan currentSpan() {
BraveScope currentSpan() {
BraveScope scope = currentScopes.get().peekFirst();
if (scope != null) {
return scope.span();
return scope;
} else {
brave.Span braveSpan = tracer.currentSpan();
if (braveSpan != null) {
return new BraveSpan(tracer, braveSpan, BraveSpan.EMPTY_ENDPOINT);
return newScope(new BraveSpan(tracer, braveSpan, BraveSpan.EMPTY_ENDPOINT), false);
}
}
return null;
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/brave/opentracing/BraveScopeManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ public class BraveScopeManagerTest {
}
}

@Test public void closeScopeFromScopeManager() {
Scope scopeA = opentracing.buildSpan("spanA").startActive(false);
Scope scopeB = opentracing.scopeManager().active();
assertThat(scopeB).isEqualTo(scopeA);
assertThat(brave.currentTraceContext().get())
.isEqualTo(((BraveSpan)opentracing.scopeManager().active().span()).unwrap().context());

scopeA.close();
assertThat(brave.currentTraceContext().get())
.isEqualTo(null);
assertThat(opentracing.scopeManager().active())
.isEqualTo(null);
}

@After public void clear() {
Tracing current = Tracing.current();
if (current != null) current.close();
Expand Down