8000 use contextvars backport for aiohttp and sanic support in py3.6 by antonio-antuan · Pull Request #293 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

use contextvars backport for aiohttp and sanic support in py3.6 #293

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ format: .venv
.PHONY: format

test: .venv
@$(VENV_PATH)/bin/tox -e py2.7,py3.7
@$(VENV_PATH)/bin/tox -e py2.7,py3.7,py3.6
.PHONY: test

test-all: .venv
Expand Down
7 changes: 3 additions & 4 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ class AioHttpIntegration(Integration):
@staticmethod
def setup_once():
# type: () -> None
if sys.version_info < (3, 7):
if sys.version_info < (3, 7) and 'aiocontextvars' not in sys.modules:
# We better have contextvars or we're going to leak state between
# requests.
raise RuntimeError(
"The aiohttp integration for Sentry requires Python 3.7+"
)
raise RuntimeError("The aiohttp integration for Sentry requires Python 3.7+ "
" or aiocontextvars package")

ignore_logger("aiohttp.server")

Expand Down
9 changes: 5 additions & 4 deletions sentry_sdk/integrations/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ class SanicIntegration(Integration):
@staticmethod
def setup_once():
# type: () -> None
if sys.version_info < (3, 7):
# Sanic is async. We better have contextvars or we're going to leak
# state between requests.
raise RuntimeError("The sanic integration for Sentry requires Python 3.7+")
if sys.version_info < (3, 7) and 'aiocontextvars' not in sys.modules:
# We better have contextvars or we're going to leak state between
# requests.
raise RuntimeError("The sanic integration for Sentry requires Python 3.7+ "
" or aiocontextvars package")

# Sanic 0.8 and older creates a logger named "root" and puts a
# stringified version of every exception in there (without exc_info),
Expand Down
6 changes: 5 additions & 1 deletion sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,12 @@ def realign_remark(remark):
value=rv, metadata={"len": rv_original_length, "rem": rv_remarks}
)


try:
if not PY2 and sys.version_info < (3, 7):
try:
import aiocontextvars
except ImportError:
pass
from contextvars import ContextVar # type: ignore
except ImportError:
from threading import local
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tox==3.7.0
Werkzeug==0.14.1
pytest-localserver==0.4.1
pytest-cov==2.6.0
aiocontextvars==0.2.1
8 changes: 7 additions & 1 deletion tests/integrations/sanic/test_sanic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import random
import asyncio

Expand Down Expand Up @@ -156,7 +158,11 @@ async def task(i):
async def runner():
await asyncio.gather(*(task(i) for i in range(1000)))

asyncio.run(runner())
if sys.version_info < (3, 7):
loop = asyncio.get_event_loop()
loop.run_until_complete(runner())
else:
asyncio.run(runner())

with configure_scope() as scope:
assert not scope._tags
0