Sentry-python: Improve documentation for configure_scope()

Created on 30 Nov 2018  路  10Comments  路  Source: getsentry/sentry-python

The current documentation does not make it clear whether this code is permanent or if it is reverted as soon as the with block is exited:

from sentry_sdk import configure_scope

with configure_scope() as scope:
    scope.set_tag("my-tag", "my value")
    scope.user = {'id': 42, 'email': '[email protected]'}

I assume it is permanent, because if it is not then there would be no difference from push_scope, but it should be made explicit with an example (the opposite of the example given for push_scope would make it clear).

doc enhancement

Most helpful comment

This is the one that contains the counter-example:

https://docs.sentry.io/enriching-error-data/scopes/?platform=python

from sentry_sdk import push_scope, capture_exception

with push_scope() as scope:
    scope.set_tag("my-tag", "my value")
    scope.level = 'warning'
    # will be tagged with my-tag="my value"
    capture_exception(Exception("my error"))

# will not be tagged with my-tag
capture_exception(Exception("my other error"))

Inverting that example would give something like this:

from sentry_sdk import configure_scope, capture_exception

with configure_scope() as scope:
    scope.set_tag("my-tag", "my value")
    scope.level = 'warning'
    # will be tagged with my-tag="my value"
    capture_exception(Exception("my error"))

# will also be tagged with my-tag
capture_exception(Exception("my other error"))

All 10 comments

This is the one that contains the counter-example:

https://docs.sentry.io/enriching-error-data/scopes/?platform=python

from sentry_sdk import push_scope, capture_exception

with push_scope() as scope:
    scope.set_tag("my-tag", "my value")
    scope.level = 'warning'
    # will be tagged with my-tag="my value"
    capture_exception(Exception("my error"))

# will not be tagged with my-tag
capture_exception(Exception("my other error"))

Inverting that example would give something like this:

from sentry_sdk import configure_scope, capture_exception

with configure_scope() as scope:
    scope.set_tag("my-tag", "my value")
    scope.level = 'warning'
    # will be tagged with my-tag="my value"
    capture_exception(Exception("my error"))

# will also be tagged with my-tag
capture_exception(Exception("my other error"))

Is there any way to provide a function rather than a dict that gets run at exception time? I think js does this.

If this:

with configure_scope() as scope:
    scope.set_tag("my-tag", "my value")
    scope.level = 'warning'
    # will be tagged with my-tag="my value"
    capture_exception(Exception("my error"))

# will also be tagged with my-tag
capture_exception(Exception("my other error"))

then why is configure_scope() a context manager? What does it do in its __exit__ method?

@aviv-ebates nothing. it will be gone or at least superseded soon. configure_scope is a concept that didn't translate well to python from other languages

Can I use before_send for setting custom tag? I have the following code and it doesn't set the tag, or at least it is not visible in the issue. Am I missing something?

sentry_sdk.init(
     dsn=app.config['SENTRY_DSN'],
     in_app_exclude=logging_exclusions,
     integrations=[FlaskIntegration()],
     before_send=self.add_request_id_sentry_python)    

def add_request_id_sentry_python(self, event, hint):
        from sentry_sdk import push_scope
        with push_scope() as scope:
            scope.set_tag('request_id', '214')
        return event

if you want to set a tag in before_send, scopes cannot be used.

I suggest to not use before-send and instead to do this in your request handler:

with configure_scope() as scope:
    scope.set_tag("request_id", 214)

The configure_scope() call shouldn't offer the context manager interface, as it's misleading (IMHO). Plain function calls to set scope parameters "from now on" should be sufficient and clearer.

We're revamping the documentation right now, and while there will be shortcuts to setting tags, the context manager will stay.

Was this page helpful?
0 / 5 - 0 ratings