Skip to content

Commit fbb17e6

Browse files
committed
Add capture_payload_max_depth.
1 parent d836b23 commit fbb17e6

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

datadog_lambda/config.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,26 @@
66
import logging
77
import os
88

9+
logger = logging.getLogger(__name__)
10+
911

1012
def _get_env(key, default=None, cast=None):
1113
@property
1214
def _getter(self):
1315
if not hasattr(self, prop_key):
1416
val = os.environ.get(key, default)
1517
if cast is not None:
16-
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
1729
setattr(self, prop_key, val)
1830
return getattr(self, prop_key)
1931

@@ -45,6 +57,9 @@ class Config:
4557
trace_enabled = _get_env("DD_TRACE_ENABLED", "true", as_bool)
4658
merge_xray_traces = _get_env("DD_MERGE_XRAY_TRACES", "false", as_bool)
4759
trace_extractor = _get_env("DD_TRACE_EXTRACTOR")
60+
capture_payload_max_depth = _get_env(
61+
"DD_CAPTURE_PAYLOAD_MAX_DEPTH", 10, int
62+
)
4863

4964
@property
5065
def fips_mode_enabled(self):
@@ -67,7 +82,6 @@ def _reset(self):
6782
config = Config()
6883

6984
if config.is_gov_region or config.fips_mode_enabled:
70-
logger = logging.getLogger(__name__)
7185
logger.debug(
7286
"Python Lambda Layer FIPS mode is %s.",
7387
"enabled" if config.fips_mode_enabled else "not enabled",

datadog_lambda/tag_object.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
# Copyright 2021 Datadog, Inc.
55

66
from decimal import Decimal
7-
import logging
87
import ujson as json
98

9+
from datadog_lambda.config import config
10+
1011
redactable_keys = ["authorization", "x-authorization", "password", "token"]
11-
max_depth = 10
12-
logger = logging.getLogger(__name__)
1312

1413

1514
def tag_object(span, key, obj, depth=0):
1615
if obj is None:
1716
return span.set_tag(key, obj)
18-
if depth >= max_depth:
17+
if depth >= config.capture_payload_max_depth:
1918
return span.set_tag(key, _redact_val(key, str(obj)[0:5000]))
2019
depth += 1
2120
if _should_try_string(obj):

datadog_lambda/wrapper.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
DD_MIN_COLD_START_DURATION = "DD_MIN_COLD_START_DURATION"
7070
DD_COLD_START_TRACE_SKIP_LIB = "DD_COLD_START_TRACE_SKIP_LIB"
7171
DD_CAPTURE_LAMBDA_PAYLOAD = "DD_CAPTURE_LAMBDA_PAYLOAD"
72-
DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH = "DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH"
7372
DD_REQUESTS_SERVICE_NAME = "DD_REQUESTS_SERVICE_NAME"
7473
DD_SERVICE = "DD_SERVICE"
7574
DD_ENV = "DD_ENV"
@@ -89,13 +88,6 @@ def get_env_as_int(env_key, default_value: int) -> int:
8988
os.environ.get(DD_CAPTURE_LAMBDA_PAYLOAD, "false").lower() == "true"
9089
)
9190

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-
9991
env_env_var = os.environ.get(DD_ENV, None)
10092

10193
init_timestamp_ns = time_ns()

tests/test_config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ def set_env(key, value):
130130
("DD_TRACE_EXTRACTOR", "trace_extractor", None, None),
131131
("DD_TRACE_EXTRACTOR", "trace_extractor", "", ""),
132132
("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),
133140
)
134141

135142

tests/test_tag_object.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_tag_object(self):
2929
True,
3030
)
3131

32+
@patch("datadog_lambda.config.Config.capture_payload_max_depth", 2)
3233
def test_tag_object_max_depth(self):
3334
payload = {
3435
"hello": "world",
@@ -41,11 +42,8 @@ def test_tag_object_max_depth(self):
4142
"vals": [{"thingOne": 1}, {"thingTwo": 2}],
4243
}
4344
spanMock = MagicMock()
44-
import datadog_lambda.tag_object as lib_ref
4545

46-
lib_ref.max_depth = 2 # setting up the test
4746
tag_object(spanMock, "function.request", payload)
48-
lib_ref.max_depth = 10 # revert the setup
4947
spanMock.set_tag.assert_has_calls(
5048
[
5149
call("function.request.vals.0", "{'thingOne': 1}"),
@@ -62,6 +60,7 @@ def test_tag_object_max_depth(self):
6260
True,
6361
)
6462

63+
@patch("datadog_lambda.config.Config.capture_payload_max_depth", 0)
6564
def test_tag_object_max_depth_0(self):
6665
payload = {
6766
"hello": "world",
@@ -74,11 +73,8 @@ def test_tag_object_max_depth_0(self):
7473
"vals": [{"thingOne": 1}, {"thingTwo": 2}],
7574
}
7675
spanMock = MagicMock()
77-
import datadog_lambda.tag_object as lib_ref
7876

79-
lib_ref.max_depth = 0 # setting up the test
8077
tag_object(spanMock, "function.request", payload)
81-
lib_ref.max_depth = 10 # revert the setup
8278
spanMock.set_tag.assert_has_calls(
8379
[
8480
call(

0 commit comments

Comments
 (0)