question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`set_tags` missing from python SDK

See original GitHub issue

According to unified-api page, each SDK should have a set_tags method.

https://develop.sentry.dev/sdk/unified-api/

image

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
VojtechBartoscommented, Aug 17, 2022

@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()
1reaction
VojtechBartoscommented, Apr 8, 2022

@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 push_scope cause it’s not automatically reporting errors.

So we ended up with using configure_scopeand following

  • capturing scope tags on __enter__
  • setting up specific tags overrides for actual functionality in the context
  • if anything raises exception in the context manager it will have the new tags
  • re-hydrating the captured scope tags on __exit__ to get it to previous state

We are having a pretty much nested context managers and works pretty well, but there is a downside of using private scope attribute _tags given there is not getter.

Would it be okay for you guys to add set_tags and along with that get_tags to Scope? If yes, i can make that change pretty fast. 😃 ⏩ :shipit:

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Sentry-sdk: set tags through extra keyword on logger
I'm upgrading my current code using the raven library to the sentry-sdk library. The Client is created as follows:
Read more >
The API — django-taggit 1.3.0 documentation - Read the Docs
Otherwise sets the object's tags to those specified, removing only the missing tags and adding only the new tags. Use the through_defaults argument...
Read more >
Method: instances.setTags | Compute Engine Documentation
[Output Only] An ID that represents a group of operations, such as when a group of operations results from a bulkInsert API request....
Read more >
Data Tags - OneSignal Documentation
Data tags are custom key : value pairs of string data used to add custom properties or event data to user records within...
Read more >
Performance Monitoring: Sentry SDK API Evolution
The Python and JavaScript SDKs were the test bed where the first concepts were designed and developed. ... { /* ... some code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found