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

Remove patching for botocore.vendored.requests #22

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 7 additions & 22 deletions datadog_lambda/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
# Copyright 2019 Datadog, Inc.

import sys
import logging

from wrapt import wrap_function_wrapper as wrap

from datadog_lambda.tracing import get_dd_trace_context

logger = logging.getLogger(__name__)

if sys.version_info >= (3, 0, 0):
httplib_module = 'http.client'
else:
httplib_module = 'httplib'

_httplib_patched = False
_requests_patched = False
_botocore_requests_patched = False


def patch_all():
Expand All @@ -26,7 +28,6 @@ def patch_all():
"""
_patch_httplib()
_patch_requests()
_patch_botocore_requests()


def _patch_httplib():
Expand All @@ -42,6 +43,7 @@ def _patch_httplib():
'HTTPConnection.request',
_wrap_httplib_request
)
logger.debug('Patched %s', httplib_module)


def _patch_requests():
Expand All @@ -58,26 +60,9 @@ def _patch_requests():
'Session.request',
_wrap_requests_request
)
except ImportError:
pass


def _patch_botocore_requests():
"""
Patch the `requests` module that is packaged into `botocore`.
https://stackoverflow.com/questions/40741282/cannot-use-requests-module-on-aws-lambda
"""
global _botocore_requests_patched
if not _botocore_requests_patched:
_botocore_requests_patched = True
try:
wrap(
'botocore.vendored.requests',
'Session.request',
_wrap_requests_request
)
except ImportError:
pass
logger.debug('Patched requests')
except Exception:
logger.debug('Failed to patch requests', exc_info=True)


def _wrap_requests_request(func, instance, args, kwargs):
Expand Down
10 changes: 0 additions & 10 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from datadog_lambda.patch import (
_patch_httplib,
_patch_requests,
_patch_botocore_requests,
)
from datadog_lambda.constants import TraceHeader

Expand Down Expand Up @@ -42,12 +41,3 @@ def test_patch_requests(self):
self.assertEqual(r.request.headers[TraceHeader.TRACE_ID], '123')
self.assertEqual(r.request.headers[TraceHeader.PARENT_ID], '321')
self.assertEqual(r.request.headers[TraceHeader.SAMPLING_PRIORITY], '2')

def test_patch_botocore_requests(self):
_patch_botocore_requests()
from botocore.vendored import requests
r = requests.get("https://www.datadoghq.com/")
self.mock_get_dd_trace_context.assert_called()
self.assertEqual(r.request.headers[TraceHeader.TRACE_ID], '123')
self.assertEqual(r.request.headers[TraceHeader.PARENT_ID], '321')
self.assertEqual(r.request.headers[TraceHeader.SAMPLING_PRIORITY], '2')