Skip to content

Commit 24ca960

Browse files
levesquejfhaotianw465
authored andcommitted
Removing Segment/Subsegment name invalid characters (#9)
1 parent e8a1d4f commit 24ca960

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

aws_xray_sdk/core/models/entity.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import binascii
44
import time
5+
import string
56

67
import jsonpickle
78

@@ -13,20 +14,28 @@
1314

1415
log = logging.getLogger(__name__)
1516

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

1721
class Entity(object):
1822
"""
1923
The parent class for segment/subsegment. It holds common properties
2024
and methods on segment and subsegment.
2125
"""
26+
2227
def __init__(self, name):
2328

2429
# required attributes
2530
self.id = self._generate_random_id()
2631
self.name = name
32+
self.name = ''.join([c for c in name if c in _valid_name_characters])
2733
self.start_time = time.time()
2834
self.parent_id = None
2935

36+
if self.name != name:
37+
log.warning("Removing Segment/Subsugment Name invalid characters.")
38+
3039
# sampling
3140
self.sampled = True
3241

tests/test_dummy_entites.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,11 @@ def test_structure_intact():
5757
subsegment.close()
5858
segment.close()
5959
assert segment.ready_to_send()
60+
61+
62+
def test_invalid_entity_name():
63+
segment = DummySegment('DummySegment() Test?')
64+
subsegment = DummySubsegment(segment, 'Dummy*Sub!segment$')
65+
66+
assert segment.name == 'DummySegment Test'
67+
assert subsegment.name == 'DummySubsegment'

0 commit comments

Comments
 (0)