Skip to content

Commit

Permalink
Opentracing ScopeManager implementation based on our context (#110)
Browse files Browse the repository at this point in the history
Implementation of ScopeManager based on our own AbstractThreadLocalContext class with the following advantages over the 'standard' opentracing-util ThreadLocalScopeManager:

1. Close is explicitly idempotent; closing more than once has no additional side-effects
(even when finishOnClose is set to true).
2. More predictable behaviour for out-of-order closing of scopes.
Although this is explicitly unsupported by the opentracing specification,
we think having consistent and predictable behaviour is an advantage.
3. Support for {@link nl.talsmasoftware.context.observer.ContextObserver}.
See opentracing/opentracing-java#334 why this is an advantage.

Signed-off-by: Sjoerd Talsma <[email protected]>
  • Loading branch information
sjoerdtalsma committed May 6, 2019
1 parent 65307a3 commit 74a3274
Showing 1 changed file with 11 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,25 @@
*/
public class ContextScopeManager implements ScopeManager, ContextManager<Span> {
/**
* {@inheritDoc}
* Makes the given span the new active span.
*
* @param span The span to become the active span.
* @return a {@code Scope} instance to control the end of the active period for the {@link Span}.
* It is a programming error to neglect to call {@link Scope#close()} on the returned instance.
* @see #activeSpan()
* @param span The span to become the active span.
* @param finishSpanOnClose Whether the span should automatically finish when closing the resulting scope.
* @return The new active scope (must be closed from the same thread).
*/
@Override
public Scope activate(Span span) {
return new ThreadLocalSpanContext(getClass(), span, false);
public Scope activate(Span span, boolean finishSpanOnClose) {
return new ThreadLocalSpanContext(getClass(), span, finishSpanOnClose);
}

/**
* {@inheritDoc}
* The currently active {@link Scope} containing the active span {@link Scope#span()}.
*
* @return The active span or {@code null} if there is none.
* @return the active scope, or {@code null} if none could be found.
*/
@Override
public Span activeSpan() {
ThreadLocalSpanContext activeScope = ThreadLocalSpanContext.current();
return activeScope == null ? null : activeScope.getValue();
public Scope active() {
return ThreadLocalSpanContext.current();
}

/**
Expand All @@ -95,39 +93,14 @@ public Context<Span> initializeNewContext(Span value) {
}

/**
* @return The active span context.
* @return The active span context (this is identical to the active scope).
* @see #active()
*/
@Override
public Context<Span> getActiveContext() {
return ThreadLocalSpanContext.current();
}

/**
* Makes the given span the new active span.
*
* @param span The span to become the active span.
* @param finishSpanOnClose Whether the span should automatically finish when closing the resulting scope.
* @return The new active scope (must be closed from the same thread).
* @deprecated Use {@code activate(Span)} instead and finish explicitly.
*/
@Override
@Deprecated
public Scope activate(Span span, boolean finishSpanOnClose) {
return new ThreadLocalSpanContext(getClass(), span, finishSpanOnClose);
}

/**
* The currently active {@link Scope} containing the active span {@link Scope#span()}.
*
* @return the active scope, or {@code null} if none could be found.
*/
@Override
@Deprecated
public Scope active() {
return ThreadLocalSpanContext.current();
}

/**
* @return String representation for this context manager.
*/
Expand Down

0 comments on commit 74a3274

Please sign in to comment.