From f451e8adecafd2441b290aa0536e9aa0e6a99079 Mon Sep 17 00:00:00 2001 From: Florentin Labelle Date: Wed, 29 Oct 2025 18:24:15 +0100 Subject: [PATCH] feat(config): enable telemetry when SCA is on independently from appsec --- datadog_lambda/__init__.py | 9 +-------- datadog_lambda/config.py | 15 +++++++++------ datadog_lambda/tracing.py | 5 ----- tests/test_config.py | 34 ++++++++++++++++++++++++++++------ 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/datadog_lambda/__init__.py b/datadog_lambda/__init__.py index 2034d3cbf..059cead9e 100644 --- a/datadog_lambda/__init__.py +++ b/datadog_lambda/__init__.py @@ -1,12 +1,5 @@ +import datadog_lambda.config # noqa: F401 needs to be imported before `ddtrace` from datadog_lambda.cold_start import initialize_cold_start_tracing -import os - - -if os.environ.get("DD_INSTRUMENTATION_TELEMETRY_ENABLED") is None: - # Telemetry is required for Appsec Software Composition Analysis - os.environ["DD_INSTRUMENTATION_TELEMETRY_ENABLED"] = os.environ.get( - "DD_APPSEC_ENABLED", "false" - ) initialize_cold_start_tracing() diff --git a/datadog_lambda/config.py b/datadog_lambda/config.py index aaa1af5e8..eda6b5828 100644 --- a/datadog_lambda/config.py +++ b/datadog_lambda/config.py @@ -82,12 +82,6 @@ def _resolve_env(self, key, default=None, cast=None, depends_on_tracing=False): logs_injection = _get_env("DD_LOGS_INJECTION", "true", as_bool) merge_xray_traces = _get_env("DD_MERGE_XRAY_TRACES", "false", as_bool) - telemetry_enabled = _get_env( - "DD_INSTRUMENTATION_TELEMETRY_ENABLED", - "false", - as_bool, - depends_on_tracing=True, - ) otel_enabled = _get_env("DD_TRACE_OTEL_ENABLED", "false", as_bool) profiling_enabled = _get_env("DD_PROFILING_ENABLED", "false", as_bool) 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): "DD_DATA_STREAMS_ENABLED", "false", as_bool, depends_on_tracing=True ) appsec_enabled = _get_env("DD_APPSEC_ENABLED", "false", as_bool) + sca_enabled = _get_env("DD_APPSEC_SCA_ENABLED", "false", as_bool) is_gov_region = _get_env("AWS_REGION", "", lambda x: x.startswith("us-gov-")) @@ -144,3 +139,11 @@ def _reset(self): "Python Lambda Layer FIPS mode is %s.", "enabled" if config.fips_mode_enabled else "not enabled", ) + + +if ( + "DD_INSTRUMENTATION_TELEMETRY_ENABLED" not in os.environ + and not config.sca_enabled + and not config.appsec_enabled +): + os.environ["DD_INSTRUMENTATION_TELEMETRY_ENABLED"] = "false" diff --git a/datadog_lambda/tracing.py b/datadog_lambda/tracing.py index 4faaed2df..225f5dd82 100644 --- a/datadog_lambda/tracing.py +++ b/datadog_lambda/tracing.py @@ -55,11 +55,6 @@ logger = logging.getLogger(__name__) dd_trace_context = None -if config.telemetry_enabled: - # Enable the telemetry client if the user has opted in - from ddtrace.internal.telemetry import telemetry_writer - - telemetry_writer.enable() propagator = HTTPPropagator() diff --git a/tests/test_config.py b/tests/test_config.py index 92002439d..9c5da63a2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,3 +1,6 @@ +import importlib +import sys + import pytest from datadog_lambda.config import config, _get_env, Config @@ -14,6 +17,29 @@ def set_env(key, value): return set_env +def test_config_import_does_not_import_ddtrace(monkeypatch): + import datadog_lambda + + with monkeypatch.context() as mp: + for name in list(sys.modules): + if name == "ddtrace" or name.startswith("ddtrace."): + mp.delitem(sys.modules, name, raising=False) + + class _BlockDdtrace(importlib.abc.MetaPathFinder): + def find_spec(self, fullname, path=None, target=None): + if fullname == "ddtrace" or fullname.startswith("ddtrace."): + raise ImportError("ddtrace must not be imported during this test") + return None + + blocker = _BlockDdtrace() + mp.setattr(sys, "meta_path", [blocker] + sys.meta_path, raising=False) + + mp.delattr(datadog_lambda, "config", raising=False) + mp.delitem(sys.modules, "datadog_lambda.config", raising=False) + importlib.invalidate_caches() + importlib.import_module("datadog_lambda.config") + + def _test_as_bool(env_key, conf_key, default): return ( (env_key, conf_key, None, default), @@ -72,9 +98,6 @@ def _test_as_list(env_key, conf_key, default): *_test_as_bool("DD_INTEGRATION_TEST", "integration_test", default=False), *_test_as_bool("DD_BOTOCORE_ADD_SPAN_POINTERS", "add_span_pointers", default=True), *_test_as_bool("DD_TRACE_OTEL_ENABLED", "otel_enabled", default=False), - *_test_as_bool( - "DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", default=False - ), *_test_as_bool("DD_MERGE_XRAY_TRACES", "merge_xray_traces", default=False), *_test_as_bool("DD_PROFILING_ENABLED", "profiling_enabled", default=False), *_test_as_bool("DD_LLMOBS_ENABLED", "llmobs_enabled", default=False), @@ -86,6 +109,8 @@ def _test_as_list(env_key, conf_key, default): ), *_test_as_bool("DD_LOCAL_TEST", "local_test", default=False), *_test_as_bool("DD_DATA_STREAMS_ENABLED", "data_streams_enabled", default=False), + *_test_as_bool("DD_APPSEC_ENABLED", "appsec_enabled", default=False), + *_test_as_bool("DD_APPSEC_SCA_ENABLED", "sca_enabled", default=False), *_test_int( "DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH", "capture_payload_max_depth", default=10 ), @@ -143,9 +168,6 @@ def test_config_from_environ(env_key, conf_key, env_val, conf_val, setenv): "DD_DECODE_AUTHORIZER_CONTEXT", "decode_authorizer_context", default=True ), *_test_as_bool("DD_DATA_STREAMS_ENABLED", "data_streams_enabled", default=False), - *_test_as_bool( - "DD_INSTRUMENTATION_TELEMETRY_ENABLED", "telemetry_enabled", default=False - ), )