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

Log an error when Scope.close() is called on the incorrect object. #369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
import io.opentracing.ScopeManager;
import io.opentracing.Span;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* {@link ThreadLocalScope} is a simple {@link Scope} implementation that relies on Java's
* thread-local storage primitive.
*
* @see ScopeManager
*/
public class ThreadLocalScope implements Scope {
private static final Logger log = Logger.getLogger(ThreadLocalScope.class.getName());

private final ThreadLocalScopeManager scopeManager;
private final Span wrapped;
private final ThreadLocalScope toRestore;
Expand All @@ -39,6 +44,8 @@ public class ThreadLocalScope implements Scope {
public void close() {
if (scopeManager.tlsScope.get() != this) {
// This shouldn't happen if users call methods in the expected order. Bail out.
log.log(Level.SEVERE, "Scope being closed is not the active instance.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I would log a warning at this point I think.
Also, wrap the log in an if for the level since you forcefully get a stacktrace.

Side note; My own code is a little more lenient with out-of-sequence closing; it won't touch the active scope, but it will 'mark' the wrong one closed. So when the correct one is closed, it will be 'skipped' in the stack unwinding code since it was already closed out of sequence.

I hope you get the idea, sorry it's still rather early in the morning here 😉

new Throwable().fillInStackTrace());
return;
}

Expand Down