Skip to content

Commit 4b72ad0

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #445 from palazzem/environment-getter
[core] provide an environment getter utility to ensure consistency between env vars
2 parents 353cae2 + d064b91 commit 4b72ad0

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

ddtrace/util.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
"""
2-
Generic utilities for tracers
3-
"""
4-
5-
from functools import wraps
1+
import os
62
import inspect
73
import logging
84
import wrapt
95

6+
from functools import wraps
7+
108

119
def deprecated(message='', version=None):
1210
"""Function decorator to report a deprecated function"""
@@ -22,6 +20,7 @@ def wrapper(*args, **kwargs):
2220
return wrapper
2321
return decorator
2422

23+
2524
def deep_getattr(obj, attr_string, default=None):
2625
"""
2726
Returns the attribute of `obj` at the dotted path given by `attr_string`
@@ -66,7 +65,6 @@ def safe_patch(patchable, key, patch_func, service, meta, tracer):
6665
the original unpatched method we wish to trace.
6766
6867
"""
69-
7068
def _get_original_method(thing, key):
7169
orig = None
7270
if hasattr(thing, '_dogtraced'):
@@ -112,6 +110,27 @@ def asbool(value):
112110
return value.lower() in ("true", "1")
113111

114112

113+
def get_env(integration, variable, default=None):
114+
"""Retrieves environment variables value for the given integration. It must be used
115+
for consistency between integrations. The implementation is backward compatible
116+
with legacy nomenclature:
117+
* `DATADOG_` is a legacy prefix with lower priority
118+
* `DD_` environment variables have the highest priority
119+
* the environment variable is built concatenating `integration` and `variable`
120+
arguments
121+
* return `default` otherwise
122+
"""
123+
key = '{}_{}'.format(integration, variable).upper()
124+
legacy_env = 'DATADOG_{}'.format(key)
125+
env = 'DD_{}'.format(key)
126+
127+
# [Backward compatibility]: `DATADOG_` variables should be supported;
128+
# add a deprecation warning later if it's used, so that we can drop the key
129+
# in newer releases.
130+
value = os.getenv(env) or os.getenv(legacy_env)
131+
return value if value else default
132+
133+
115134
def unwrap(obj, attr):
116135
f = getattr(obj, attr, None)
117136
if f and isinstance(f, wrapt.ObjectProxy) and hasattr(f, '__wrapped__'):

tests/test_utils.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import os
12
import unittest
23

3-
from nose.tools import eq_
4+
from nose.tools import eq_, ok_
45

5-
from ddtrace.util import asbool
6+
from ddtrace.util import asbool, get_env
67

78

89
class TestUtilities(unittest.TestCase):
@@ -17,3 +18,31 @@ def test_asbool(self):
1718
eq_(asbool(""), False)
1819
eq_(asbool(True), True)
1920
eq_(asbool(False), False)
21+
22+
def test_get_env(self):
23+
# ensure `get_env` returns a default value if environment variables
24+
# are not set
25+
value = get_env('django', 'distributed_tracing')
26+
ok_(value is None)
27+
value = get_env('django', 'distributed_tracing', False)
28+
ok_(value is False)
29+
30+
def test_get_env_found(self):
31+
# ensure `get_env` returns a value if the environment variable is set
32+
os.environ['DD_REQUESTS_DISTRIBUTED_TRACING'] = '1'
33+
value = get_env('requests', 'distributed_tracing')
34+
eq_(value, '1')
35+
36+
def test_get_env_found_legacy(self):
37+
# ensure `get_env` returns a value if legacy environment variables
38+
# are used
39+
os.environ['DATADOG_REQUESTS_DISTRIBUTED_TRACING'] = '1'
40+
value = get_env('requests', 'distributed_tracing')
41+
eq_(value, '1')
42+
43+
def test_get_env_key_priority(self):
44+
# ensure `get_env` use `DD_` with highest priority
45+
os.environ['DD_REQUESTS_DISTRIBUTED_TRACING'] = 'highest'
46+
os.environ['DATADOG_REQUESTS_DISTRIBUTED_TRACING'] = 'lowest'
47+
value = get_env('requests', 'distributed_tracing')
48+
eq_(value, 'highest')

0 commit comments

Comments
 (0)