Skip to content

Commit

Permalink
chore(integrations): move django, dogpile_cache,dramtiq,elasticsearch…
Browse files Browse the repository at this point in the history
…,falcon to internal (#10110)

- Moves all integration internals in ddtrace/contrib/(integration name)/
to ddtrace/contrib/internal/(integration name)/ for django,
dogpile_cache, dramatiq, elasticsearch, and falcon
- Ensures ddtrace/contrib/(integration name)/ and
ddtrace/contrib/(integration name)/ continue to expose the same
functions, classes, imports, and module level variables (via from
..internal.integration.module import * imports).
- Log a deprecation warning if internal modules in
ddtrace/contrib/(integration name)/ and ddtrace/contrib/(integration
name)/. Only patch and unpack methods should be exposed by these
packages.
- #9996

## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [ ] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)

---------

Co-authored-by: Emmett Butler <[email protected]>
  • Loading branch information
rachelyangdog and emmettbutler authored Aug 9, 2024
1 parent 741756d commit 5d8e52a
Show file tree
Hide file tree
Showing 33 changed files with 2,299 additions and 2,140 deletions.
12 changes: 8 additions & 4 deletions ddtrace/contrib/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,13 @@

with require_modules(required_modules) as missing_modules:
if not missing_modules:
from . import patch as _patch
from .patch import get_version
from .patch import patch
from .patch import unpatch
# Required to allow users to import from `ddtrace.contrib.django.patch` directly
from . import patch as _ # noqa: F401, I001

# Expose public methods
from ..internal.django.patch import patch as _patch
from ..internal.django.patch import get_version
from ..internal.django.patch import patch
from ..internal.django.patch import unpatch

__all__ = ["patch", "unpatch", "_patch", "get_version"]
43 changes: 11 additions & 32 deletions ddtrace/contrib/django/_asgi.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
"""
Module providing async hooks. Do not import this module unless using Python >= 3.6.
"""
from ddtrace.contrib.asgi import span_from_scope
from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning
from ddtrace.vendor.debtcollector import deprecate

from ...internal.utils import get_argument_value
from .. import trace_utils
from .utils import REQUEST_DEFAULT_RESOURCE
from .utils import _after_request_tags
from .utils import _before_request_tags
from ..internal.django._asgi import * # noqa: F401,F403


@trace_utils.with_traced_module
async def traced_get_response_async(django, pin, func, instance, args, kwargs):
"""Trace django.core.handlers.base.BaseHandler.get_response() (or other implementations).
def __getattr__(name):
deprecate(
("%s.%s is deprecated" % (__name__, name)),
category=DDTraceDeprecationWarning,
)

This is the main entry point for requests.
Django requests are handled by a Handler.get_response method (inherited from base.BaseHandler).
This method invokes the middleware chain and returns the response generated by the chain.
"""
request = get_argument_value(args, kwargs, 0, "request")
span = span_from_scope(request.scope)
if span is None:
return await func(*args, **kwargs)

# Reset the span resource so we can know if it was modified during the request or not
span.resource = REQUEST_DEFAULT_RESOURCE
_before_request_tags(pin, span, request)
response = None
try:
response = await func(*args, **kwargs)
finally:
# DEV: Always set these tags, this is where `span.resource` is set
_after_request_tags(pin, span, request, response)
return response
if name in globals():
return globals()[name]
raise AttributeError("%s has no attribute %s", __name__, name)
38 changes: 11 additions & 27 deletions ddtrace/contrib/django/compat.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import django
from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning
from ddtrace.vendor.debtcollector import deprecate

from ..internal.django.compat import * # noqa: F401,F403

if django.VERSION >= (1, 10, 1):
from django.urls import get_resolver

def user_is_authenticated(user):
# Explicit comparison due to the following bug
# https://code.djangoproject.com/ticket/26988
return user.is_authenticated == True # noqa E712
def __getattr__(name):
deprecate(
("%s.%s is deprecated" % (__name__, name)),
category=DDTraceDeprecationWarning,
)

else:
from django.conf import settings
from django.core import urlresolvers

def user_is_authenticated(user):
return user.is_authenticated()

if django.VERSION >= (1, 9, 0):

def get_resolver(urlconf=None):
urlconf = urlconf or settings.ROOT_URLCONF
urlresolvers.set_urlconf(urlconf)
return urlresolvers.get_resolver(urlconf)

else:

def get_resolver(urlconf=None):
urlconf = urlconf or settings.ROOT_URLCONF
urlresolvers.set_urlconf(urlconf)
return urlresolvers.RegexURLResolver(r"^/", urlconf)
if name in globals():
return globals()[name]
raise AttributeError("%s has no attribute %s", __name__, name)
Loading

0 comments on commit 5d8e52a

Please sign in to comment.