Skip to content

Commit d80dfa9

Browse files
author
Emanuele Palazzetti
committed
[core] ensure users sitecustomize.py is called
1 parent c86b122 commit d80dfa9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

ddtrace/bootstrap/sitecustomize.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"""
55

66
import os
7+
import imp
8+
import sys
79
import logging
810

911
from ddtrace.utils.formats import asbool
@@ -46,6 +48,8 @@ def update_patched_modules():
4648
patch = True
4749

4850
# Respect DATADOG_* environment variables in global tracer configuration
51+
# TODO: these variables are deprecated; use utils method and update our documentation
52+
# correct prefix should be DD_*
4953
enabled = os.environ.get("DATADOG_TRACE_ENABLED")
5054
hostname = os.environ.get("DATADOG_TRACE_AGENT_HOSTNAME")
5155
port = os.environ.get("DATADOG_TRACE_AGENT_PORT")
@@ -76,5 +80,24 @@ def update_patched_modules():
7680

7781
if 'DATADOG_ENV' in os.environ:
7882
tracer.set_tags({"env": os.environ["DATADOG_ENV"]})
83+
84+
# Ensure sitecustomize.py is properly called if available in application directories:
85+
# * exclude `bootstrap_dir` from the search
86+
# * find a user `sitecustomize.py` module
87+
# * import that module via `imp`
88+
bootstrap_dir = os.path.dirname(__file__)
89+
path = list(sys.path)
90+
path.remove(bootstrap_dir)
91+
92+
try:
93+
(f, path, description) = imp.find_module('sitecustomize', path)
94+
except ImportError:
95+
pass
96+
else:
97+
# `sitecustomize.py` found, load it
98+
log.debug('sitecustomize from user found in: %s', path)
99+
imp.load_module('sitecustomize', f, path, description)
100+
101+
79102
except Exception as e:
80103
log.warn("error configuring Datadog tracing", exc_info=True)

0 commit comments

Comments
 (0)