-
Notifications
You must be signed in to change notification settings - Fork 145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e8642b
Replace jsonpickle with json to serialize entity
lupengamzn 2d66ba3
Added workflow to create release tag
lupengamzn 7abff72
Pinned sqlalchemy and Flask-SQLAlchemy for unit test
lupengamzn 827b83e
Fixed version
lupengamzn 389eacc
Changed log to debug level
lupengamzn 7bc1cc5
Update logging
lupengamzn 7fcf969
Added empty line
lupengamzn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
log.exception("Failed to serialize %s", self.name) | ||
|
||
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': | ||
# 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: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.exception("Failed to convert {} to dict".format(str(obj))) | ||
return {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,6 @@ | |
], | ||
|
||
install_requires=[ | ||
'jsonpickle', | ||
'enum34;python_version<"3.4"', | ||
'wrapt', | ||
'future', | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we handle the case if key is
annotations
,aws
orhttp
? I see that you're handlingmetadata
case separately. Shouldn't we do the same withannotations
too? since in that case also we don't know how many key-value pairs would be there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
annotations
,aws
orhttp
are alldict
with serializable values likestring
,int
orboolean
, so there is no need to handle these cases.