From 07517cb4efacfa1598b44e20ef97c92e4261575d Mon Sep 17 00:00:00 2001 From: Guilherme Scaranse Date: Thu, 18 Nov 2021 12:20:34 -0300 Subject: [PATCH 1/4] Patch eventlet on Sentry SDK --- sentry_sdk/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 5c43fa3cc6..755bb8e555 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1136,9 +1136,12 @@ def _is_contextvars_broken(): pass try: + import greenlet from eventlet.patcher import is_monkey_patched # type: ignore if is_monkey_patched("thread"): + version_tuple = tuple([int(part) for part in greenlet.__version__.split(".")[:2]]) + if is_monkey_patched("thread") and version_tuple < (0, 5): return True except ImportError: pass @@ -1197,6 +1200,15 @@ def _get_contextvars(): return True, ContextVar except ImportError: pass + # For eventlet + try: + import eventlet # type: ignore + from eventlet.patcher import is_monkey_patched # type: ignore + if is_monkey_patched('thread'): + contextvars = eventlet.import_patched('contextvars') + return True, contextvars.ContextVar + except ImportError: + pass # Fall back to basic thread-local usage. From e584049f5772d81c06b6648118a1b28db41d543b Mon Sep 17 00:00:00 2001 From: Guilherme Scaranse Date: Thu, 18 Nov 2021 12:21:36 -0300 Subject: [PATCH 2/4] Update sequence --- sentry_sdk/utils.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 755bb8e555..c17b8597fc 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1194,13 +1194,7 @@ def _get_contextvars(): pass else: # On Python 3.7 contextvars are functional. - try: - from contextvars import ContextVar - - return True, ContextVar - except ImportError: - pass - # For eventlet + # Special check for eventlet try: import eventlet # type: ignore from eventlet.patcher import is_monkey_patched # type: ignore @@ -1209,6 +1203,14 @@ def _get_contextvars(): return True, contextvars.ContextVar except ImportError: pass + + try: + from contextvars import ContextVar + + return True, ContextVar + except ImportError: + pass + # Fall back to basic thread-local usage. From b411c96a747e50f133b327be5da006a083647c7c Mon Sep 17 00:00:00 2001 From: Guilherme Scaranse Date: Fri, 19 Nov 2021 11:03:09 -0300 Subject: [PATCH 3/4] Remove redundant stuff Co-authored-by: Sergey Shepelev --- sentry_sdk/utils.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index c17b8597fc..f0414ff666 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1139,7 +1139,6 @@ def _is_contextvars_broken(): import greenlet from eventlet.patcher import is_monkey_patched # type: ignore - if is_monkey_patched("thread"): version_tuple = tuple([int(part) for part in greenlet.__version__.split(".")[:2]]) if is_monkey_patched("thread") and version_tuple < (0, 5): return True @@ -1194,16 +1193,6 @@ def _get_contextvars(): pass else: # On Python 3.7 contextvars are functional. - # Special check for eventlet - try: - import eventlet # type: ignore - from eventlet.patcher import is_monkey_patched # type: ignore - if is_monkey_patched('thread'): - contextvars = eventlet.import_patched('contextvars') - return True, contextvars.ContextVar - except ImportError: - pass - try: from contextvars import ContextVar @@ -1211,9 +1200,7 @@ def _get_contextvars(): except ImportError: pass - # Fall back to basic thread-local usage. - from threading import local return False, _make_threadlocal_contextvars(local) From 55ce654c1e2ad8dde86fae89e6b47deca53ea719 Mon Sep 17 00:00:00 2001 From: Guilherme Scaranse Date: Fri, 19 Nov 2021 14:50:04 -0300 Subject: [PATCH 4/4] fix codestyle --- sentry_sdk/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index f0414ff666..7bcf0075f3 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1201,6 +1201,7 @@ def _get_contextvars(): pass # Fall back to basic thread-local usage. + from threading import local return False, _make_threadlocal_contextvars(local)