Skip to content

pynamodb: make the patch compatible with PynamoDB 4 #177

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

Merged
merged 1 commit into from
Oct 9, 2019
Merged
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
19 changes: 14 additions & 5 deletions aws_xray_sdk/ext/pynamodb/patch.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import botocore.vendored.requests.sessions
import json
import wrapt
import pynamodb

from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.models import http
from aws_xray_sdk.ext.boto_utils import _extract_whitelisted_params

PYNAMODB4 = int(pynamodb.__version__.split('.')[0]) >= 4


def patch():
"""Patch PynamoDB so it generates subsegements when calling DynamoDB."""
import pynamodb

if hasattr(botocore.vendored.requests.sessions, '_xray_enabled'):
return
setattr(botocore.vendored.requests.sessions, '_xray_enabled', True)

if PYNAMODB4:
module = 'botocore.httpsession'
name = 'URLLib3Session.send'
else:
module = 'botocore.vendored.requests.sessions'
name = 'Session.send'
wrapt.wrap_function_wrapper(
'botocore.vendored.requests.sessions',
'Session.send',
_xray_traced_pynamodb,
module, name, _xray_traced_pynamodb,
)


Expand Down Expand Up @@ -59,7 +65,10 @@ def pynamodb_meta_processor(wrapped, instance, args, kwargs, return_value,
subsegment.add_error_flag()
subsegment.add_exception(exception, stack, True)

resp = return_value.json() if return_value else None
if PYNAMODB4:
resp = json.loads(return_value.text) if return_value else None
else:
resp = return_value.json() if return_value else None
_extract_whitelisted_params(subsegment.name, operation_name, aws_meta,
[None, json.loads(args[0].body.decode('utf-8'))],
None, resp)
Expand Down