Skip to content

Filter invalid characters from annotation key names; raise warning if found #22

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 5 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion aws_xray_sdk/core/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# List of valid characters found at http://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
_valid_name_characters = string.ascii_letters + string.digits + '_.:/%&#=+\-@ '
_valid_annotation_key_characters = string.ascii_letters + string.digits + '_'


class Entity(object):
Expand Down Expand Up @@ -140,7 +141,12 @@ def put_annotation(self, key, value):
log.warning("ignoring unsupported annotation value type %s.", type(value))
return

self.annotations[key] = value
sanitized_key = ''.join([c for c in key if c in _valid_annotation_key_characters])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotation key is difference to entity name. User can have 100 lines of code adding annotations to a single entity. Under worst case this could result in 100 times of string concatenation, which adds a lot of overhead.

Invalid key is a user error and the key should be dropped until the author explicit fix it using valid characters.


if sanitized_key != key:
log.warning("ignoring unsupported characters in annotation key %s", key)

self.annotations[sanitized_key] = value

def put_metadata(self, key, value, namespace='default'):
"""
Expand Down
3 changes: 2 additions & 1 deletion docs/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ You can add annotations and metadata to an active segment/subsegment.
Annotations are simple key-value pairs that are indexed for use with
`filter expressions <http://docs.aws.amazon.com/xray/latest/devguide/xray-console-filters.html>`_.
Use annotations to record data that you want to use to group traces in the console,
or when calling the GetTraceSummaries API.
or when calling the GetTraceSummaries API. Annotation keys may only use ASCII letters and the underscore(_)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably replace "may" with "should" as user needs to understand invalid keys are dropped, per RFC2119. Also ASCII digits are missing here.

character.

Metadata are key-value pairs with values of any type, including objects and lists, but that are not indexed.
Use metadata to record data you want to store in the trace but don't need to use for searching traces.
Expand Down