Skip to content

Commit

Permalink
Merge branch 'main' into avara1986/APPSEC-56157-migrate_iast_django
Browse files Browse the repository at this point in the history
  • Loading branch information
avara1986 committed Jan 17, 2025
2 parents 820ec47 + 26dc258 commit bca3ec9
Show file tree
Hide file tree
Showing 40 changed files with 592 additions and 335 deletions.
15 changes: 9 additions & 6 deletions ddtrace/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import os
import warnings


Expand Down Expand Up @@ -42,40 +43,42 @@
# initialization, which added this module to sys.modules. We catch deprecation
# warnings as this is only to retain a side effect of the package
# initialization.
# TODO: Remove this in v3.0 when the ddtrace/tracer.py module is removed
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from .tracer import Tracer as _


__version__ = get_version()

# a global tracer instance with integration settings
tracer = Tracer()
# TODO: Deprecate accessing tracer from ddtrace.__init__ module in v4.0
if os.environ.get("_DD_GLOBAL_TRACER_INIT", "true").lower() in ("1", "true"):
from ddtrace.trace import tracer # noqa: F401

__all__ = [
"patch",
"patch_all",
"Pin",
"Span",
"tracer",
"Tracer",
"config",
"DDTraceDeprecationWarning",
]


_DEPRECATED_MODULE_ATTRIBUTES = [
_DEPRECATED_TRACE_ATTRIBUTES = [
"Span",
"Tracer",
"Pin",
]


def __getattr__(name):
if name in _DEPRECATED_MODULE_ATTRIBUTES:
if name in _DEPRECATED_TRACE_ATTRIBUTES:
debtcollector.deprecate(
("%s.%s is deprecated" % (__name__, name)),
message="Import from ddtrace.trace instead.",
category=DDTraceDeprecationWarning,
removal_version="3.0.0",
)

if name in globals():
Expand Down
34 changes: 27 additions & 7 deletions ddtrace/_trace/pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import wrapt

import ddtrace
from ddtrace.vendor.debtcollector import deprecate

from ..internal.logger import get_logger

Expand Down Expand Up @@ -41,6 +42,12 @@ def __init__(
_config=None, # type: Optional[Dict[str, Any]]
):
# type: (...) -> None
if tracer is not None and tracer is not ddtrace.tracer:
deprecate(
"Initializing ddtrace.Pin with `tracer` argument is deprecated",
message="All Pin instances should use the global tracer instance",
removal_version="3.0.0",
)
tracer = tracer or ddtrace.tracer
self.tags = tags
self.tracer = tracer
Expand Down Expand Up @@ -72,15 +79,15 @@ def __repr__(self):
def _find(*objs):
# type: (Any) -> Optional[Pin]
"""
Return the first :class:`ddtrace.trace.Pin` found on any of the provided objects or `None` if none were found
Return the first :class:`ddtrace.pin.Pin` found on any of the provided objects or `None` if none were found
>>> pin = Pin._find(wrapper, instance, conn)
:param objs: The objects to search for a :class:`ddtrace.trace.Pin` on
:param objs: The objects to search for a :class:`ddtrace.pin.Pin` on
:type objs: List of objects
:rtype: :class:`ddtrace.trace.Pin`, None
:returns: The first found :class:`ddtrace.trace.Pin` or `None` is none was found
:rtype: :class:`ddtrace.pin.Pin`, None
:returns: The first found :class:`ddtrace.pin.Pin` or `None` is none was found
"""
for obj in objs:
pin = Pin.get_from(obj)
Expand All @@ -98,10 +105,10 @@ def get_from(obj):
>>> pin = Pin.get_from(conn)
:param obj: The object to look for a :class:`ddtrace.trace.Pin` on
:param obj: The object to look for a :class:`ddtrace.pin.Pin` on
:type obj: object
:rtype: :class:`ddtrace.trace.Pin`, None
:returns: :class:`ddtrace.trace.Pin` associated with the object or None
:rtype: :class:`ddtrace.pin.Pin`, None
:returns: :class:`ddtrace.pin.Pin` associated with the object, or None if none was found
"""
if hasattr(obj, "__getddpin__"):
return obj.__getddpin__()
Expand Down Expand Up @@ -132,6 +139,12 @@ def override(
>>> # Override a pin for a specific connection
>>> Pin.override(conn, service='user-db')
"""
if tracer is not None:
deprecate(
"Calling ddtrace.Pin.override(...) with the `tracer` argument is deprecated",
message="All Pin instances should use the global tracer instance",
removal_version="3.0.0",
)
if not obj:
return

Expand Down Expand Up @@ -193,6 +206,13 @@ def clone(
if not tags and self.tags:
tags = self.tags.copy()

if tracer is not None:
deprecate(
"Initializing ddtrace.Pin with `tracer` argument is deprecated",
message="All Pin instances should use the global tracer instance",
removal_version="3.0.0",
)

# we use a copy instead of a deepcopy because we expect configurations
# to have only a root level dictionary without nested objects. Using
# deepcopy introduces a big overhead:
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/_trace/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ddtrace import _hooks
from ddtrace import config
from ddtrace._trace.context import Context
from ddtrace._trace.filters import TraceFilter
from ddtrace._trace.processor import SpanAggregator
from ddtrace._trace.processor import SpanProcessor
from ddtrace._trace.processor import TopLevelSpanProcessor
Expand Down Expand Up @@ -68,7 +69,6 @@
from ddtrace.settings import Config
from ddtrace.settings.asm import config as asm_config
from ddtrace.settings.peer_service import _ps_config
from ddtrace.trace import TraceFilter
from ddtrace.vendor.debtcollector import deprecate


Expand Down
2 changes: 2 additions & 0 deletions ddtrace/appsec/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class WAF_DATA_NAMES(metaclass=Constant_Class):
REQUEST_COOKIES: Literal["server.request.cookies"] = "server.request.cookies"
REQUEST_HTTP_IP: Literal["http.client_ip"] = "http.client_ip"
REQUEST_USER_ID: Literal["usr.id"] = "usr.id"
REQUEST_USERNAME: Literal["usr.login"] = "usr.login"
RESPONSE_STATUS: Literal["server.response.status"] = "server.response.status"
RESPONSE_HEADERS_NO_COOKIES: Literal["server.response.headers.no_cookies"] = "server.response.headers.no_cookies"
RESPONSE_BODY: Literal["server.response.body"] = "server.response.body"
Expand All @@ -196,6 +197,7 @@ class WAF_DATA_NAMES(metaclass=Constant_Class):
REQUEST_COOKIES,
REQUEST_HTTP_IP,
REQUEST_USER_ID,
REQUEST_USERNAME,
RESPONSE_STATUS,
RESPONSE_HEADERS_NO_COOKIES,
RESPONSE_BODY,
Expand Down
Loading

0 comments on commit bca3ec9

Please sign in to comment.