forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Record logger name as the instrumentation scope name (open-telemetry#…
- Loading branch information
1 parent
d1904b9
commit d5c54e3
Showing
5 changed files
with
231 additions
and
17 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
38 changes: 38 additions & 0 deletions
38
opentelemetry-sdk/benchmarks/logs/test_benchmark_logging_handler.py
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]) | ||
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 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
111 changes: 111 additions & 0 deletions
111
opentelemetry-sdk/tests/logs/test_logger_provider_cache.py
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,111 @@ | ||
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)) | ||
|
||
with self.assertLogs(level=logging.WARNING): | ||
logger.warning("test message") | ||
|
||
self.assertEqual(1, len(logger_cache)) | ||
|
||
# Ensure only one logger is cached | ||
with self.assertLogs(level=logging.WARNING): | ||
rounds = 100 | ||
for _ in range(rounds): | ||
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)) | ||
|
||
with self.assertLogs(level=logging.WARNING): | ||
for logger in loggers: | ||
logger.warning("test message") | ||
|
||
self.assertEqual(num_loggers, len(logger_cache)) | ||
|
||
with self.assertLogs(level=logging.WARNING): | ||
rounds = 100 | ||
for _ in range(rounds): | ||
for logger in loggers: | ||
logger.warning("test message") | ||
|
||
self.assertEqual(num_loggers, len(logger_cache)) | ||
|
||
def test_provider_get_logger_no_cache(self): | ||
_, logger_provider = set_up_logging_handler(level=logging.DEBUG) | ||
# pylint: disable=protected-access | ||
logger_cache = logger_provider._logger_cache | ||
|
||
logger_provider.get_logger( | ||
name="test_logger", | ||
version="version", | ||
schema_url="schema_url", | ||
attributes={"key": "value"}, | ||
) | ||
|
||
# Ensure logger is not cached if attributes is set | ||
self.assertEqual(0, len(logger_cache)) | ||
|
||
def test_provider_get_logger_cached(self): | ||
_, logger_provider = set_up_logging_handler(level=logging.DEBUG) | ||
# pylint: disable=protected-access | ||
logger_cache = logger_provider._logger_cache | ||
|
||
logger_provider.get_logger( | ||
name="test_logger", | ||
version="version", | ||
schema_url="schema_url", | ||
) | ||
|
||
# Ensure only one logger is cached | ||
self.assertEqual(1, len(logger_cache)) | ||
|
||
logger_provider.get_logger( | ||
name="test_logger", | ||
version="version", | ||
schema_url="schema_url", | ||
) | ||
|
||
# Ensure only one logger is cached | ||
self.assertEqual(1, len(logger_cache)) |