Skip to content
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

Fix #2689 LoggingHandler to handle exc_info=False #2690

Merged
merged 2 commits into from
May 16, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.11.1-0.30b1...HEAD)

- Fix LoggingHandler to handle LogRecord with exc_info=False
([#2690](https://github.com/open-telemetry/opentelemetry-python/pull/2690))
- Make metrics components public
([#2684](https://github.com/open-telemetry/opentelemetry-python/pull/2684))
- Update to semantic conventions v1.11.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def _get_attributes(record: logging.LogRecord) -> Attributes:
attributes = {
k: v for k, v in vars(record).items() if k not in _RESERVED_ATTRS
}
if record.exc_info is not None:
if record.exc_info:
exc_type = ""
message = ""
stack_trace = ""
Expand Down
21 changes: 21 additions & 0 deletions opentelemetry-sdk/tests/logs/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ def test_log_record_exception(self):
self.assertTrue("division by zero" in stack_trace)
self.assertTrue(__file__ in stack_trace)

def test_log_exc_info_false(self):
"""Exception information will be included in attributes"""
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(log_emitter=emitter_mock)
try:
raise ZeroDivisionError("division by zero")
except ZeroDivisionError:
logger.error("Zero Division Error", exc_info=False)
args, _ = emitter_mock.emit.call_args_list[0]
log_record = args[0]

self.assertIsNotNone(log_record)
self.assertEqual(log_record.body, "Zero Division Error")
self.assertNotIn(SpanAttributes.EXCEPTION_TYPE, log_record.attributes)
self.assertNotIn(
SpanAttributes.EXCEPTION_MESSAGE, log_record.attributes
)
self.assertNotIn(
SpanAttributes.EXCEPTION_STACKTRACE, log_record.attributes
)

def test_log_record_trace_correlation(self):
emitter_mock = Mock(spec=LogEmitter)
logger = get_logger(log_emitter=emitter_mock)
Expand Down