Skip to content

Commit

Permalink
add test for warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Oct 7, 2024
1 parent e456701 commit f3153d4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
26 changes: 19 additions & 7 deletions ddtrace/settings/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class Config(object):
"trace_methods": ("_trace_methods", "DD_TRACE_METHODS"),
"ci_visibility_log_level": ("_ci_visibility_log_level", "DD_CIVISIBILITY_LOG_LEVEL"),
"service_mapping": ("_service_mapping", "DD_SERVICE_MAPPING"),
"_logs_injection": ("_logs_injection", "DD_LOGS_INJECTION"),
"logs_injection": ("_logs_injection", "DD_LOGS_INJECTION"),
}

class _HTTPServerConfig(object):
Expand Down Expand Up @@ -627,18 +627,22 @@ def __init__(self):
self._inject_was_attempted = _get_config("_DD_INJECT_WAS_ATTEMPTED", False, asbool)

def __getattr__(self, name) -> Any:
if name in self._config:
return self._config[name].value()
elif name in self._DEPRECATED_ATTRS:
internal_name, env_var = self._DEPRECATED_ATTRS[name]
if name in self._DEPRECATED_ATTRS:
new_name, env_var = self._DEPRECATED_ATTRS[name]
assert (
new_name != name
), f"Circular mapping detected: deprecated attribute {name} in {self._DEPRECATED_ATTRS} maps to itself"
deprecate(
f"ddtrace.config.{name} is deprecated",
message=f"Use {env_var} configuration instead. "
"This configuration must be set before importing ddtrace.",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return self._config[internal_name].value()
return getattr(self, new_name)

if name in self._config:
return self._config[name].value()

if name not in self._integration_configs:
self._integration_configs[name] = IntegrationConfig(self, name)
Expand Down Expand Up @@ -768,7 +772,15 @@ def __setattr__(self, key, value):
else:
if key in self._DEPRECATED_ATTRS:
# replace deprecated attribute name with the new name
key, _ = self._DEPRECATED_ATTRS[key]
new_key, env_var = self._DEPRECATED_ATTRS[key]
deprecate(
f"ddtrace.config.{key} is deprecated",
message=f"Use {env_var} configuration instead. "
"This configuration must be set before importing ddtrace.",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
key = new_key
return super(self.__class__, self).__setattr__(key, value)

def _set_config_items(self, items):
Expand Down
57 changes: 57 additions & 0 deletions tests/tracer/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import pytest

from ddtrace.settings import Config
Expand Down Expand Up @@ -211,3 +213,58 @@ def test_x_datadog_tags(env, expected):
with override_env(env):
_ = Config()
assert expected == (_._x_datadog_tags_max_length, _._x_datadog_tags_enabled)


@pytest.mark.parametrize(
"deprecated_name,name,test_value,env",
(
("http_tag_query_string", "_http_tag_query_string", True, "DD_TRACE_HTTP_CLIENT_TAG_QUERY_STRING"),
("trace_http_header_tags", "_trace_http_header_tags", {"x-dd": "x_dd"}, "DD_TRACE_HEADER_TAGS"),
("report_hostname", "_report_hostname", True, "DD_TRACE_REPORT_HOSTNAME"),
("health_metrics_enabled", "_health_metrics_enabled", True, "DD_TRACE_HEALTH_METRICS_ENABLED"),
("analytics_enabled", "_analytics_enabled", True, "DD_TRACE_ANALYTICS_ENABLED"),
("client_ip_header", "_client_ip_header", True, "DD_TRACE_CLIENT_IP_HEADER"),
("retrieve_client_ip", "_retrieve_client_ip", True, "DD_TRACE_CLIENT_IP_ENABLED"),
(
"propagation_http_baggage_enabled",
"_propagation_http_baggage_enabled",
True,
"DD_TRACE_PROPAGATION_HTTP_BAGGAGE_ENABLED",
),
(
"global_query_string_obfuscation_disabled",
"_global_query_string_obfuscation_disabled",
True,
'DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP=""',
),
("trace_methods", "_trace_methods", ["monkey.banana_melon"], "DD_TRACE_METHODS"),
("ci_visibility_log_level", "_ci_visibility_log_level", True, "DD_CIVISIBILITY_LOG_LEVEL"),
("service_mapping", "_service_mapping", {"badname": "bettername"}, "DD_SERVICE_MAPPING"),
("logs_injection", "_logs_injection", False, "DD_LOGS_INJECTION"),
),
)
def test_deprecated_config_attributes(deprecated_name, name, test_value, env):
"""Ensures setting and getting deprecated attributes log a warning and still
set/return the expected values.
"""
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
config = Config()
# Test getting/setting a configuration by the expected name
setattr(config, name, test_value)
assert getattr(config, name) == test_value
assert len(warns) == 0
expected_warning = (
f"ddtrace.config.{deprecated_name} is deprecated and will be "
f"removed in version '3.0.0': Use {env} configuration instead. "
"This configuration must be set before importing ddtrace."
)
# Test getting the configuration by the deprecated name
getattr(config, deprecated_name) == test_value
assert len(warns) == 1
assert str(warns[0].message) == expected_warning
# Test setting the configuration by the deprecated name
setattr(config, deprecated_name, None)
assert getattr(config, name) is None
assert len(warns) == 2
assert str(warns[1].message) == expected_warning

0 comments on commit f3153d4

Please sign in to comment.