Thread safety for tagging #2409
-
Hi, quick question I have a multithread application that we use to Sentry to capture our exception. class BusinessLogic {
public void doSomething(final UserRegion region) {
try {
doBusinessLogic();
} catch(Exception exception) {
Sentry.setTag("user_region", region.current());
Sentry.captureException(exception);
}
}
} I mean a situation where two different threads with different UserRegion call that method but |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hello @krystianzybala. Each thread has their own 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's some more docs on the topic: https://docs.sentry.io/platforms/java/enriching-events/scopes/ |
Beta Was this translation helpful? Give feedback.
-
Thank you :) |
Beta Was this translation helpful? Give feedback.
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:This only sets the tag for one call by creating a scope, setting the tag, capturing the exception and then popping the scope.
Here'…