Skip to content

Thread safety for tagging #2409

Answered by adinauer
krystianzybala asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @krystianzybala. Each thread has their own Scope. However the tags you set remain on the scope until removed or until the scope is replaced / popped (e.g. at the end of a request). This code should do what you are looking for:

class BusinessLogic {
    public void doSomething(final UserRegion region) {
        try {
            doBusinessLogic();
        } catch(Exception exception) {
      		Sentry.withScope(scope -> {
        		scope.setTag("user_region", region.current());
            	Sentry.captureException(exception);
      		});
        }
    }
}

This only sets the tag for one call by creating a scope, setting the tag, capturing the exception and then popping the scope.

Here'…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by krystianzybala
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants