Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions datadog_lambda/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import logging
import os

from datadog_lambda.extension import is_extension_present

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -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"
)
35 changes: 33 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")

Expand Down Expand Up @@ -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"),
Comment thread
purple4reina marked this conversation as resolved.
Resolved
(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
Loading