Skip to content
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

chore: privatizing methods in tornado,unittest,urllib,urllib3,vertica #9928

Closed
Show file tree
Hide file tree
Changes from 3 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
14 changes: 13 additions & 1 deletion ddtrace/contrib/tornado/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import ddtrace
from ddtrace import config
from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning
from ddtrace.vendor.debtcollector import deprecate
from ddtrace.vendor.wrapt import wrap_function_wrapper as _w

from ...internal.utils.formats import asbool
Expand All @@ -23,11 +25,21 @@
)


def get_version():
def _get_version():
# type: () -> str
return getattr(tornado, "version", "")


def get_version():
deprecate(
"get_version is deprecated",
message="get_version is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _get_version()


def patch():
"""
Tracing function that patches the Tornado web application so that it will be
Expand Down
155 changes: 133 additions & 22 deletions ddtrace/contrib/unittest/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
from ddtrace.internal.ci_visibility.utils import get_relative_or_absolute_path_for_path
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal.logger import get_logger
from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning
from ddtrace.internal.utils.formats import asbool
from ddtrace.internal.utils.wrappers import unwrap as _u
from ddtrace.vendor import wrapt
from ddtrace.vendor.debtcollector import deprecate


log = get_logger(__name__)
Expand All @@ -64,11 +66,21 @@
)


def get_version():
def _get_version():
# type: () -> str
return ""


def get_version():
deprecate(
"get_version is deprecated",
message="get_version is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _get_version()


def _enable_unittest_if_not_started():
_initialize_unittest_data()
if _CIVisibility.enabled:
Expand Down Expand Up @@ -408,17 +420,17 @@ def patch():

_w = wrapt.wrap_function_wrapper

_w(unittest, "TextTestResult.addSuccess", add_success_test_wrapper)
_w(unittest, "TextTestResult.addFailure", add_failure_test_wrapper)
_w(unittest, "TextTestResult.addError", add_failure_test_wrapper)
_w(unittest, "TextTestResult.addSkip", add_skip_test_wrapper)
_w(unittest, "TextTestResult.addExpectedFailure", add_xfail_test_wrapper)
_w(unittest, "TextTestResult.addUnexpectedSuccess", add_xpass_test_wrapper)
_w(unittest, "skipIf", skip_if_decorator)
_w(unittest, "TestCase.run", handle_test_wrapper)
_w(unittest, "TestSuite.run", collect_text_test_runner_session)
_w(unittest, "TextTestRunner.run", handle_text_test_runner_wrapper)
_w(unittest, "TestProgram.runTests", handle_cli_run)
_w(unittest, "TextTestResult.addSuccess", _add_success_test_wrapper)
_w(unittest, "TextTestResult.addFailure", _add_failure_test_wrapper)
_w(unittest, "TextTestResult.addError", _add_failure_test_wrapper)
_w(unittest, "TextTestResult.addSkip", _add_skip_test_wrapper)
_w(unittest, "TextTestResult.addExpectedFailure", _add_xfail_test_wrapper)
_w(unittest, "TextTestResult.addUnexpectedSuccess", _add_xpass_test_wrapper)
_w(unittest, "skipIf", _skip_if_decorator)
_w(unittest, "TestCase.run", _handle_test_wrapper)
_w(unittest, "TestSuite.run", _collect_text_test_runner_session)
_w(unittest, "TextTestRunner.run", _handle_text_test_runner_wrapper)
_w(unittest, "TestProgram.runTests", _handle_cli_run)


def unpatch():
Expand Down Expand Up @@ -473,41 +485,90 @@ def _set_test_xpass_xfail_result(test_item, result: str):
span.set_tag_str(test.STATUS, test.Status.PASS.value)


def add_success_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
def _add_success_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
if _is_valid_result(instance, args):
_set_test_span_status(test_item=args[0], status=test.Status.PASS.value)

return func(*args, **kwargs)


def add_failure_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
def add_success_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
deprecate(
"add_success_test_wrapper is deprecated",
message="add_success_test_wrapper is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _add_success_test_wrapper(func, instance, args, kwargs)


def _add_failure_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
if _is_valid_result(instance, args):
_set_test_span_status(test_item=args[0], exc_info=_extract_test_reason(args), status=test.Status.FAIL.value)

return func(*args, **kwargs)


def add_xfail_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
def add_failure_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
deprecate(
"add_failure_test_wrapper is deprecated",
message="add_failure_test_wrapper is deprecated",
removal_version="3.0.0",
)
return _add_failure_test_wrapper(func, instance, args, kwargs)


def _add_xfail_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
if _is_valid_result(instance, args):
_set_test_xpass_xfail_result(test_item=args[0], result=test.Status.XFAIL.value)

return func(*args, **kwargs)


def add_skip_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
def add_xfail_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
deprecate(
"add_xfail_test_wrapper is deprecated",
message="add_xfail_test_wrapper is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _add_xfail_test_wrapper(func, instance, args, kwargs)


def _add_skip_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
if _is_valid_result(instance, args):
_set_test_span_status(test_item=args[0], skip_reason=_extract_test_reason(args), status=test.Status.SKIP.value)

return func(*args, **kwargs)


def add_xpass_test_wrapper(func, instance, args: tuple, kwargs: dict):
def add_skip_test_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
deprecate(
"add_skip_test_wrapper is deprecated",
message="add_skip_test_wrapper is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _add_skip_test_wrapper(func, instance, args, kwargs)


def _add_xpass_test_wrapper(func, instance, args: tuple, kwargs: dict):
if _is_valid_result(instance, args):
_set_test_xpass_xfail_result(test_item=args[0], result=test.Status.XPASS.value)

return func(*args, **kwargs)


def add_xpass_test_wrapper(func, instance, args: tuple, kwargs: dict):
deprecate(
"add_xpass_test_wrapper is deprecated",
message="add_xpass_test_wrapper is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _add_xpass_test_wrapper(func, instance, args, kwargs)


def _mark_test_as_unskippable(obj):
test_name = obj.__name__
test_suite_name = str(obj).split(".")[0].split()[1]
Expand All @@ -521,13 +582,23 @@ def _using_unskippable_decorator(args, kwargs):
return args[0] is False and _extract_skip_if_reason(args, kwargs) == ITR_UNSKIPPABLE_REASON


def skip_if_decorator(func, instance, args: tuple, kwargs: dict):
def _skip_if_decorator(func, instance, args: tuple, kwargs: dict):
if _using_unskippable_decorator(args, kwargs):
return _mark_test_as_unskippable
return func(*args, **kwargs)


def handle_test_wrapper(func, instance, args: tuple, kwargs: dict):
def skip_if_decorator(func, instance, args: tuple, kwargs: dict):
deprecate(
"skip_if_decorator is deprecated",
message="skip_if_decorator is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _skip_if_decorator(func, instance, args, kwargs)


def _handle_test_wrapper(func, instance, args: tuple, kwargs: dict):
"""
Creates module and suite spans for `unittest` test executions.
"""
Expand Down Expand Up @@ -602,7 +673,17 @@ def handle_test_wrapper(func, instance, args: tuple, kwargs: dict):
return func(*args, **kwargs)


def collect_text_test_runner_session(func, instance: unittest.TestSuite, args: tuple, kwargs: dict):
def handle_test_wrapper(func, instance, args: tuple, kwargs: dict):
deprecate(
"handle_test_wrapper is deprecated",
message="handle_test_wrapper is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _handle_test_wrapper(func, instance, args, kwargs)


def _collect_text_test_runner_session(func, instance: unittest.TestSuite, args: tuple, kwargs: dict):
"""
Discovers test suites and tests for the current `unittest` `TextTestRunner` execution
"""
Expand All @@ -621,6 +702,16 @@ def collect_text_test_runner_session(func, instance: unittest.TestSuite, args: t
return result


def collect_text_test_runner_session(func, instance: unittest.TestSuite, args: tuple, kwargs: dict):
deprecate(
"collect_text_test_runner_session is deprecated",
message="collect_text_test_runner_session is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _collect_text_test_runner_session(func, instance, args, kwargs)


def _start_test_session_span(instance) -> ddtrace.Span:
"""
Starts a test session span and sets the required tags for a `unittest` session instance.
Expand Down Expand Up @@ -810,7 +901,7 @@ def _finish_test_session_span():
_finish_span(_CIVisibility._datadog_session_span)


def handle_cli_run(func, instance: unittest.TestProgram, args: tuple, kwargs: dict):
def _handle_cli_run(func, instance: unittest.TestProgram, args: tuple, kwargs: dict):
"""
Creates session span and discovers test suites and tests for the current `unittest` CLI execution
"""
Expand All @@ -836,7 +927,17 @@ def handle_cli_run(func, instance: unittest.TestProgram, args: tuple, kwargs: di
return result


def handle_text_test_runner_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
def handle_cli_run(func, instance: unittest.TestProgram, args: tuple, kwargs: dict):
deprecate(
"handle_cli_run is deprecated",
message="handle_cli_run is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _handle_cli_run(func, instance, args, kwargs)


def _handle_text_test_runner_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
"""
Creates session span if unittest is called through the `TextTestRunner` method
"""
Expand All @@ -862,3 +963,13 @@ def handle_text_test_runner_wrapper(func, instance: unittest.TextTestRunner, arg
_finish_test_session_span()
del _CIVisibility._datadog_session_span
return result


def handle_text_test_runner_wrapper(func, instance: unittest.TextTestRunner, args: tuple, kwargs: dict):
deprecate(
"handle_text_test_runner_wrapper is deprecated",
message="handle_text_test_runner_wrapper is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _handle_text_test_runner_wrapper(func, instance, args, kwargs)
14 changes: 13 additions & 1 deletion ddtrace/contrib/urllib/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
from ddtrace.appsec._common_module_patches import wrapped_request_D8CB81E472AF98A2 as _wrap_open
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
from ddtrace.appsec._iast.constants import VULN_SSRF
from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning
from ddtrace.settings.asm import config as asm_config
from ddtrace.vendor.debtcollector import deprecate
from ddtrace.vendor.wrapt import wrap_function_wrapper as _w

from ..trace_utils import unwrap as _u


def get_version():
def _get_version():
# type: () -> str
return ""


def get_version():
deprecate(
"get_version is deprecated",
message="get_version is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _get_version()


def patch():
"""patch the built-in urllib.request methods for tracing"""
if getattr(urllib.request, "__datadog_patch", False):
Expand Down
14 changes: 13 additions & 1 deletion ddtrace/contrib/urllib3/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from ddtrace.appsec._iast.constants import VULN_SSRF
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal.schema.span_attribute_schema import SpanDirection
from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning
from ddtrace.pin import Pin
from ddtrace.settings.asm import config as asm_config
from ddtrace.vendor.debtcollector import deprecate
from ddtrace.vendor.wrapt import wrap_function_wrapper as _w

from ...constants import ANALYTICS_SAMPLE_RATE_KEY
Expand Down Expand Up @@ -42,11 +44,21 @@
)


def get_version():
def _get_version():
# type: () -> str
return getattr(urllib3, "__version__", "")


def get_version():
deprecate(
"get_version is deprecated",
message="get_version is deprecated",
removal_version="3.0.0",
category=DDTraceDeprecationWarning,
)
return _get_version()


def patch():
"""Enable tracing for all urllib3 requests"""
if getattr(urllib3, "__datadog_patch", False):
Expand Down
Loading
Loading