Skip to content

Commit e7711fe

Browse files
lupengamznsrprash
andauthored
Fixed serialize exception (#284)
Co-authored-by: Prashant Srivastava <[email protected]>
1 parent 7cc609f commit e7711fe

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

aws_xray_sdk/core/models/entity.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,16 @@ def to_dict(self):
280280
subsegments.append(subsegment.to_dict())
281281
entity_dict[key] = subsegments
282282
elif key == 'cause':
283-
entity_dict[key] = {}
284-
entity_dict[key]['working_directory'] = self.cause['working_directory']
285-
# exceptions are stored as List
286-
throwables = []
287-
for throwable in value['exceptions']:
288-
throwables.append(throwable.to_dict())
289-
entity_dict[key]['exceptions'] = throwables
283+
if isinstance(self.cause, dict):
284+
entity_dict[key] = {}
285+
entity_dict[key]['working_directory'] = self.cause['working_directory']
286+
# exceptions are stored as List
287+
throwables = []
288+
for throwable in value['exceptions']:
289+
throwables.append(throwable.to_dict())
290+
entity_dict[key]['exceptions'] = throwables
291+
else:
292+
entity_dict[key] = self.cause
290293
elif key == 'metadata':
291294
entity_dict[key] = metadata_to_dict(value)
292295
elif key != 'sampled' and key != ORIGIN_TRACE_HEADER_ATTR_KEY:

tests/test_serialize_entities.py

+37-12
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class TestException(Exception):
260260
def __init__(self, message):
261261
super(TestException, self).__init__(message)
262262

263-
segment = Segment('test')
263+
segment_one = Segment('test')
264264

265265
stack_one = [
266266
('/path/to/test.py', 10, 'module', 'another_function()'),
@@ -275,18 +275,18 @@ def __init__(self, message):
275275
exception_one = TestException('test message one')
276276
exception_two = TestException('test message two')
277277

278-
segment.add_exception(exception_one, stack_one, True)
279-
segment.add_exception(exception_two, stack_two, False)
278+
segment_one.add_exception(exception_one, stack_one, True)
279+
segment_one.add_exception(exception_two, stack_two, False)
280280

281-
segment.close()
281+
segment_one.close()
282282

283-
expected_segment_dict = {
284-
"id": segment.id,
283+
expected_segment_one_dict = {
284+
"id": segment_one.id,
285285
"name": "test",
286-
"start_time": segment.start_time,
286+
"start_time": segment_one.start_time,
287287
"in_progress": False,
288288
"cause": {
289-
"working_directory": segment.cause['working_directory'],
289+
"working_directory": segment_one.cause['working_directory'],
290290
"exceptions": [
291291
{
292292
"id": exception_one._cause_id,
@@ -326,14 +326,39 @@ def __init__(self, message):
326326
}
327327
]
328328
},
329-
"trace_id": segment.trace_id,
329+
"trace_id": segment_one.trace_id,
330330
"fault": True,
331-
"end_time": segment.end_time
331+
"end_time": segment_one.end_time
332332
}
333333

334-
actual_segment_dict = entity_to_dict(segment)
334+
segment_two = Segment('test')
335+
subsegment = Subsegment('test', 'local', segment_two)
336+
337+
subsegment.add_exception(exception_one, stack_one, True)
338+
subsegment.add_exception(exception_two, stack_two, False)
339+
subsegment.close()
335340

336-
assert expected_segment_dict == actual_segment_dict
341+
# will record cause id instead as same exception already recorded in its subsegment
342+
segment_two.add_exception(exception_one, stack_one, True)
343+
344+
segment_two.close()
345+
346+
expected_segment_two_dict = {
347+
"id": segment_two.id,
348+
"name": "test",
349+
"start_time": segment_two.start_time,
350+
"in_progress": False,
351+
"cause": exception_one._cause_id,
352+
"trace_id": segment_two.trace_id,
353+
"fault": True,
354+
"end_time": segment_two.end_time
355+
}
356+
357+
actual_segment_one_dict = entity_to_dict(segment_one)
358+
actual_segment_two_dict = entity_to_dict(segment_two)
359+
360+
assert expected_segment_one_dict == actual_segment_one_dict
361+
assert expected_segment_two_dict == actual_segment_two_dict
337362

338363
def test_serialize_subsegment():
339364

0 commit comments

Comments
 (0)