-
Notifications
You must be signed in to change notification settings - Fork 705
Add log-formatting to the otel-LogHandler #3673
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
Closed
Closed
Changes from 21 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
82993a3
changed the default otel-LogHandler to format the original logrecord …
a-recknagel b6ed8bf
added a straight forward way to exclude LogRecord attributes from bei…
a-recknagel de1d49b
respect semantic conventions for log record attributes
a-recknagel 222fb9b
merged recent main and resolved conflicts, and added changelog info
a-recknagel 772be51
merged current main and applied suggestion from review
a-recknagel b0a5a8d
added a comment exmplaining the reason behind log record attribute ex…
a-recknagel c9b48cf
exclude invalid attributes, which might also trigger endless logging …
a-recknagel a019772
Merge remote-tracking branch 'origin/main'
a-recknagel 943c9de
chore: Merge remote-tracking branch 'origin/main'
a-recknagel 169931b
fix: mollify linter
a-recknagel 426abef
Merge branch 'main' into main
srikanthccv 5b7b195
Merge branch 'main' into main
srikanthccv a3ec2f0
chore: Merge remote-tracking branch 'origin/main'
a-recknagel eaf328d
Merge remote-tracking branch 'origin/main'
a-recknagel 0552590
fix: roll back changes to the dev-setup that somehow only worked this…
a-recknagel 79ce99a
fix: re-enabled skipping over native logrecord attributes again that …
a-recknagel 6ad9d1a
Merge branch 'main' into main
a-recknagel 8ecd6a7
chore: merge current main
a-recknagel 6ed248f
Merge remote-tracking branch 'origin/main'
a-recknagel 3180964
Merge branch 'main' into main
a-recknagel bc2f8a2
Merge branch 'main' into main
a-recknagel 2126fe8
chore: Merged current main
a-recknagel 0b8c9c5
Merge remote-tracking branch 'origin/main'
a-recknagel cbc0920
fix: add tests for percent-style logging calls to ensure they're stil…
a-recknagel b9f0de9
fix: remove attribute extension to keep this PR on the enabling of fo…
a-recknagel aace0cb
fix: add one more test to ensure what default formatting looks like
a-recknagel 4a2a984
chore: ran black
a-recknagel d731803
fix: added checking the actual log message to percent-format test
a-recknagel 484615d
chore: Merge remote-tracking branch 'upstream/main'
a-recknagel 23eb08b
fix: fixed unit test to use the context-manager setup for the logging…
a-recknagel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,11 @@ | |
from opentelemetry.trace import INVALID_SPAN_CONTEXT | ||
|
||
|
||
def get_logger(level=logging.NOTSET, logger_provider=None): | ||
def get_logger(level=logging.NOTSET, logger_provider=None, formatter=None): | ||
logger = logging.getLogger(__name__) | ||
handler = LoggingHandler(level=level, logger_provider=logger_provider) | ||
if formatter: | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
return logger | ||
|
||
|
@@ -139,8 +141,10 @@ def test_log_record_user_attributes(self): | |
log_record = args[0] | ||
|
||
self.assertIsNotNone(log_record) | ||
self.assertEqual(len(log_record.attributes), 4) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why this was removed. Attributes should stay the same even when formatted. |
||
self.assertEqual(log_record.attributes["http.status_code"], 200) | ||
self.assertEqual( | ||
log_record.attributes, | ||
{**log_record.attributes, **{"http.status_code": 200}}, | ||
a-recknagel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
self.assertTrue( | ||
log_record.attributes[SpanAttributes.CODE_FILEPATH].endswith( | ||
"test_handler.py" | ||
|
@@ -171,7 +175,7 @@ def test_log_record_exception(self): | |
log_record = args[0] | ||
|
||
self.assertIsNotNone(log_record) | ||
self.assertEqual(log_record.body, "Zero Division Error") | ||
self.assertIn("Zero Division Error", log_record.body) | ||
a-recknagel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual( | ||
log_record.attributes[SpanAttributes.EXCEPTION_TYPE], | ||
ZeroDivisionError.__name__, | ||
|
@@ -235,3 +239,59 @@ def test_log_record_trace_correlation(self): | |
self.assertEqual(log_record.trace_id, span_context.trace_id) | ||
self.assertEqual(log_record.span_id, span_context.span_id) | ||
self.assertEqual(log_record.trace_flags, span_context.trace_flags) | ||
|
||
def test_log_record_args_are_translated(self): | ||
emitter_provider_mock = Mock(spec=LoggerProvider) | ||
a-recknagel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
emitter_mock = APIGetLogger( | ||
__name__, logger_provider=emitter_provider_mock | ||
) | ||
logger = get_logger(logger_provider=emitter_provider_mock) | ||
|
||
with self.assertLogs(level=logging.INFO): | ||
logger.info("Test message") | ||
args, _ = emitter_mock.emit.call_args_list[0] | ||
log_record = args[0] | ||
|
||
self.assertEqual( | ||
set(log_record.attributes), | ||
{ | ||
"thread.id", | ||
"code.filepath", | ||
"code.lineno", | ||
"code.function", | ||
"thread.name", | ||
}, | ||
) | ||
|
||
def test_format_is_called(self): | ||
emitter_provider_mock = Mock(spec=LoggerProvider) | ||
emitter_mock = APIGetLogger( | ||
__name__, logger_provider=emitter_provider_mock | ||
) | ||
formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s") | ||
logger = get_logger( | ||
logger_provider=emitter_provider_mock, formatter=formatter | ||
) | ||
|
||
with self.assertLogs(level=logging.INFO): | ||
logger.info("Test message") | ||
args, _ = emitter_mock.emit.call_args_list[0] | ||
log_record = args[0] | ||
|
||
self.assertEqual( | ||
log_record.body, "tests.logs.test_handler - INFO - Test message" | ||
) | ||
|
||
a-recknagel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_log_body_is_always_string(self): | ||
emitter_provider_mock = Mock(spec=LoggerProvider) | ||
emitter_mock = APIGetLogger( | ||
__name__, logger_provider=emitter_provider_mock | ||
) | ||
logger = get_logger(logger_provider=emitter_provider_mock) | ||
|
||
with self.assertLogs(level=logging.INFO): | ||
logger.info(["something", "of", "note"]) | ||
args, _ = emitter_mock.emit.call_args_list[0] | ||
log_record = args[0] | ||
|
||
self.assertIsInstance(log_record.body, str) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.