Skip to content

Commit 8bf9772

Browse files
committed
feat(config): enable telemetry when SCA is on independently from appsec
1 parent 1fac67c commit 8bf9772

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

datadog_lambda/__init__.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1+
import datadog_lambda.config # noqa: F401 needs to be imported before `ddtrace`
12
from datadog_lambda.cold_start import initialize_cold_start_tracing
2-
import os
3-
4-
5-
if os.environ.get("DD_INSTRUMENTATION_TELEMETRY_ENABLED") is None:
6-
# Telemetry is required for Appsec Software Composition Analysis
7-
os.environ["DD_INSTRUMENTATION_TELEMETRY_ENABLED"] = os.environ.get(
8-
"DD_APPSEC_ENABLED", "false"
9-
)
103

114

125
initialize_cold_start_tracing()

datadog_lambda/config.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ def _resolve_env(self, key, default=None, cast=None, depends_on_tracing=False):
8282
logs_injection = _get_env("DD_LOGS_INJECTION", "true", as_bool)
8383
merge_xray_traces = _get_env("DD_MERGE_XRAY_TRACES", "false", as_bool)
8484

85-
telemetry_enabled = _get_env(
86-
"DD_INSTRUMENTATION_TELEMETRY_ENABLED",
87-
"false",
88-
as_bool,
89-
depends_on_tracing=True,
90-
)
9185
otel_enabled = _get_env("DD_TRACE_OTEL_ENABLED", "false", as_bool)
9286
profiling_enabled = _get_env("DD_PROFILING_ENABLED", "false", as_bool)
9387
llmobs_enabled = _get_env("DD_LLMOBS_ENABLED", "false", as_bool)
@@ -96,6 +90,7 @@ def _resolve_env(self, key, default=None, cast=None, depends_on_tracing=False):
9690
"DD_DATA_STREAMS_ENABLED", "false", as_bool, depends_on_tracing=True
9791
)
9892
appsec_enabled = _get_env("DD_APPSEC_ENABLED", "false", as_bool)
93+
sca_enabled = _get_env("DD_APPSEC_SCA_ENABLED", "false", as_bool)
9994

10095
is_gov_region = _get_env("AWS_REGION", "", lambda x: x.startswith("us-gov-"))
10196

@@ -144,3 +139,11 @@ def _reset(self):
144139
"Python Lambda Layer FIPS mode is %s.",
145140
"enabled" if config.fips_mode_enabled else "not enabled",
146141
)
142+
143+
144+
if (
145+
"DD_INSTRUMENTATION_TELEMETRY_ENABLED" not in os.environ
146+
and not config.sca_enabled
147+
and not config.appsec_enabled
148+
):
149+
os.environ["DD_INSTRUMENTATION_TELEMETRY_ENABLED"] = "false"

datadog_lambda/tracing.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555
logger = logging.getLogger(__name__)
5656

5757
dd_trace_context = None
58-
if config.telemetry_enabled:
59-
# Enable the telemetry client if the user has opted in
60-
from ddtrace.internal.telemetry import telemetry_writer
61-
62-
telemetry_writer.enable()
6358

6459
propagator = HTTPPropagator()
6560

scripts/check_format.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ set -e
44
PYTHON_VERSION=$(python -c 'import sys; print(sys.version_info.major)')
55
pip install -Iv black==22.3.0
66

7-
python -m black --check datadog_lambda/ --diff
8-
python -m black --check tests --diff
9-
10-
11-
7+
python -m black datadog_lambda/
8+
python -m black tests

tests/test_config.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import importlib
2+
import sys
3+
14
import pytest
25

36
from datadog_lambda.config import config, _get_env, Config
@@ -14,6 +17,33 @@ def set_env(key, value):
1417
return set_env
1518

1619

20+
def test_config_import_does_not_import_ddtrace(monkeypatch):
21+
import datadog_lambda
22+
23+
with monkeypatch.context() as mp:
24+
for name in list(sys.modules):
25+
if name == "ddtrace" or name.startswith("ddtrace."):
26+
mp.delitem(sys.modules, name, raising=False)
27+
28+
class _BlockDdtrace(importlib.abc.MetaPathFinder):
29+
def find_spec(self, fullname, path=None, target=None):
30+
if fullname == "ddtrace" or fullname.startswith("ddtrace."):
31+
raise ImportError("ddtrace must not be imported during this test")
32+
return None
33+
34+
blocker = _BlockDdtrace()
35+
mp.setattr(sys, "meta_path", [blocker] + sys.meta_path, raising=False)
36+
37+
mp.delattr(datadog_lambda, "config", raising=False)
38+
mp.delitem(sys.modules, "datadog_lambda.config", raising=False)
39+
importlib.invalidate_caches()
40+
importlib.import_module("datadog_lambda.config")
41+
42+
assert all(
43+
not (n == "ddtrace" or n.startswith("ddtrace.")) for n in sys.modules
44+
), "The config module should not import ddtrace."
45+
46+
1747
def _test_as_bool(env_key, conf_key, default):
1848
return (
1949
(env_key, conf_key, None, default),
@@ -72,9 +102,6 @@ def _test_as_list(env_key, conf_key, default):
72102
*_test_as_bool("DD_INTEGRATION_TEST", "integration_test", default=False),
73103
*_test_as_bool("DD_BOTOCORE_ADD_SPAN_POINTERS", "add_span_pointers", default=True),
74104
*_test_as_bool("DD_TRACE_OTEL_ENABLED", "otel_enabled", default=False),
75-
*_test_as_bool(
76-
"DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", default=False
77-
),
78105
*_test_as_bool("DD_MERGE_XRAY_TRACES", "merge_xray_traces", default=False),
79106
*_test_as_bool("DD_PROFILING_ENABLED", "profiling_enabled", default=False),
80107
*_test_as_bool("DD_LLMOBS_ENABLED", "llmobs_enabled", default=False),
@@ -86,6 +113,8 @@ def _test_as_list(env_key, conf_key, default):
86113
),
87114
*_test_as_bool("DD_LOCAL_TEST", "local_test", default=False),
88115
*_test_as_bool("DD_DATA_STREAMS_ENABLED", "data_streams_enabled", default=False),
116+
*_test_as_bool("DD_APPSEC_ENABLED", "appsec_enabled", default=False),
117+
*_test_as_bool("DD_APPSEC_SCA_ENABLED", "sca_enabled", default=False),
89118
*_test_int(
90119
"DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", default=10
91120
),
@@ -143,9 +172,6 @@ def test_config_from_environ(env_key, conf_key, env_val, conf_val, setenv):
143172
"DD_DECODE_AUTHORIZER_CONTEXT", "decode_authorizer_context", default=True
144173
),
145174
*_test_as_bool("DD_DATA_STREAMS_ENABLED", "data_streams_enabled", default=False),
146-
*_test_as_bool(
147-
"DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", default=False
148-
),
149175
)
150176

151177

0 commit comments

Comments
 (0)