Skip to content

Commit f8b65f1

Browse files
tay-birdhaotianw465
authored andcommitted
Filter invalid characters from annotation key names; raise warning if found (#22)
* Filter invalid characters from annotation key names; raise a warning if any are found * Adjust wording in docs, as per RFC2119. * Remove string concatenation when checking validity of annotation key names and drop annotation if invalid * Update changelog for this PR * Add test to ensure annotations with invalid characters in their key are dropped
1 parent d110386 commit f8b65f1

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
CHANGELOG
33
=========
44

5+
Unreleased
6+
==========
7+
* bugfix: Fixed an issue that caused annotation keys with invalid characters to be silently ignored.
8+
59
0.95
610
====
711
* **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>`_.

aws_xray_sdk/core/models/entity.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

2021

2122
class Entity(object):
@@ -140,6 +141,10 @@ def put_annotation(self, key, value):
140141
log.warning("ignoring unsupported annotation value type %s.", type(value))
141142
return
142143

144+
if any(character not in _valid_annotation_key_characters for character in key):
145+
log.warning("ignoring annnotation with unsupported characters in key: '%s'.", key)
146+
return
147+
143148
self.annotations[key] = value
144149

145150
def put_metadata(self, key, value, namespace='default'):

docs/basic.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ You can add annotations and metadata to an active segment/subsegment.
5151
Annotations are simple key-value pairs that are indexed for use with
5252
`filter expressions <http://docs.aws.amazon.com/xray/latest/devguide/xray-console-filters.html>`_.
5353
Use annotations to record data that you want to use to group traces in the console,
54-
or when calling the GetTraceSummaries API.
54+
or when calling the GetTraceSummaries API. Annotation keys should only use ASCII letters, numbers, and
55+
the underscore(_) character.
5556

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

tests/test_trace_entities.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def test_put_annotation():
5252
'key2': 'value2',
5353
}
5454
# invalid annotation key-value pair should be dropped
55-
segment.put_annotation('invalid', invalid)
55+
segment.put_annotation('invalid_value', invalid)
56+
segment.put_annotation('invalid-key', invalid)
5657
segment.put_annotation('number', 1)
5758

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

6263
doc = entity_to_dict(segment)
6364
assert doc['annotations']['number'] == 1
64-
assert 'invalid' not in doc['annotations']
65+
assert 'invalid-value' not in doc['annotations']
66+
assert 'invalid-key' not in doc['annotations']
6567

6668
sub_doc = doc['subsegments'][0]
6769
assert not sub_doc['annotations']['bool']

0 commit comments

Comments
 (0)