Skip to content

Commit 4040b13

Browse files
committed
test: Cover gevent autotracing
Signed-off-by: Ferenc Géczi <[email protected]>
1 parent a59fbc9 commit 4040b13

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

src/instana/__init__.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,25 @@ def load(_):
5555
sys.argv = ['']
5656
return None
5757

58-
5958
def apply_gevent_monkey_patch():
60-
from gevent import monkey
61-
62-
if os.environ.get("INSTANA_GEVENT_MONKEY_OPTIONS"):
63-
all_accepted_patch_all_args = getter(monkey.patch_all)[0]
64-
provided_options = os.environ.get("INSTANA_GEVENT_MONKEY_OPTIONS").replace(" ","").replace("--","")
65-
args = {((k[3:] if k.startswith('no-') else k), k.startswith('no-')) for k in provided_options if (k in all_accepted_patch_all_args)}
66-
monkey.patch_all(**args)
67-
else:
68-
monkey.patch_all()
59+
from gevent import monkey
60+
61+
if os.environ.get("INSTANA_GEVENT_MONKEY_OPTIONS"):
62+
def short_key(k):
63+
return k[3:] if k.startswith('no-') else k
64+
65+
def key_to_bool(k):
66+
return not k.startswith('no-')
67+
68+
import inspect
69+
all_accepted_patch_all_args = inspect.getfullargspec(monkey.patch_all)[0]
70+
provided_options = os.environ.get("INSTANA_GEVENT_MONKEY_OPTIONS").replace(" ","").replace("--","").split(',')
71+
provided_options = [k for k in provided_options if short_key(k) in all_accepted_patch_all_args]
72+
73+
fargs = {short_key(k): key_to_bool(k) for (k,v) in zip(provided_options, [True]*len(provided_options))}
74+
monkey.patch_all(**fargs)
75+
else:
76+
monkey.patch_all()
6977

7078

7179
def get_lambda_handler_or_default():
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# (c) Copyright IBM Corp. 2021
2+
# (c) Copyright Instana Inc. 2020
3+
4+
import importlib
5+
import os
6+
import unittest
7+
import socket
8+
9+
import gevent
10+
from gevent import monkey
11+
from instana import apply_gevent_monkey_patch
12+
13+
14+
class TestGEventAutoTrace(unittest.TestCase):
15+
def setUp(self):
16+
# Ensure that the test suite is operational even when Django is installed
17+
# but not running or configured
18+
os.environ['DJANGO_SETTINGS_MODULE'] = ''
19+
20+
self.default_patched_modules = ('socket', 'time', 'select', 'os',
21+
'threading', 'ssl', 'subprocess', 'signal', 'queue',)
22+
23+
def tearDown(self):
24+
if os.environ.get('INSTANA_GEVENT_MONKEY_OPTIONS'):
25+
os.environ.pop('INSTANA_GEVENT_MONKEY_OPTIONS')
26+
27+
# Clean up after gevent monkey patches, by restore from the saved dict
28+
for modname in monkey.saved.keys():
29+
try:
30+
mod = __import__(modname)
31+
importlib.reload(mod)
32+
for key in monkey.saved[modname].keys():
33+
setattr(mod, key, monkey.saved[modname][key])
34+
except ImportError:
35+
pass
36+
monkey.saved = {}
37+
38+
39+
def test_default_patch_all(self):
40+
apply_gevent_monkey_patch()
41+
for module_name in self.default_patched_modules:
42+
self.assertTrue(monkey.is_module_patched(module_name),
43+
f"{module_name} is not patched")
44+
45+
def test_instana_monkey_options_only_time(self):
46+
os.environ['INSTANA_GEVENT_MONKEY_OPTIONS'] = (
47+
'time,no-socket,no-select,no-os,no-select,no-threading,no-os,'
48+
'no-ssl,no-subprocess,''no-signal,no-queue')
49+
apply_gevent_monkey_patch()
50+
51+
self.assertTrue(monkey.is_module_patched('time'), "time module is not patched")
52+
not_patched_modules = (m for m in self.default_patched_modules if m not in ('time', 'threading'))
53+
54+
for module_name in not_patched_modules:
55+
self.assertFalse(monkey.is_module_patched(module_name),
56+
f"{module_name} is patched, when it shouldn't be")
57+
58+
59+
def test_instana_monkey_options_only_socket(self):
60+
os.environ['INSTANA_GEVENT_MONKEY_OPTIONS'] = (
61+
'--socket, --no-time, --no-select, --no-os, --no-queue, --no-threading,'
62+
'--no-os, --no-ssl, no-subprocess, --no-signal, --no-select,')
63+
apply_gevent_monkey_patch()
64+
65+
self.assertTrue(monkey.is_module_patched('socket'), "socket module is not patched")
66+
not_patched_modules = (m for m in self.default_patched_modules if m not in ('socket', 'threading'))
67+
68+
for module_name in not_patched_modules:
69+
self.assertFalse(monkey.is_module_patched(module_name),
70+
f"{module_name} is patched, when it shouldn't be")
71+

0 commit comments

Comments
 (0)