Skip to content

Commit 6e507eb

Browse files
committed
Lazy read env values on config.
1 parent d225798 commit 6e507eb

File tree

2 files changed

+32
-53
lines changed

2 files changed

+32
-53
lines changed

datadog_lambda/config.py

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,46 @@
88

99

1010
def _get_env(key, default=None, cast=None):
11-
"""Get an environment variable with a default value."""
12-
prop_key = f"_{key}"
13-
1411
@property
1512
def _getter(self):
1613
if not hasattr(self, prop_key):
1714
val = os.environ.get(key, default)
1815
if cast is not None:
19-
try:
20-
val = cast(val)
21-
except ValueError:
22-
raise ValueError(f"Invalid value for {key}: {val}")
23-
return cast(default)
16+
val = cast(val)
17+
setattr(self, prop_key, val)
2418
return getattr(self, prop_key)
2519

20+
prop_key = f"_{key}"
2621
return _getter
2722

23+
2824
def as_bool(val):
29-
"""Convert a string to a boolean."""
30-
if isinstance(val, bool):
31-
return val
32-
if isinstance(val, str):
33-
val = val.lower()
34-
if val in ("true", "1", "yes"):
35-
return True
36-
elif val in ("false", "0", "no"):
37-
return False
38-
raise ValueError(f"Invalid boolean value: {val}")
25+
return val.lower() == "true" or val == "1"
3926

4027

4128
class Config:
4229

43-
def __init__(self):
44-
self.function_name = os.environ.get("AWS_LAMBDA_FUNCTION_NAME")
45-
self.flush_to_log = os.environ.get("DD_FLUSH_TO_LOG", "").lower() == "true"
46-
self.trace_enabled = os.environ.get("DD_TRACE_ENABLED", "true").lower() == "true"
47-
self.cold_start_tracing = (
48-
os.environ.get("DD_COLD_START_TRACING", "true").lower() == "true"
49-
)
50-
self.is_gov_region = os.environ.get("AWS_REGION", "").startswith("us-gov-")
51-
self.fips_mode_enabled = (
52-
os.environ.get(
30+
add_span_pointers = _get_env("DD_BOTOCORE_ADD_SPAN_POINTERS", "true", as_bool)
31+
cold_start_tracing = _get_env("DD_COLD_START_TRACING", "true", as_bool)
32+
enhanced_metrics_enabled = _get_env("DD_ENHANCED_METRICS", "true", as_bool)
33+
flush_in_thread = _get_env("DD_FLUSH_IN_THREAD", "false", as_bool)
34+
flush_to_log = _get_env("DD_FLUSH_TO_LOG", "false", as_bool)
35+
function_name = _get_env("AWS_LAMBDA_FUNCTION_NAME")
36+
is_gov_region = _get_env("AWS_REGION", "", lambda x: x.startswith("us-gov-"))
37+
is_in_tests = _get_env("DD_INTEGRATION_TEST", "false", as_bool)
38+
is_lambda_context = _get_env("AWS_LAMBDA_FUNCTION_NAME", None, bool)
39+
otel_enabled = _get_env("DD_TRACE_OTEL_ENABLED", "false", as_bool)
40+
telemetry_enabled = _get_env("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "false", as_bool)
41+
trace_enabled = _get_env("DD_TRACE_ENABLED", "true", as_bool)
42+
43+
@property
44+
def fips_mode_enabled(self):
45+
if not hasattr(self, "_fips_mode_enabled"):
46+
self._fips_mode_enabled = os.environ.get(
5347
"DD_LAMBDA_FIPS_MODE",
5448
"true" if self.is_gov_region else "false",
55-
).lower()
56-
== "true"
57-
)
58-
self.flush_in_thread = os.environ.get("DD_FLUSH_IN_THREAD", "").lower() == "true"
59-
self.enhanced_metrics_enabled = (
60-
os.environ.get("DD_ENHANCED_METRICS", "true").lower() == "true"
61-
)
62-
self.is_in_tests = os.environ.get("DD_INTEGRATION_TEST", "false").lower() == "true"
63-
self.add_span_pointers = os.environ.get(
64-
"DD_BOTOCORE_ADD_SPAN_POINTERS", "true"
65-
).lower() in ("true", "1")
66-
self.otel_enabled = os.environ.get("DD_TRACE_OTEL_ENABLED", "false").lower() == "true"
67-
self.is_lambda_context = bool(self.function_name)
68-
self.telemetry_enabled = (
69-
os.environ.get("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "false").lower()
70-
== "true"
71-
)
49+
).lower() == "true"
50+
return self._fips_mode_enabled
7251

7352

7453
config = Config()

tests/test_config.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
("DD_FLUSH_TO_LOG", "flush_to_log", "TRUE", True),
1515
("DD_FLUSH_TO_LOG", "flush_to_log", "false", False),
1616
("DD_FLUSH_TO_LOG", "flush_to_log", "FALSE", False),
17-
("DD_FLUSH_TO_LOG", "flush_to_log", "1", False),
17+
("DD_FLUSH_TO_LOG", "flush_to_log", "1", True), # CHANGED
1818
("DD_FLUSH_TO_LOG", "flush_to_log", "0", False),
1919
("DD_FLUSH_TO_LOG", "flush_to_log", "purple", False),
2020

@@ -24,7 +24,7 @@
2424
("DD_TRACE_ENABLED", "trace_enabled", "TRUE", True),
2525
("DD_TRACE_ENABLED", "trace_enabled", "false", False),
2626
("DD_TRACE_ENABLED", "trace_enabled", "FALSE", False),
27-
("DD_TRACE_ENABLED", "trace_enabled", "1", False),
27+
("DD_TRACE_ENABLED", "trace_enabled", "1", True), # CHANGED
2828
("DD_TRACE_ENABLED", "trace_enabled", "0", False),
2929
("DD_TRACE_ENABLED", "trace_enabled", "purple", False),
3030

@@ -34,7 +34,7 @@
3434
("DD_COLD_START_TRACING", "cold_start_tracing", "TRUE", True),
3535
("DD_COLD_START_TRACING", "cold_start_tracing", "false", False),
3636
("DD_COLD_START_TRACING", "cold_start_tracing", "FALSE", False),
37-
("DD_COLD_START_TRACING", "cold_start_tracing", "1", False),
37+
("DD_COLD_START_TRACING", "cold_start_tracing", "1", True), # CHANGED
3838
("DD_COLD_START_TRACING", "cold_start_tracing", "0", False),
3939
("DD_COLD_START_TRACING", "cold_start_tracing", "purple", False),
4040

@@ -49,7 +49,7 @@
4949
("DD_FLUSH_IN_THREAD", "flush_in_thread", "TRUE", True),
5050
("DD_FLUSH_IN_THREAD", "flush_in_thread", "false", False),
5151
("DD_FLUSH_IN_THREAD", "flush_in_thread", "FALSE", False),
52-
("DD_FLUSH_IN_THREAD", "flush_in_thread", "1", False),
52+
("DD_FLUSH_IN_THREAD", "flush_in_thread", "1", True), # CHANGED
5353
("DD_FLUSH_IN_THREAD", "flush_in_thread", "0", False),
5454
("DD_FLUSH_IN_THREAD", "flush_in_thread", "purple", False),
5555

@@ -59,7 +59,7 @@
5959
("DD_ENHANCED_METRICS", "enhanced_metrics_enabled", "TRUE", True),
6060
("DD_ENHANCED_METRICS", "enhanced_metrics_enabled", "false", False),
6161
("DD_ENHANCED_METRICS", "enhanced_metrics_enabled", "FALSE", False),
62-
("DD_ENHANCED_METRICS", "enhanced_metrics_enabled", "1", False),
62+
("DD_ENHANCED_METRICS", "enhanced_metrics_enabled", "1", True), # CHANGED
6363
("DD_ENHANCED_METRICS", "enhanced_metrics_enabled", "0", False),
6464
("DD_ENHANCED_METRICS", "enhanced_metrics_enabled", "purple", False),
6565

@@ -69,7 +69,7 @@
6969
("DD_INTEGRATION_TEST", "is_in_tests", "TRUE", True),
7070
("DD_INTEGRATION_TEST", "is_in_tests", "false", False),
7171
("DD_INTEGRATION_TEST", "is_in_tests", "FALSE", False),
72-
("DD_INTEGRATION_TEST", "is_in_tests", "1", False),
72+
("DD_INTEGRATION_TEST", "is_in_tests", "1", True), # CHANGED
7373
("DD_INTEGRATION_TEST", "is_in_tests", "0", False),
7474
("DD_INTEGRATION_TEST", "is_in_tests", "purple", False),
7575

@@ -89,7 +89,7 @@
8989
("DD_TRACE_OTEL_ENABLED", "otel_enabled", "TRUE", True),
9090
("DD_TRACE_OTEL_ENABLED", "otel_enabled", "false", False),
9191
("DD_TRACE_OTEL_ENABLED", "otel_enabled", "FALSE", False),
92-
("DD_TRACE_OTEL_ENABLED", "otel_enabled", "1", False),
92+
("DD_TRACE_OTEL_ENABLED", "otel_enabled", "1", True), # CHANGED
9393
("DD_TRACE_OTEL_ENABLED", "otel_enabled", "0", False),
9494
("DD_TRACE_OTEL_ENABLED", "otel_enabled", "purple", False),
9595

@@ -103,7 +103,7 @@
103103
("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", "TRUE", True),
104104
("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", "false", False),
105105
("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", "FALSE", False),
106-
("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", "1", False),
106+
("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", "1", True), # CHANGED
107107
("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", "0", False),
108108
("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", "purple", False),
109109
)

0 commit comments

Comments
 (0)