Skip to content

Commit ba0ecb7

Browse files
ramshaw888beniwohli
authored andcommitted
Execution context - correctly check if gevent has patched threading.local (#579)
* Typo in call to is_monkey_patched * Adds proper gevent tests * remove pinning of Logbook introduced in #195 * exclude gevent on pypy-2 see gevent/gevent#1380
1 parent 0bce215 commit ba0ecb7

File tree

9 files changed

+111
-3
lines changed

9 files changed

+111
-3
lines changed

.ci/.jenkins_exclude.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,6 @@ exclude:
167167
FRAMEWORK: zerorpc-0.4
168168
- PYTHON_VERSION: python-3.8-rc
169169
FRAMEWORK: zerorpc-0.4
170+
# gevent
171+
- PYTHON_VERSION: pypy-2 # see https://github.com/gevent/gevent/issues/1380, fix will be in gevent 1.5
172+
FRAMEWORK: gevent-newest

.ci/.jenkins_framework.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ FRAMEWORK:
2828
- cassandra-newest
2929
- psutil-newest
3030
- eventlet-newest
31+
- gevent-newest
3132
- zerorpc-0.4
32-

.ci/.jenkins_framework_full.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ FRAMEWORK:
5959
- elasticsearch-5
6060
- elasticsearch-6
6161
- elasticsearch-7
62+
- gevent-newest
6263
- zerorpc-0.4

elasticapm/context/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def threading_local_monkey_patched():
5252
except ImportError:
5353
pass
5454
else:
55-
if is_object_patched("_threading", "local"):
55+
if is_object_patched("threading", "local"):
5656
return True
5757

5858
try:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# BSD 3-Clause License
2+
#
3+
# Copyright (c) 2019, Elasticsearch BV
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this
10+
# list of conditions and the following disclaimer.
11+
#
12+
# * Redistributions in binary form must reproduce the above copyright notice,
13+
# this list of conditions and the following disclaimer in the documentation
14+
# and/or other materials provided with the distribution.
15+
#
16+
# * Neither the name of the copyright holder nor the names of its
17+
# contributors may be used to endorse or promote products derived from
18+
# this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
32+
import pytest # isort:skip
33+
34+
eventlet = pytest.importorskip("eventlet") # isort:skip
35+
36+
from eventlet.patcher import is_monkey_patched
37+
38+
import elasticapm.context
39+
from elasticapm.context.threadlocal import ThreadLocalContext
40+
41+
pytestmark = pytest.mark.eventlet
42+
43+
44+
def test_eventlet_thread_monkeypatched():
45+
eventlet.monkey_patch(thread=True)
46+
assert is_monkey_patched("thread")
47+
48+
execution_context = elasticapm.context.init_execution_context()
49+
50+
# Should always use ThreadLocalContext when eventlet has patched
51+
# threading.local
52+
assert isinstance(execution_context, ThreadLocalContext)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# BSD 3-Clause License
2+
#
3+
# Copyright (c) 2019, Elasticsearch BV
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this
10+
# list of conditions and the following disclaimer.
11+
#
12+
# * Redistributions in binary form must reproduce the above copyright notice,
13+
# this list of conditions and the following disclaimer in the documentation
14+
# and/or other materials provided with the distribution.
15+
#
16+
# * Neither the name of the copyright holder nor the names of its
17+
# contributors may be used to endorse or promote products derived from
18+
# this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
32+
import pytest
33+
34+
import elasticapm.context
35+
from elasticapm.context.threadlocal import ThreadLocalContext
36+
37+
gevent = pytest.importorskip("gevent")
38+
pytestmark = pytest.mark.gevent
39+
40+
41+
def test_gevent_thread_monkeypatched():
42+
gevent.monkey.patch_thread()
43+
assert gevent.monkey.is_object_patched("threading", "local")
44+
45+
execution_context = elasticapm.context.init_execution_context()
46+
47+
# Should always use ThreadLocalContext when gevent has patched
48+
# threading.local
49+
assert isinstance(execution_context, ThreadLocalContext)

tests/requirements/requirements-base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ statistics==1.0.3.5
1919
urllib3
2020
certifi
2121
Jinja2
22-
Logbook==1.3.0
22+
Logbook
2323
MarkupSafe
2424
WebOb
2525
Werkzeug
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gevent
2+
-r requirements-base.txt

tests/scripts/envs/gevent.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export PYTEST_MARKER="-m gevent"

0 commit comments

Comments
 (0)