def test_structure_intact(): segment = FacadeSegment('name', 'id', 'id', True) subsegment = Subsegment('name', 'local', segment) subsegment2 = Subsegment('name', 'local', segment) segment.add_subsegment(subsegment) subsegment.add_subsegment(subsegment2) assert segment.subsegments[0] is subsegment assert subsegment.subsegments[0] is subsegment2
def test_unicode_entity_name(): name1 = u'福' name2 = u'セツナ' segment = Segment(name1) subsegment = Subsegment(name2, 'local', segment) assert segment.name == name1 assert subsegment.name == name2
def test_put_metadata(): segment = Segment('seg') meta = { 'key1': 'value1', 'key2': 'value2', } segment.put_metadata('key', meta) subsegment = Subsegment('sub', 'local', segment) segment.add_subsegment(subsegment) subsegment.put_metadata('key', meta, 'my namespace') doc = entity_to_dict(segment) assert doc['metadata']['default']['key'] == meta sub_doc = doc['subsegments'][0] assert sub_doc['metadata']['my namespace']['key'] == meta
def test_context(self): store = aws.XrayContext() self.assertEqual(store.handle_context_missing(), None) x = Segment('foo') y = Segment('foo') a = Subsegment('bar', 'boo', x) b = Subsegment('bar', 'boo', x) b.thread_id = '123' store.put_segment(x) store.put_subsegment(a) store.put_subsegment(b) self.assertEqual(store._local.entities, [x, a, b]) self.assertEqual(store.get_trace_entity(), a) store.end_subsegment(a) self.assertEqual(store.get_trace_entity(), x) store.put_segment(y) self.assertEqual(store._local.entities, [y]) self.assertEqual(store.get_trace_entity(), y) self.assertFalse(store.end_subsegment(42))
def test_put_subsegment(): segment = context.get_trace_entity() subsegment = Subsegment('name', 'local', segment) context.put_subsegment(subsegment) assert context.get_trace_entity().id == subsegment.id subsegment2 = Subsegment('name', 'local', segment) context.put_subsegment(subsegment2) assert context.get_trace_entity().id == subsegment2.id assert subsegment.subsegments[0] is subsegment2 assert subsegment2.parent_id == subsegment.id assert subsegment.parent_id == segment.id assert subsegment2.parent_segment is segment context.end_subsegment() assert context.get_trace_entity().id == subsegment.id context.end_subsegment() assert context.get_trace_entity().id == segment.id
def test_context_worker_thread_main_acquire(self): store = aws.XrayContext() x = Segment('foo') a = Subsegment('bar', 'boo', x) store.put_segment(x) store.put_subsegment(a) def get_ident(): return 42 self.patch(threading, 'get_ident', get_ident) self.assertEqual(store.get_trace_entity(), a)
def test_put_annotation(): segment = Segment('seg') invalid = { 'key1': 'value1', 'key2': 'value2', } # invalid annotation key-value pair should be dropped segment.put_annotation('invalid', invalid) segment.put_annotation('number', 1) subsegment = Subsegment('sub', 'local', segment) segment.add_subsegment(subsegment) subsegment.put_annotation('bool', False) doc = entity_to_dict(segment) assert doc['annotations']['number'] == 1 assert 'invalid' not in doc['annotations'] sub_doc = doc['subsegments'][0] assert not sub_doc['annotations']['bool']
def test_serialize_subsegment(): segment = Segment('test') subsegment = Subsegment('test', 'local', segment) subsegment.close() segment.close() expected_subsegment_dict = { "id": subsegment.id, "name": "test", "start_time": subsegment.start_time, "in_progress": False, "trace_id": subsegment.trace_id, "type": "subsegment", "namespace": "local", "end_time": subsegment.end_time } actual_subsegment_dict = entity_to_dict(subsegment) assert expected_subsegment_dict == actual_subsegment_dict
def test_non_initialized(): # Context that hasn't been initialized by lambda container should not add subsegments to the facade segment. temp_header_var = os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] del os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] temp_context = lambda_launcher.LambdaContext() facade_segment = temp_context.get_trace_entity() subsegment = Subsegment("TestSubsegment", "local", facade_segment) temp_context.put_subsegment(subsegment) assert temp_context.get_trace_entity() == facade_segment # "Lambda" container added metadata now. Should see subsegment now. os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = temp_header_var temp_context.put_subsegment(subsegment) assert temp_context.get_trace_entity() == subsegment
def test_add_exception_cause_resetting(): segment = Segment('seg') subseg = Subsegment('subseg', 'remote', segment) exception = Exception("testException") stack = [['path', 'line', 'label']] subseg.add_exception(exception=exception, stack=stack) segment.add_exception(exception=exception, stack=stack) segment.add_exception(exception=Exception("newException"), stack=stack) subseg.close() segment.close() seg_cause = segment.cause assert isinstance(seg_cause, dict) assert 'newException' == seg_cause['exceptions'][0].message
def test_serialize_subsegment_with_aws(): segment = Segment('test') subsegment = Subsegment('test', 'aws', segment) aws = { "bucket_name": "testbucket", "region": "us-east-1", "operation": "GetObject", "request_id": "0000000000000000", "key": "123", "resource_names": [ "testbucket" ] } subsegment.set_aws(aws) subsegment.close() segment.close() expected_subsegment_dict = { "id": subsegment.id, "name": "test", "start_time": subsegment.start_time, "in_progress": False, "aws": { "bucket_name": "testbucket", "region": "us-east-1", "operation": "GetObject", "request_id": "0000000000000000", "key": "123", "resource_names": [ "testbucket" ] }, "trace_id": subsegment.trace_id, "type": "subsegment", "namespace": "aws", "end_time": subsegment.end_time } actual_subsegment_dict = entity_to_dict(subsegment) assert expected_subsegment_dict == actual_subsegment_dict
def test_add_exception_referencing(): segment = Segment('seg') subseg = Subsegment('subseg', 'remote', segment) exception = Exception("testException") stack = [['path', 'line', 'label']] subseg.add_exception(exception=exception, stack=stack) segment.add_exception(exception=exception, stack=stack) subseg.close() segment.close() seg_cause = segment.cause subseg_cause = subseg.cause assert isinstance(subseg_cause, dict) if sys.version_info.major == 2: assert isinstance(seg_cause, basestring) else: assert isinstance(seg_cause, str) assert seg_cause == subseg_cause['exceptions'][0].id
def test_serialize_subsegment_with_sql(): segment = Segment('test') subsegment = Subsegment('test', 'remote', segment) sql = { "url": "jdbc:postgresql://aawijb5u25wdoy.cpamxznpdoq8.us-west-2.rds.amazonaws.com:5432/ebdb", "preparation": "statement", "database_type": "PostgreSQL", "database_version": "9.5.4", "driver_version": "PostgreSQL 9.4.1211.jre7", "user" : "dbuser", "sanitized_query" : "SELECT * FROM customers WHERE customer_id=?;" } subsegment.set_sql(sql) subsegment.close() segment.close() expected_subsegment_dict = { "id": subsegment.id, "name": "test", "start_time": subsegment.start_time, "in_progress": False, "trace_id": subsegment.trace_id, "type": "subsegment", "namespace": "remote", "sql": { "url": "jdbc:postgresql://aawijb5u25wdoy.cpamxznpdoq8.us-west-2.rds.amazonaws.com:5432/ebdb", "preparation": "statement", "database_type": "PostgreSQL", "database_version": "9.5.4", "driver_version": "PostgreSQL 9.4.1211.jre7", "user": "******", "sanitized_query": "SELECT * FROM customers WHERE customer_id=?;" }, "end_time": subsegment.end_time } actual_subsegment_dict = entity_to_dict(subsegment) assert expected_subsegment_dict == actual_subsegment_dict
def test_missing_parent_segment(): with pytest.raises(SegmentNotFoundException): Subsegment('name', 'local', None)
def test_serialize_subsegment_with_http(): segment = Segment('test') subsegment = Subsegment('test', 'remote', segment) subsegment.put_http_meta(http.URL, 'https://aws.amazon.com') subsegment.put_http_meta(http.METHOD, 'get') subsegment.put_http_meta(http.STATUS, 200) subsegment.put_http_meta(http.CONTENT_LENGTH, 0) subsegment.close() segment.close() expected_subsegment_dict = { "id": subsegment.id, "name": "test", "start_time": subsegment.start_time, "in_progress": False, "http": { "request": { "url": "https://aws.amazon.com", "method": "get" }, "response": { "status": 200, "content_length": 0 } }, "trace_id": subsegment.trace_id, "type": "subsegment", "namespace": "remote", "end_time": subsegment.end_time } actual_subsegment_dict = entity_to_dict(subsegment) assert expected_subsegment_dict == actual_subsegment_dict
def test_reference_counting(): segment = Segment('seg') subsegment = Subsegment('sub', 'local', segment) segment.add_subsegment(subsegment) subsegment = Subsegment('sub', 'local', segment) subsubsegment = Subsegment('subsub', 'local', segment) subsegment.add_subsegment(subsubsegment) assert not segment.ready_to_send() assert segment.ref_counter.get_current() == 2 subsubsegment.close() assert not segment.ready_to_send() assert segment.ref_counter.get_current() == 1 subsegment.close() assert not segment.ready_to_send() assert segment.ref_counter.get_current() == 0 segment.close() assert segment.ready_to_send() assert segment.get_total_subsegments_size() == 2
def test_serialize_segment_with_exception(): class TestException(Exception): def __init__(self, message): super(TestException, self).__init__(message) segment_one = Segment('test') stack_one = [ ('/path/to/test.py', 10, 'module', 'another_function()'), ('/path/to/test.py', 3, 'another_function', 'wrong syntax') ] stack_two = [ ('/path/to/test.py', 11, 'module', 'another_function()'), ('/path/to/test.py', 4, 'another_function', 'wrong syntax') ] exception_one = TestException('test message one') exception_two = TestException('test message two') segment_one.add_exception(exception_one, stack_one, True) segment_one.add_exception(exception_two, stack_two, False) segment_one.close() expected_segment_one_dict = { "id": segment_one.id, "name": "test", "start_time": segment_one.start_time, "in_progress": False, "cause": { "working_directory": segment_one.cause['working_directory'], "exceptions": [ { "id": exception_one._cause_id, "message": "test message one", "type": "TestException", "remote": True, "stack": [ { "path": "test.py", "line": 10, "label": "module" }, { "path": "test.py", "line": 3, "label": "another_function" } ] }, { "id": exception_two._cause_id, "message": "test message two", "type": "TestException", "remote": False, "stack": [ { "path": "test.py", "line": 11, "label": "module" }, { "path": "test.py", "line": 4, "label": "another_function" } ] } ] }, "trace_id": segment_one.trace_id, "fault": True, "end_time": segment_one.end_time } segment_two = Segment('test') subsegment = Subsegment('test', 'local', segment_two) subsegment.add_exception(exception_one, stack_one, True) subsegment.add_exception(exception_two, stack_two, False) subsegment.close() # will record cause id instead as same exception already recorded in its subsegment segment_two.add_exception(exception_one, stack_one, True) segment_two.close() expected_segment_two_dict = { "id": segment_two.id, "name": "test", "start_time": segment_two.start_time, "in_progress": False, "cause": exception_one._cause_id, "trace_id": segment_two.trace_id, "fault": True, "end_time": segment_two.end_time } actual_segment_one_dict = entity_to_dict(segment_one) actual_segment_two_dict = entity_to_dict(segment_two) assert expected_segment_one_dict == actual_segment_one_dict assert expected_segment_two_dict == actual_segment_two_dict