Skip to content

Removing Segment/Subsegment name invalid characters #9

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 1 commit into from
Dec 4, 2017
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
9 changes: 9 additions & 0 deletions aws_xray_sdk/core/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import binascii
import time
import string

import jsonpickle

Expand All @@ -13,20 +14,28 @@

log = logging.getLogger(__name__)

# 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 + '_.:/%&#=+\-@ '


class Entity(object):
"""
The parent class for segment/subsegment. It holds common properties
and methods on segment and subsegment.
"""

def __init__(self, name):

# required attributes
self.id = self._generate_random_id()
self.name = name
self.name = ''.join([c for c in name if c in _valid_name_characters])
self.start_time = time.time()
self.parent_id = None

if self.name != name:
log.warning("Removing Segment/Subsugment Name invalid characters.")

# sampling
self.sampled = True

Expand Down
8 changes: 8 additions & 0 deletions tests/test_dummy_entites.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ def test_structure_intact():
subsegment.close()
segment.close()
assert segment.ready_to_send()


def test_invalid_entity_name():
segment = DummySegment('DummySegment() Test?')
subsegment = DummySubsegment(segment, 'Dummy*Sub!segment$')

assert segment.name == 'DummySegment Test'
assert subsegment.name == 'DummySubsegment'