Skip to content

Commit 99bdbe5

Browse files
committed
feat: initialize SCA reachability in the Lambda wrapper
1 parent ae646b6 commit 99bdbe5

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎datadog_lambda/wrapper.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@
6767

6868
start()
6969

70+
if config.sca_enabled:
71+
try:
72+
from ddtrace.internal.sca.product import start as start_sca
73+
from ddtrace.internal.sca.product import post_preload as instrument_sca
74+
75+
# The layer patches integrations before loading the wrapper. It does not
76+
# use ddtrace.auto, so run both SCA startup phases before importing user code.
77+
start_sca()
78+
instrument_sca()
79+
except Exception:
80+
logger.debug("Failed to initialize SCA reachability", exc_info=True)
81+
7082
profiler = None
7183
if config.profiling_enabled:
7284
try:

‎tests/test_wrapper.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,38 @@
2323
MAX_UINT_64BITS = (1 << 64) - 1
2424

2525

26+
@pytest.mark.parametrize("sca_enabled", [False, True])
27+
def test_sca_startup(sca_enabled, monkeypatch):
28+
product = MagicMock()
29+
monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", sca_enabled)
30+
monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product)
31+
32+
importlib.reload(wrapper)
33+
34+
assert product.mock_calls == (
35+
[call.start(), call.post_preload()] if sca_enabled else []
36+
)
37+
38+
39+
def test_sca_startup_with_older_tracer(monkeypatch):
40+
monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", True)
41+
monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", None)
42+
43+
importlib.reload(wrapper)
44+
45+
46+
def test_sca_startup_failure_preserves_handler(monkeypatch):
47+
product = MagicMock()
48+
product.start.side_effect = RuntimeError("SCA startup failed")
49+
monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", True)
50+
monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product)
51+
52+
importlib.reload(wrapper)
53+
54+
product.post_preload.assert_not_called()
55+
assert callable(wrapper.datadog_lambda_wrapper)
56+
57+
2658
class TestDatadogLambdaWrapper(unittest.TestCase):
2759
def setUp(self):
2860
# Force @datadog_lambda_wrapper to always create a real

0 commit comments

Comments
 (0)