Skip to content

perf(rc): avoid calling config subscribers on duplicate values #13933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 15, 2025
17 changes: 16 additions & 1 deletion ddtrace/settings/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ def set_value_source(self, value: Any, source: _ConfigSource) -> None:
else:
log.warning("Invalid source: %s", source)

def get_value_source(self, source: _ConfigSource) -> _JSONType:
if source == "code":
return self._code_value
elif source == "remote_config":
return self._rc_value
elif source == "env_var":
return self._env_value
elif source == "default":
return self._default_value
else:
log.warning("Invalid source: %s", source)

def set_code(self, value: _JSONType) -> None:
self._code_value = value

Expand Down Expand Up @@ -790,8 +802,11 @@ def _set_config_items(self, items):
# type: (List[Tuple[str, Any, _ConfigSource]]) -> None
item_names = []
for key, value, origin in items:
item_names.append(key)
item = self._config[key]
if item.get_value_source(origin) == value:
# No change in config value, no need to notify subscribers or report telemetry
continue
item_names.append(key)
item.set_value_source(value, origin)
telemetry_writer.add_configuration(item._name, item.value(), item.source())
self._notify_subscribers(item_names)
Expand Down
34 changes: 34 additions & 0 deletions tests/internal/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,37 @@ def test_config_public_properties_and_methods():
"tags",
"version",
}, public_attrs


@pytest.mark.subprocess()
def test_subscription_handler_called_once_for_duplicate_values():
"""This test ensures that the subscription handler is called only once
when the value is set multiple times to the same value.
It also checks that the handler is called again when the value changes.
"""
from ddtrace import config

call_count = 0

def _handler(cfg, items):
global call_count
call_count += 1

config._subscribe(
[
"_logs_injection",
],
_handler,
)

assert call_count == 0, "Handler should not be called before setting the value"
for _ in range(3):
config._logs_injection = "true"
assert call_count == 1, "Handler should be called only once for the same value"

for _ in range(3):
config._logs_injection = "false"
assert call_count == 2, "Handler should be called only once for the same value"

config._logs_injection = "true"
assert call_count == 3, "Handler should be called again for a different value"
Loading