Skip to content

Commit 97ca621

Browse files
authored
uWSGI postfork hooks (#207)
* Add uWSGI postfork hook support * Immediate reset on fork and hook cleanup * More refinements: add thread disabled warning
1 parent 010cc18 commit 97ca621

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

instana/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def boot_agent():
5353

5454
import instana.singletons
5555

56+
# Instrumentation
5657
if "INSTANA_DISABLE_AUTO_INSTR" not in os.environ:
5758
# Import & initialize instrumentation
5859
if sys.version_info >= (3, 5, 3):
@@ -81,6 +82,8 @@ def boot_agent():
8182
from .instrumentation import urllib3
8283
from .instrumentation.django import middleware
8384

85+
# Hooks
86+
from .hooks import hook_uwsgi
8487

8588
if "INSTANA_MAGIC" in os.environ:
8689
pkg_resources.working_set.add_entry("/tmp/instana/python")

instana/agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def announce(self, discovery):
154154
"""
155155
try:
156156
url = self.__discovery_url()
157-
logger.debug("making announce request to %s", url)
157+
# logger.debug("making announce request to %s", url)
158158
response = None
159159
response = self.client.put(url,
160160
data=to_json(discovery),

instana/fsm.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,8 @@ def reset(self):
9090
9191
:return: void
9292
"""
93-
logger.debug("State machine being reset. Will schedule new announce cycle 6 seconds from now.")
94-
95-
self.timer = t.Timer(6, self.fsm.lookup)
96-
self.timer.daemon = True
97-
self.timer.name = self.THREAD_NAME
98-
self.timer.start()
93+
logger.debug("State machine being reset. Will start a new announce cycle.")
94+
self.fsm.lookup()
9995

10096
def lookup_agent_host(self, e):
10197
self.agent.should_threads_shutdown.clear()

instana/hooks/__init__.py

Whitespace-only changes.

instana/hooks/hook_uwsgi.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
The uwsgi and uwsgidecorators packages are added automatically to the Python environment
3+
when running under uWSGI. Here we attempt to detect the presence of these packages and
4+
then use the appropriate hooks.
5+
"""
6+
from __future__ import absolute_import
7+
8+
from ..log import logger
9+
from ..singletons import agent
10+
11+
try:
12+
import uwsgi
13+
logger.debug("uWSGI options: %s", uwsgi.opt)
14+
15+
opt_master = uwsgi.opt.get('master', False)
16+
opt_lazy_apps = uwsgi.opt.get('lazy-apps', False)
17+
18+
if uwsgi.opt.get('enable-threads', False) is False:
19+
logger.warn("Required: uWSGI threads are not enabled. " +
20+
"Please enable by using the uWSGI --enable-threads option.")
21+
22+
if opt_master and opt_lazy_apps is False:
23+
# --master is supplied in uWSGI options (otherwise uwsgidecorators package won't be available)
24+
# When --lazy-apps is True, this postfork hook isn't needed
25+
import uwsgidecorators
26+
27+
@uwsgidecorators.postfork
28+
def uwsgi_handle_fork():
29+
""" This is our uWSGI hook to detect and act when worker processes are forked off. """
30+
logger.debug("Handling uWSGI fork...")
31+
agent.handle_fork()
32+
33+
logger.debug("Applied uWSGI hooks")
34+
else:
35+
logger.debug("uWSGI --master=%s --lazy-apps=%s: postfork hooks not applied", opt_master, opt_lazy_apps)
36+
except ImportError as e:
37+
logger.debug('uwsgi hooks: decorators not available: %s', e)
38+
pass

0 commit comments

Comments
 (0)