Skip to content

Commit 7bc8751

Browse files
committed
Cut memory overhead.
1 parent 43e31fc commit 7bc8751

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

datadog_lambda/config.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,7 @@ def _get_env(key, default=None, cast=None, depends_on_tracing=False):
1313
@property
1414
def _getter(self):
1515
if not hasattr(self, prop_key):
16-
if depends_on_tracing and not config.trace_enabled:
17-
val = False
18-
else:
19-
val = os.environ.get(key, default)
20-
if cast is not None:
21-
try:
22-
val = cast(val)
23-
except (ValueError, TypeError):
24-
msg = (
25-
"Failed to cast environment variable '%s' with "
26-
"value '%s' to type %s. Using default value '%s'."
27-
)
28-
logger.warning(msg, key, val, cast.__name__, default)
29-
val = default
16+
val = self._resolve_env(key, default, cast, depends_on_tracing)
3017
setattr(self, prop_key, val)
3118
return getattr(self, prop_key)
3219

@@ -43,6 +30,21 @@ def as_list(val):
4330

4431

4532
class Config:
33+
def _resolve_env(self, key, default=None, cast=None, depends_on_tracing=False):
34+
if depends_on_tracing and not self.trace_enabled:
35+
return False
36+
val = os.environ.get(key, default)
37+
if cast is not None:
38+
try:
39+
val = cast(val)
40+
except (ValueError, TypeError):
41+
msg = (
42+
"Failed to cast environment variable '%s' with "
43+
"value '%s' to type %s. Using default value '%s'."
44+
)
45+
logger.warning(msg, key, val, cast.__name__, default)
46+
val = default
47+
return val
4648

4749
service = _get_env("DD_SERVICE")
4850
env = _get_env("DD_ENV")

tests/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from datadog_lambda.config import config, _get_env
3+
from datadog_lambda.config import config, _get_env, Config
44

55

66
@pytest.fixture
@@ -218,7 +218,7 @@ def test__get_env_does_not_log_when_env_not_set(setenv, monkeypatch):
218218
setenv("TEST_3", None)
219219
setenv("TEST_4", None)
220220

221-
class Testing:
221+
class Testing(Config):
222222
test_1 = _get_env("TEST_1")
223223
test_2 = _get_env("TEST_2", "purple")
224224
test_3 = _get_env("TEST_3", "true", bool)

0 commit comments

Comments
 (0)