-
Notifications
You must be signed in to change notification settings - Fork 473
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
set_tags
missing from python SDK
#1344
Comments
@sl0thentr0py is there any update? :) We are currently thinking how to extend our contextual logging (works in similar fashion as scope stacking in Sentry), but we can't use So we ended up with using
We are having a pretty much nested context managers and works pretty well, but there is a downside of using private scope attribute Would it be okay for you guys to add Thanks |
Can you share your context manager? Will be helpful maybe for us too |
Ok, I came up with this context manager to handle sentry tags - @contextmanager
def sentry_tags_session(logger=None, **tags):
with sentry_sdk.configure_scope() as scope:
try:
curr_tags = scope._tags.copy()
for k, v in tags.items():
scope.set_tag(k, v)
yield
except:
if logger:
# Log here to have the exception attached with the tags too
logger.exception("Failed inside sentry_tags_session context")
raise
finally:
# Must clear explicitly the scope, because it's not handled by sentry_sdk,
# and need also to restore the previous sentry tags because calling `clear()` remove all tags...
scope.clear()
for k, v in curr_tags.items():
scope.set_tag(k, v) |
@bityob we have something similar :) @contextmanager
def create_logging_ctx(context: LoggingContext) -> Generator[None, None, None]:
# NOTE(vojta) unfortunately we can't use Sentry push_scope given the fact it
# wont automatically capture any excentions happening inside the push_scope context
# manager and you have to do it manually. For there reason we have ended up with
# using configure_scope, which actually tweaks the root scope, but with leaving
# context manager keep the values. To make it work, we are capturing the actual tags
# on entering and re-hydrating them on exiting the context manager
with sentry_sdk.configure_scope() as scope:
# there is no official API how to get all the scope tags se we have to use
# private attribute and in meantime we have asked about potentially extending
# the API
# https://github.com/getsentry/sentry-python/issues/1344#issuecomment-1092700348
captured_tags = dict(scope._tags)
_LOGGING_CONTEXTS.append(context)
# setting up the sentry tags to be able to see the information on the issues and
# be able to search by them. Context not suitable for this purpose
# https://docs.sentry.io/platforms/python/enriching-events/tags/
for tag_name, tag_value in context.tags.items():
scope.set_tag(tag_name, tag_value)
# in case that wrapped funcionality inside by the logging context trigger any
# exception we need to make sure we always do clean up aka remove Sentry scopes
# and logging context
try:
yield
finally:
# re-hydrating the captured tags only for the keys which has been set from
# the logging context
for tag_name in context.tags.keys():
value = captured_tags.get(tag_name)
if value is None:
# value set from logging context, but not in the original captured logs
# so instead of rewriting, just removing
scope.remove_tag(tag_name)
continue
scope.set_tag(tag_name, value)
_LOGGING_CONTEXTS.pop() |
`Scope.set_tags` allows multiple tags to be set at the same time by passing the tags to update as a dictionary (or other `Mapping` type). Closes GH-1344
According to unified-api page, each SDK should have a
set_tags
method.https://develop.sentry.dev/sdk/unified-api/
I found it was dicscussed and implemented 2 years ago in this PR #530 but the PR was closed.
Can this feature be added?
Thanks
The text was updated successfully, but these errors were encountered: