-
Notifications
You must be signed in to change notification settings - Fork 144
Replace jsonpickle with json to serialize entity #275
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
Changes from 4 commits
3e8642b
2d66ba3
7abff72
827b83e
389eacc
7bc1cc5
7fcf969
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Release X-Ray Python SDK | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
version: | ||
description: The version to tag the release with, e.g., 1.2.0, 1.3.0 | ||
required: true | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout master branch | ||
uses: actions/checkout@v2 | ||
|
||
- name: Create Release | ||
id: create_release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: '${{ github.event.inputs.version }}' | ||
release_name: '${{ github.event.inputs.version }} Release' | ||
body: 'See details in [CHANGELOG](https://github.com/aws/aws-xray-sdk-python/blob/master/CHANGELOG.rst)' | ||
draft: true | ||
prerelease: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,10 @@ | |
import time | ||
import string | ||
|
||
import jsonpickle | ||
import json | ||
|
||
from ..utils.compat import annotation_value_types, string_types | ||
from ..utils.conversion import metadata_to_dict | ||
from .throwable import Throwable | ||
from . import http | ||
from ..exceptions.exceptions import AlreadyEndedException | ||
|
@@ -256,36 +257,42 @@ def get_origin_trace_header(self): | |
def serialize(self): | ||
""" | ||
Serialize to JSON document that can be accepted by the | ||
X-Ray backend service. It uses jsonpickle to perform | ||
serialization. | ||
X-Ray backend service. It uses json to perform serialization. | ||
""" | ||
try: | ||
return jsonpickle.encode(self, unpicklable=False) | ||
return json.dumps(self.to_dict(), default=str) | ||
except Exception: | ||
log.exception("got an exception during serialization") | ||
|
||
def _delete_empty_properties(self, properties): | ||
def to_dict(self): | ||
""" | ||
Delete empty properties before serialization to avoid | ||
extra keys with empty values in the output json. | ||
Convert Entity(Segment/Subsegment) object to dict | ||
with required properties that have non-empty values. | ||
""" | ||
if not self.parent_id: | ||
del properties['parent_id'] | ||
if not self.subsegments: | ||
del properties['subsegments'] | ||
if not self.aws: | ||
del properties['aws'] | ||
if not self.http: | ||
del properties['http'] | ||
if not self.cause: | ||
del properties['cause'] | ||
if not self.annotations: | ||
del properties['annotations'] | ||
if not self.metadata: | ||
del properties['metadata'] | ||
properties.pop(ORIGIN_TRACE_HEADER_ATTR_KEY, None) | ||
|
||
del properties['sampled'] | ||
entity_dict = {} | ||
|
||
for key, value in vars(self).items(): | ||
if isinstance(value, bool) or value: | ||
if key == 'subsegments': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we handle the case if key is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# child subsegments are stored as List | ||
subsegments = [] | ||
for subsegment in value: | ||
subsegments.append(subsegment.to_dict()) | ||
entity_dict[key] = subsegments | ||
elif key == 'cause': | ||
entity_dict[key] = {} | ||
entity_dict[key]['working_directory'] = self.cause['working_directory'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't dive into this implementation yet to provide a fix, but this is causing a regression in the AWS Lambda Powertools Tracer feature leading to a |
||
# exceptions are stored as List | ||
throwables = [] | ||
for throwable in value['exceptions']: | ||
throwables.append(throwable.to_dict()) | ||
entity_dict[key]['exceptions'] = throwables | ||
elif key == 'metadata': | ||
entity_dict[key] = metadata_to_dict(value) | ||
elif key != 'sampled' and key != ORIGIN_TRACE_HEADER_ATTR_KEY: | ||
entity_dict[key] = value | ||
|
||
return entity_dict | ||
|
||
def _check_ended(self): | ||
if not self.in_progress: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
def metadata_to_dict(obj): | ||
""" | ||
Convert object to dict with all serializable properties like: | ||
dict, list, set, tuple, str, bool, int, float, type, object, etc. | ||
""" | ||
try: | ||
if isinstance(obj, dict): | ||
metadata = {} | ||
for key, value in obj.items(): | ||
metadata[key] = metadata_to_dict(value) | ||
return metadata | ||
elif isinstance(obj, type): | ||
return str(obj) | ||
elif hasattr(obj, "_ast"): | ||
return metadata_to_dict(obj._ast()) | ||
elif hasattr(obj, "__iter__") and not isinstance(obj, str): | ||
metadata = [] | ||
for item in obj: | ||
metadata.append(metadata_to_dict(item)) | ||
return metadata | ||
elif hasattr(obj, "__dict__"): | ||
metadata = {} | ||
for key, value in vars(obj).items(): | ||
if not callable(value) and not key.startswith('_'): | ||
metadata[key] = metadata_to_dict(value) | ||
return metadata | ||
else: | ||
return obj | ||
except Exception: | ||
log.warning("Failed to convert " + str(obj) + " to dict") | ||
lupengamzn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return {} | ||
lupengamzn marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,6 @@ | |
], | ||
|
||
install_requires=[ | ||
'jsonpickle', | ||
'enum34;python_version<"3.4"', | ||
'wrapt', | ||
'future', | ||
|
Uh oh!
There was an error while loading. Please reload this page.