1- """
2- Generic utilities for tracers
3- """
4-
5- from functools import wraps
1+ import os
62import inspect
73import logging
84import wrapt
95
6+ from functools import wraps
7+
108
119def 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+
2524def 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+
115134def unwrap (obj , attr ):
116135 f = getattr (obj , attr , None )
117136 if f and isinstance (f , wrapt .ObjectProxy ) and hasattr (f , '__wrapped__' ):
0 commit comments