Skip to content

Commit c967cef

Browse files
☔ Assert exception properties
1 parent b26ba45 commit c967cef

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

opentelemetry-sdk/tests/logs/test_handler.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import logging
1616
import os
1717
import sys
18+
import traceback
1819
import unittest
1920
from unittest.mock import Mock, patch
2021

@@ -51,18 +52,33 @@ def test_handler_default_log_level(self):
5152

5253
def test_handler_error_exc_info(self):
5354
processor, logger = set_up_test_logging(logging.NOTSET)
55+
56+
class CustomException(Exception):
57+
pass
58+
59+
try:
60+
raise CustomException("Custom exception")
61+
except CustomException as exception:
62+
exc_info = (type(exception), exception, exception.__traceback__)
63+
traceback_str = "".join(traceback.format_exception(*exc_info))
64+
5465
exc_info_values = [
5566
# Don't know what caused it in my context, so I'm relying on mocks to replicate the behavior.
5667
# First the `record.exc_info` becomes a string somehow, then `sys.exc_info` brings the tuple.
5768
"Stringified exception",
58-
(None, None, None),
69+
exc_info,
5970
]
6071

6172
with patch.object(sys, "exc_info", side_effect=exc_info_values):
6273
logger.exception("Exception message") # Should not raise exception
6374

6475
assert processor.emit_count() == 1
6576

77+
attributes = processor.log_data_emitted[0].log_record.attributes._dict
78+
assert attributes["exception.type"] == "CustomException"
79+
assert attributes["exception.message"] == str(exc_info[1])
80+
assert attributes["exception.stacktrace"] == traceback_str
81+
6682
def test_handler_custom_log_level(self):
6783
processor, logger = set_up_test_logging(logging.ERROR)
6884

0 commit comments

Comments
 (0)