-
Notifications
You must be signed in to change notification settings - Fork 704
Record logger name as the instrumentation scope name #4208
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
Merged
lzchen
merged 17 commits into
open-telemetry:main
from
sfc-gh-jopel:fix-issue-2485-instrumentation-scope-name-2
Oct 15, 2024
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1d42539
Fix issue 2485 enable caching for get_logger calls
tm0nk 8259ec5
Add entry to CHANGELOG.md
tm0nk 18eaa37
Add logger caching by name, version, schema_url
sfc-gh-jopel 386835f
Update CHANGELOG.md
sfc-gh-jopel 0ba8cb4
Merge branch 'main' into fix-issue-2485-instrumentation-scope-name-2
emdneto 95a162b
Update benchmark
sfc-gh-jopel c072fe3
Attributes make logger unique
sfc-gh-jopel 8357f41
Do not use lru_cache on method
sfc-gh-jopel a6c8ed1
Merge branch 'main' into fix-issue-2485-instrumentation-scope-name-2
emdneto f055bfb
Fix linter for test
sfc-gh-jopel 5a155ab
fix nit
sfc-gh-jopel 1e6d179
Merge branch 'main' into fix-issue-2485-instrumentation-scope-name-2
emdneto b24fcc9
Merge branch 'main' into fix-issue-2485-instrumentation-scope-name-2
sfc-gh-jopel 2b7835c
add no cache get_logger test
sfc-gh-jopel b7aaad3
Merge branch 'main' into fix-issue-2485-instrumentation-scope-name-2
xrmx 5a5538c
Merge branch 'main' into fix-issue-2485-instrumentation-scope-name-2
sfc-gh-jopel 26090ea
catch logs in test
sfc-gh-jopel 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
38 changes: 38 additions & 0 deletions
38
opentelemetry-sdk/benchmarks/logs/test_benchmark_logging_handler.py
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
|
||
import pytest | ||
|
||
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler | ||
from opentelemetry.sdk._logs.export import ( | ||
InMemoryLogExporter, | ||
SimpleLogRecordProcessor, | ||
) | ||
|
||
|
||
def _set_up_logging_handler(level): | ||
logger_provider = LoggerProvider() | ||
exporter = InMemoryLogExporter() | ||
processor = SimpleLogRecordProcessor(exporter=exporter) | ||
logger_provider.add_log_record_processor(processor) | ||
handler = LoggingHandler(level=level, logger_provider=logger_provider) | ||
return handler | ||
|
||
|
||
def _create_logger(handler, name): | ||
logger = logging.getLogger(name) | ||
logger.addHandler(handler) | ||
return logger | ||
|
||
|
||
@pytest.mark.parametrize("num_loggers", [1, 10, 100, 1000]) | ||
def test_simple_get_logger_different_names(benchmark, num_loggers): | ||
handler = _set_up_logging_handler(level=logging.DEBUG) | ||
loggers = [ | ||
_create_logger(handler, str(f"logger_{i}")) for i in range(num_loggers) | ||
] | ||
|
||
def benchmark_get_logger(): | ||
for index in range(1000): | ||
loggers[index % num_loggers].warning("test message") | ||
|
||
benchmark(benchmark_get_logger) |
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
69 changes: 69 additions & 0 deletions
69
opentelemetry-sdk/tests/logs/test_logger_provider_cache.py
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import logging | ||
import unittest | ||
|
||
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler | ||
from opentelemetry.sdk._logs.export import ( | ||
InMemoryLogExporter, | ||
SimpleLogRecordProcessor, | ||
) | ||
|
||
|
||
def set_up_logging_handler(level): | ||
logger_provider = LoggerProvider() | ||
exporter = InMemoryLogExporter() | ||
processor = SimpleLogRecordProcessor(exporter=exporter) | ||
logger_provider.add_log_record_processor(processor) | ||
handler = LoggingHandler(level=level, logger_provider=logger_provider) | ||
return handler, logger_provider | ||
|
||
|
||
def create_logger(handler, name): | ||
logger = logging.getLogger(name) | ||
logger.addHandler(handler) | ||
return logger | ||
|
||
|
||
class TestLoggerProviderCache(unittest.TestCase): | ||
|
||
def test_get_logger_single_handler(self): | ||
handler, logger_provider = set_up_logging_handler(level=logging.DEBUG) | ||
# pylint: disable=protected-access | ||
logger_cache = logger_provider._logger_cache | ||
logger = create_logger(handler, "test_logger") | ||
|
||
# Ensure logger is lazily cached | ||
self.assertEqual(0, len(logger_cache)) | ||
|
||
logger.warning("test message") | ||
|
||
self.assertEqual(1, len(logger_cache)) | ||
|
||
# Ensure only one logger is cached | ||
rounds = 100 | ||
for _ in range(rounds): | ||
sfc-gh-jopel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.warning("test message") | ||
|
||
self.assertEqual(1, len(logger_cache)) | ||
|
||
def test_get_logger_multiple_loggers(self): | ||
handler, logger_provider = set_up_logging_handler(level=logging.DEBUG) | ||
# pylint: disable=protected-access | ||
logger_cache = logger_provider._logger_cache | ||
|
||
num_loggers = 10 | ||
loggers = [create_logger(handler, str(i)) for i in range(num_loggers)] | ||
|
||
# Ensure loggers are lazily cached | ||
self.assertEqual(0, len(logger_cache)) | ||
|
||
for logger in loggers: | ||
logger.warning("test message") | ||
|
||
self.assertEqual(num_loggers, len(logger_cache)) | ||
|
||
rounds = 100 | ||
for _ in range(rounds): | ||
sfc-gh-jopel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for logger in loggers: | ||
logger.warning("test message") | ||
|
||
self.assertEqual(num_loggers, len(logger_cache)) |
Oops, something went wrong.
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.