-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logger caching by name, version, schema_url
- Loading branch information
1 parent
8259ec5
commit 18eaa37
Showing
3 changed files
with
142 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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, 10000]) | ||
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 i in range(10000): | ||
loggers[i % num_loggers].warning("test message") | ||
|
||
benchmark(benchmark_get_logger) |
This file contains 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 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,74 @@ | ||
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 TestLogProviderCache(unittest.TestCase): | ||
|
||
def test_get_logger_single_handler(self): | ||
handler, logger_provider = set_up_logging_handler(level=logging.DEBUG) | ||
|
||
logger = create_logger(handler, "test_logger") | ||
|
||
logger.warning("test message") | ||
|
||
self.assertEqual(1, len(logger_provider._logger_cache)) | ||
self.assertTrue( | ||
("test_logger", "", None) in logger_provider._logger_cache | ||
) | ||
|
||
rounds = 100 | ||
for _ in range(rounds): | ||
logger.warning("test message") | ||
|
||
self.assertEqual(1, len(logger_provider._logger_cache)) | ||
self.assertTrue( | ||
("test_logger", "", None) in logger_provider._logger_cache | ||
) | ||
|
||
def test_get_logger_multiple_loggers(self): | ||
handler, logger_provider = set_up_logging_handler(level=logging.DEBUG) | ||
|
||
num_loggers = 10 | ||
loggers = [create_logger(handler, str(i)) for i in range(num_loggers)] | ||
|
||
for logger in loggers: | ||
logger.warning("test message") | ||
|
||
self.assertEqual(num_loggers, len(logger_provider._logger_cache)) | ||
print(logger_provider._logger_cache) | ||
for logger in loggers: | ||
self.assertTrue( | ||
(logger.name, "", None) in logger_provider._logger_cache | ||
) | ||
|
||
rounds = 100 | ||
for _ in range(rounds): | ||
for logger in loggers: | ||
logger.warning("test message") | ||
|
||
self.assertEqual(num_loggers, len(logger_provider._logger_cache)) | ||
for logger in loggers: | ||
self.assertTrue( | ||
(logger.name, "", None) in logger_provider._logger_cache | ||
) |