1717from logging import WARNING , getLogger
1818from os import environ
1919from typing import Dict , Iterable , Optional , Sequence
20- from unittest import TestCase
20+ from unittest import TestCase , mock
2121from unittest .mock import Mock , patch
2222
2323from pytest import raises
@@ -663,6 +663,32 @@ def test_logging_init_exporter(self):
663663 getLogger (__name__ ).error ("hello" )
664664 self .assertTrue (provider .processor .exporter .export_called )
665665
666+ @patch .dict (
667+ environ ,
668+ {"OTEL_RESOURCE_ATTRIBUTES" : "service.name=otlp-service" },
669+ )
670+ def test_logging_init_exporter_without_handler_setup (self ):
671+ resource = Resource .create ({})
672+ _init_logging (
673+ {"otlp" : DummyOTLPLogExporter },
674+ resource = resource ,
675+ setup_logging_handler = False ,
676+ )
677+ self .assertEqual (self .set_provider_mock .call_count , 1 )
678+ provider = self .set_provider_mock .call_args [0 ][0 ]
679+ self .assertIsInstance (provider , DummyLoggerProvider )
680+ self .assertIsInstance (provider .resource , Resource )
681+ self .assertEqual (
682+ provider .resource .attributes .get ("service.name" ),
683+ "otlp-service" ,
684+ )
685+ self .assertIsInstance (provider .processor , DummyLogRecordProcessor )
686+ self .assertIsInstance (
687+ provider .processor .exporter , DummyOTLPLogExporter
688+ )
689+ getLogger (__name__ ).error ("hello" )
690+ self .assertFalse (provider .processor .exporter .export_called )
691+
666692 @patch .dict (
667693 environ ,
668694 {"OTEL_RESOURCE_ATTRIBUTES" : "service.name=otlp-service" },
@@ -671,8 +697,8 @@ def test_logging_init_exporter(self):
671697 @patch ("opentelemetry.sdk._configuration._init_logging" )
672698 def test_logging_init_disable_default (self , logging_mock , tracing_mock ):
673699 _initialize_components (auto_instrumentation_version = "auto-version" )
674- self .assertEqual (logging_mock .call_count , 0 )
675700 self .assertEqual (tracing_mock .call_count , 1 )
701+ logging_mock .assert_called_once_with (mock .ANY , mock .ANY , False )
676702
677703 @patch .dict (
678704 environ ,
@@ -686,7 +712,7 @@ def test_logging_init_disable_default(self, logging_mock, tracing_mock):
686712 def test_logging_init_enable_env (self , logging_mock , tracing_mock ):
687713 with self .assertLogs (level = WARNING ):
688714 _initialize_components (auto_instrumentation_version = "auto-version" )
689- self . assertEqual ( logging_mock . call_count , 1 )
715+ logging_mock . assert_called_once_with ( mock . ANY , mock . ANY , True )
690716 self .assertEqual (tracing_mock .call_count , 1 )
691717
692718 @patch .dict (
@@ -768,7 +794,7 @@ def test_initialize_components_kwargs(
768794 "custom.key.2" : "pass-in-value-2" ,
769795 },
770796 "id_generator" : "TEST_GENERATOR" ,
771- "logging_enabled " : True ,
797+ "setup_logging_handler " : True ,
772798 }
773799 _initialize_components (** kwargs )
774800
@@ -810,6 +836,7 @@ def test_initialize_components_kwargs(
810836 logging_mock .assert_called_once_with (
811837 "TEST_LOG_EXPORTERS_DICT" ,
812838 "TEST_RESOURCE" ,
839+ True ,
813840 )
814841
815842
0 commit comments