We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d836b23 commit fbb17e6Copy full SHA for fbb17e6
5 files changed
datadog_lambda/config.py
@@ -6,14 +6,26 @@
6
import logging
7
import os
8
9
+logger = logging.getLogger(__name__)
10
+
11
12
def _get_env(key, default=None, cast=None):
13
@property
14
def _getter(self):
15
if not hasattr(self, prop_key):
16
val = os.environ.get(key, default)
17
if cast is not None:
- val = cast(val)
18
+ try:
19
+ val = cast(val)
20
+ except (ValueError, TypeError):
21
+ logger.warning(
22
+ "Failed to cast environment variable '%s' with value '%s' to type %s. Using default value '%s'.",
23
+ key,
24
+ val,
25
+ cast.__name__,
26
+ default,
27
+ )
28
+ val = default
29
setattr(self, prop_key, val)
30
return getattr(self, prop_key)
31
@@ -45,6 +57,9 @@ class Config:
45
57
trace_enabled = _get_env("DD_TRACE_ENABLED", "true", as_bool)
46
58
merge_xray_traces = _get_env("DD_MERGE_XRAY_TRACES", "false", as_bool)
47
59
trace_extractor = _get_env("DD_TRACE_EXTRACTOR")
60
+ capture_payload_max_depth = _get_env(
61
+ "DD_CAPTURE_PAYLOAD_MAX_DEPTH", 10, int
62
48
63
49
64
50
65
def fips_mode_enabled(self):
@@ -67,7 +82,6 @@ def _reset(self):
67
82
config = Config()
68
83
69
84
if config.is_gov_region or config.fips_mode_enabled:
70
- logger = logging.getLogger(__name__)
71
85
logger.debug(
72
86
"Python Lambda Layer FIPS mode is %s.",
73
87
"enabled" if config.fips_mode_enabled else "not enabled",
datadog_lambda/tag_object.py
@@ -4,18 +4,17 @@
4
# Copyright 2021 Datadog, Inc.
5
from decimal import Decimal
-import logging
import ujson as json
+from datadog_lambda.config import config
redactable_keys = ["authorization", "x-authorization", "password", "token"]
-max_depth = 10
-logger = logging.getLogger(__name__)
def tag_object(span, key, obj, depth=0):
if obj is None:
return span.set_tag(key, obj)
- if depth >= max_depth:
+ if depth >= config.capture_payload_max_depth:
return span.set_tag(key, _redact_val(key, str(obj)[0:5000]))
depth += 1
if _should_try_string(obj):
datadog_lambda/wrapper.py
@@ -69,7 +69,6 @@
DD_MIN_COLD_START_DURATION = "DD_MIN_COLD_START_DURATION"
DD_COLD_START_TRACE_SKIP_LIB = "DD_COLD_START_TRACE_SKIP_LIB"
DD_CAPTURE_LAMBDA_PAYLOAD = "DD_CAPTURE_LAMBDA_PAYLOAD"
-DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH = "DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH"
DD_REQUESTS_SERVICE_NAME = "DD_REQUESTS_SERVICE_NAME"
74
DD_SERVICE = "DD_SERVICE"
75
DD_ENV = "DD_ENV"
@@ -89,13 +88,6 @@ def get_env_as_int(env_key, default_value: int) -> int:
89
88
os.environ.get(DD_CAPTURE_LAMBDA_PAYLOAD, "false").lower() == "true"
90
)
91
92
-if dd_capture_lambda_payload_enabled:
93
- import datadog_lambda.tag_object as tag_object
94
-
95
- tag_object.max_depth = get_env_as_int(
96
- DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH, tag_object.max_depth
97
- )
98
99
env_env_var = os.environ.get(DD_ENV, None)
100
101
init_timestamp_ns = time_ns()
tests/test_config.py
@@ -130,6 +130,13 @@ def set_env(key, value):
130
("DD_TRACE_EXTRACTOR", "trace_extractor", None, None),
131
("DD_TRACE_EXTRACTOR", "trace_extractor", "", ""),
132
("DD_TRACE_EXTRACTOR", "trace_extractor", "my_extractor", "my_extractor"),
133
+ ("DD_CAPTURE_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", None, 10),
134
+ ("DD_CAPTURE_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", "", 10),
135
+ ("DD_CAPTURE_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", "5", 5),
136
+ ("DD_CAPTURE_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", "0", 0),
137
+ ("DD_CAPTURE_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", "2.5", 10),
138
+ ("DD_CAPTURE_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", "-1", -1),
139
+ ("DD_CAPTURE_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", "purple", 10),
140
141
142
tests/test_tag_object.py
@@ -29,6 +29,7 @@ def test_tag_object(self):
True,
32
+ @patch("datadog_lambda.config.Config.capture_payload_max_depth", 2)
33
def test_tag_object_max_depth(self):
34
payload = {
35
"hello": "world",
@@ -41,11 +42,8 @@ def test_tag_object_max_depth(self):
41
42
"vals": [{"thingOne": 1}, {"thingTwo": 2}],
43
}
44
spanMock = MagicMock()
- import datadog_lambda.tag_object as lib_ref
- lib_ref.max_depth = 2 # setting up the test
tag_object(spanMock, "function.request", payload)
- lib_ref.max_depth = 10 # revert the setup
spanMock.set_tag.assert_has_calls(
[
51
call("function.request.vals.0", "{'thingOne': 1}"),
@@ -62,6 +60,7 @@ def test_tag_object_max_depth(self):
+ @patch("datadog_lambda.config.Config.capture_payload_max_depth", 0)
def test_tag_object_max_depth_0(self):
66
@@ -74,11 +73,8 @@ def test_tag_object_max_depth_0(self):
76
77
78
79
- lib_ref.max_depth = 0 # setting up the test
80
81
call(
0 commit comments