diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 55463e00be9..f3192e2a79b 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -151,8 +151,7 @@ def _init_logging( BatchLogProcessor(exporter_class(**exporter_args)) ) - log_emitter = provider.get_log_emitter(__name__) - handler = LoggingHandler(level=logging.NOTSET, log_emitter=log_emitter) + handler = LoggingHandler(level=logging.NOTSET, log_emitter_provider=provider) logging.getLogger().addHandler(handler) diff --git a/opentelemetry-sdk/tests/logs/test_export.py b/opentelemetry-sdk/tests/logs/test_export.py index d7dbed76a6d..40a81ef2236 100644 --- a/opentelemetry-sdk/tests/logs/test_export.py +++ b/opentelemetry-sdk/tests/logs/test_export.py @@ -48,12 +48,11 @@ class TestSimpleLogProcessor(unittest.TestCase): def test_simple_log_processor_default_level(self): exporter = InMemoryLogExporter() log_emitter_provider = LogEmitterProvider() - log_emitter = log_emitter_provider.get_log_emitter(__name__) log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter)) logger = logging.getLogger("default_level") - logger.addHandler(LoggingHandler(log_emitter=log_emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=log_emitter_provider)) logger.warning("Something is wrong") finished_logs = exporter.get_finished_logs() @@ -68,13 +67,12 @@ def test_simple_log_processor_default_level(self): def test_simple_log_processor_custom_level(self): exporter = InMemoryLogExporter() log_emitter_provider = LogEmitterProvider() - log_emitter = log_emitter_provider.get_log_emitter(__name__) log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter)) logger = logging.getLogger("custom_level") logger.setLevel(logging.ERROR) - logger.addHandler(LoggingHandler(log_emitter=log_emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=log_emitter_provider)) logger.warning("Warning message") logger.debug("Debug message") @@ -99,12 +97,11 @@ def test_simple_log_processor_custom_level(self): def test_simple_log_processor_trace_correlation(self): exporter = InMemoryLogExporter() log_emitter_provider = LogEmitterProvider() - log_emitter = log_emitter_provider.get_log_emitter("name", "version") log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter)) logger = logging.getLogger("trace_correlation") - logger.addHandler(LoggingHandler(log_emitter=log_emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=log_emitter_provider)) logger.warning("Warning message") finished_logs = exporter.get_finished_logs() @@ -137,12 +134,11 @@ def test_simple_log_processor_trace_correlation(self): def test_simple_log_processor_shutdown(self): exporter = InMemoryLogExporter() log_emitter_provider = LogEmitterProvider() - log_emitter = log_emitter_provider.get_log_emitter(__name__) log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter)) logger = logging.getLogger("shutdown") - logger.addHandler(LoggingHandler(log_emitter=log_emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=log_emitter_provider)) logger.warning("Something is wrong") finished_logs = exporter.get_finished_logs() @@ -167,9 +163,8 @@ def test_emit_call_log_record(self): provider = LogEmitterProvider() provider.add_log_processor(log_processor) - emitter = provider.get_log_emitter(__name__) logger = logging.getLogger("emit_call") - logger.addHandler(LoggingHandler(log_emitter=emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=provider)) logger.error("error") self.assertEqual(log_processor.emit.call_count, 1) @@ -181,9 +176,8 @@ def test_shutdown(self): provider = LogEmitterProvider() provider.add_log_processor(log_processor) - emitter = provider.get_log_emitter(__name__) logger = logging.getLogger("shutdown") - logger.addHandler(LoggingHandler(log_emitter=emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=provider)) logger.warning("warning message: %s", "possible upcoming heatwave") logger.error("Very high rise in temperatures across the globe") @@ -214,9 +208,8 @@ def test_force_flush(self): provider = LogEmitterProvider() provider.add_log_processor(log_processor) - emitter = provider.get_log_emitter(__name__) logger = logging.getLogger("force_flush") - logger.addHandler(LoggingHandler(log_emitter=emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=provider)) logger.critical("Earth is burning") log_processor.force_flush() @@ -233,9 +226,8 @@ def test_log_processor_too_many_logs(self): provider = LogEmitterProvider() provider.add_log_processor(log_processor) - emitter = provider.get_log_emitter(__name__) logger = logging.getLogger("many_logs") - logger.addHandler(LoggingHandler(log_emitter=emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=provider)) for log_no in range(1000): logger.critical("Log no: %s", log_no) @@ -251,9 +243,8 @@ def test_with_multiple_threads(self): provider = LogEmitterProvider() provider.add_log_processor(log_processor) - emitter = provider.get_log_emitter(__name__) logger = logging.getLogger("threads") - logger.addHandler(LoggingHandler(log_emitter=emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=provider)) def bulk_log_and_flush(num_logs): for _ in range(num_logs): @@ -286,9 +277,8 @@ def test_batch_log_processor_fork(self): provider = LogEmitterProvider() provider.add_log_processor(log_processor) - emitter = provider.get_log_emitter(__name__) logger = logging.getLogger("test-fork") - logger.addHandler(LoggingHandler(log_emitter=emitter)) + logger.addHandler(LoggingHandler(log_emitter_provider=provider)) logger.critical("yolo") time.sleep(0.5) # give some time for the exporter to upload diff --git a/opentelemetry-sdk/tests/logs/test_handler.py b/opentelemetry-sdk/tests/logs/test_handler.py index a5c8c85643f..f1e90141bcb 100644 --- a/opentelemetry-sdk/tests/logs/test_handler.py +++ b/opentelemetry-sdk/tests/logs/test_handler.py @@ -16,23 +16,24 @@ from unittest.mock import Mock from opentelemetry.sdk import trace -from opentelemetry.sdk._logs import LogEmitter, LoggingHandler +from opentelemetry.sdk._logs import get_log_emitter, LogEmitterProvider, LoggingHandler from opentelemetry.sdk._logs.severity import SeverityNumber from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import INVALID_SPAN_CONTEXT -def get_logger(level=logging.NOTSET, log_emitter=None): +def get_logger(level=logging.NOTSET, log_emitter_provider=None): logger = logging.getLogger(__name__) - handler = LoggingHandler(level=level, log_emitter=log_emitter) + handler = LoggingHandler(level=level, log_emitter_provider=log_emitter_provider) logger.addHandler(handler) return logger class TestLoggingHandler(unittest.TestCase): def test_handler_default_log_level(self): - emitter_mock = Mock(spec=LogEmitter) - logger = get_logger(log_emitter=emitter_mock) + emitter_provider_mock = Mock(spec=LogEmitterProvider) + emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock) + logger = get_logger(log_emitter_provider=emitter_provider_mock) # Make sure debug messages are ignored by default logger.debug("Debug message") self.assertEqual(emitter_mock.emit.call_count, 0) @@ -41,8 +42,9 @@ def test_handler_default_log_level(self): self.assertEqual(emitter_mock.emit.call_count, 1) def test_handler_custom_log_level(self): - emitter_mock = Mock(spec=LogEmitter) - logger = get_logger(level=logging.ERROR, log_emitter=emitter_mock) + emitter_provider_mock = Mock(spec=LogEmitterProvider) + emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock) + logger = get_logger(level=logging.ERROR, log_emitter_provider=emitter_provider_mock) logger.warning("Warning message test custom log level") # Make sure any log with level < ERROR is ignored self.assertEqual(emitter_mock.emit.call_count, 0) @@ -51,8 +53,9 @@ def test_handler_custom_log_level(self): self.assertEqual(emitter_mock.emit.call_count, 2) def test_log_record_no_span_context(self): - emitter_mock = Mock(spec=LogEmitter) - logger = get_logger(log_emitter=emitter_mock) + emitter_provider_mock = Mock(spec=LogEmitterProvider) + emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock) + logger = get_logger(log_emitter_provider=emitter_provider_mock) # Assert emit gets called for warning message logger.warning("Warning message") args, _ = emitter_mock.emit.call_args_list[0] @@ -67,8 +70,9 @@ def test_log_record_no_span_context(self): def test_log_record_user_attributes(self): """Attributes can be injected into logs by adding them to the LogRecord""" - emitter_mock = Mock(spec=LogEmitter) - logger = get_logger(log_emitter=emitter_mock) + emitter_provider_mock = Mock(spec=LogEmitterProvider) + emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock) + logger = get_logger(log_emitter_provider=emitter_provider_mock) # Assert emit gets called for warning message logger.warning("Warning message", extra={"http.status_code": 200}) args, _ = emitter_mock.emit.call_args_list[0] @@ -79,8 +83,9 @@ def test_log_record_user_attributes(self): def test_log_record_exception(self): """Exception information will be included in attributes""" - emitter_mock = Mock(spec=LogEmitter) - logger = get_logger(log_emitter=emitter_mock) + emitter_provider_mock = Mock(spec=LogEmitterProvider) + emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock) + logger = get_logger(log_emitter_provider=emitter_provider_mock) try: raise ZeroDivisionError("division by zero") except ZeroDivisionError: @@ -109,8 +114,9 @@ def test_log_record_exception(self): 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) + emitter_provider_mock = Mock(spec=LogEmitterProvider) + emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock) + logger = get_logger(log_emitter_provider=emitter_provider_mock) try: raise ZeroDivisionError("division by zero") except ZeroDivisionError: @@ -129,8 +135,9 @@ def test_log_exc_info_false(self): ) def test_log_record_trace_correlation(self): - emitter_mock = Mock(spec=LogEmitter) - logger = get_logger(log_emitter=emitter_mock) + emitter_provider_mock = Mock(spec=LogEmitterProvider) + emitter_mock = get_log_emitter(__name__, log_emitter_provider=emitter_provider_mock) + logger = get_logger(log_emitter_provider=emitter_provider_mock) tracer = trace.TracerProvider().get_tracer(__name__) with tracer.start_as_current_span("test") as span: diff --git a/opentelemetry-sdk/tests/logs/test_multi_log_prcessor.py b/opentelemetry-sdk/tests/logs/test_multi_log_prcessor.py index 6a5838231b9..c87b8ba4098 100644 --- a/opentelemetry-sdk/tests/logs/test_multi_log_prcessor.py +++ b/opentelemetry-sdk/tests/logs/test_multi_log_prcessor.py @@ -57,8 +57,7 @@ def force_flush(self, timeout_millis=30000): class TestLogProcessor(unittest.TestCase): def test_log_processor(self): provider = LogEmitterProvider() - log_emitter = provider.get_log_emitter(__name__) - handler = LoggingHandler(log_emitter=log_emitter) + handler = LoggingHandler(log_emitter_provider=provider) logs_list_1 = [] processor1 = AnotherLogProcessor(Mock(), logs_list_1)