diff --git a/datadog_lambda/config.py b/datadog_lambda/config.py index 6487c9e7..93f5fc2e 100644 --- a/datadog_lambda/config.py +++ b/datadog_lambda/config.py @@ -6,6 +6,8 @@ import logging import os +from datadog_lambda.extension import is_extension_present + logger = logging.getLogger(__name__) @@ -170,3 +172,18 @@ def _reset(self): and not config.appsec_enabled ): os.environ["DD_INSTRUMENTATION_TELEMETRY_ENABLED"] = "false" + +# The extension forwards feature flag config requests with the API key it +# resolved (e.g. from DD_API_KEY_SECRET_ARN). Only use it when DD_API_KEY is +# unset: an extension without that route returns 404, and the tracer does not +# fall back to the default endpoint. +if ( + is_extension_present() + and not os.environ.get("DD_API_KEY") + and not os.environ.get( + "DD_FEATURE_FLAGS_CONFIGURATION_SOURCE_AGENTLESS_BASE_URL", "" + ).strip() +): + os.environ["DD_FEATURE_FLAGS_CONFIGURATION_SOURCE_AGENTLESS_BASE_URL"] = ( + "http://127.0.0.1:8126" + ) diff --git a/tests/test_config.py b/tests/test_config.py index 9c5da63a..8b2043da 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -21,8 +21,10 @@ def test_config_import_does_not_import_ddtrace(monkeypatch): import datadog_lambda with monkeypatch.context() as mp: + # Also evict datadog_lambda submodules so that modules config imports + # (e.g. datadog_lambda.extension) run again under the blocker. for name in list(sys.modules): - if name == "ddtrace" or name.startswith("ddtrace."): + if name.startswith(("ddtrace", "datadog_lambda.")): mp.delitem(sys.modules, name, raising=False) class _BlockDdtrace(importlib.abc.MetaPathFinder): @@ -35,7 +37,7 @@ def find_spec(self, fullname, path=None, target=None): 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) + mp.delattr(datadog_lambda, "extension", raising=False) importlib.invalidate_caches() importlib.import_module("datadog_lambda.config") @@ -260,3 +262,32 @@ def cap_warn(*args, **kwargs): testing.test_4 assert not logs + + +_ffe_url = "DD_FEATURE_FLAGS_CONFIGURATION_SOURCE_AGENTLESS_BASE_URL" + + +@pytest.mark.parametrize( + "extension,env,expected", + [ + (True, {}, "http://127.0.0.1:8126"), + (True, {"DD_API_KEY": ""}, "http://127.0.0.1:8126"), + (True, {"DD_API_KEY_SECRET_ARN": "arn"}, "http://127.0.0.1:8126"), + (True, {"DD_API_KEY": "key"}, None), + (True, {_ffe_url: "http://custom"}, "http://custom"), + (True, {_ffe_url: ""}, "http://127.0.0.1:8126"), + (True, {_ffe_url: " "}, "http://127.0.0.1:8126"), + (False, {}, None), + ], +) +@pytest.mark.filterwarnings("ignore:.*found in sys.modules:RuntimeWarning") +def test_feature_flags_base_url(extension, env, expected, monkeypatch): + import os + import runpy + + monkeypatch.setattr(os, "environ", dict(env)) + monkeypatch.setattr( + "datadog_lambda.extension.is_extension_present", lambda: extension + ) + runpy.run_module("datadog_lambda.config") + assert os.environ.get(_ffe_url) == expected