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 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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
CHANGELOG
=========

Unreleased
==========
* bugfix: Fixed an issue that caused annotation keys with invalid characters to be silently ignored.

0.95
====
* **Breaking**: AWS API parameter whitelist json file is moved to path `aws_xray_sdk/ext/resources/aws_para_whitelist.json` in `PR6 <https://github.com/aws/aws-xray-sdk-python/pull/6>`_.
Expand Down
5 changes: 5 additions & 0 deletions 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,6 +141,10 @@ def put_annotation(self, key, value):
log.warning("ignoring unsupported annotation value type %s.", type(value))
return

if any(character not in _valid_annotation_key_characters for character in key):
log.warning("ignoring annnotation with unsupported characters in key: '%s'.", key)
return

self.annotations[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 should only use ASCII letters, numbers, and
the underscore(_) 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
6 changes: 4 additions & 2 deletions tests/test_trace_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def test_put_annotation():
'key2': 'value2',
}
# invalid annotation key-value pair should be dropped
segment.put_annotation('invalid', invalid)
segment.put_annotation('invalid_value', invalid)
segment.put_annotation('invalid-key', invalid)
segment.put_annotation('number', 1)

subsegment = Subsegment('sub', 'local', segment)
Expand All @@ -61,7 +62,8 @@ def test_put_annotation():

doc = entity_to_dict(segment)
assert doc['annotations']['number'] == 1
assert 'invalid' not in doc['annotations']
assert 'invalid-value' not in doc['annotations']
assert 'invalid-key' not in doc['annotations']

sub_doc = doc['subsegments'][0]
assert not sub_doc['annotations']['bool']
Expand Down