17
17
from logging import WARNING , getLogger
18
18
from os import environ
19
19
from typing import Dict , Iterable , Optional , Sequence
20
- from unittest import TestCase
20
+ from unittest import TestCase , mock
21
21
from unittest .mock import Mock , patch
22
22
23
23
from pytest import raises
@@ -663,6 +663,32 @@ def test_logging_init_exporter(self):
663
663
getLogger (__name__ ).error ("hello" )
664
664
self .assertTrue (provider .processor .exporter .export_called )
665
665
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
+
666
692
@patch .dict (
667
693
environ ,
668
694
{"OTEL_RESOURCE_ATTRIBUTES" : "service.name=otlp-service" },
@@ -671,8 +697,8 @@ def test_logging_init_exporter(self):
671
697
@patch ("opentelemetry.sdk._configuration._init_logging" )
672
698
def test_logging_init_disable_default (self , logging_mock , tracing_mock ):
673
699
_initialize_components (auto_instrumentation_version = "auto-version" )
674
- self .assertEqual (logging_mock .call_count , 0 )
675
700
self .assertEqual (tracing_mock .call_count , 1 )
701
+ logging_mock .assert_called_once_with (mock .ANY , mock .ANY , False )
676
702
677
703
@patch .dict (
678
704
environ ,
@@ -686,7 +712,7 @@ def test_logging_init_disable_default(self, logging_mock, tracing_mock):
686
712
def test_logging_init_enable_env (self , logging_mock , tracing_mock ):
687
713
with self .assertLogs (level = WARNING ):
688
714
_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 )
690
716
self .assertEqual (tracing_mock .call_count , 1 )
691
717
692
718
@patch .dict (
@@ -768,7 +794,7 @@ def test_initialize_components_kwargs(
768
794
"custom.key.2" : "pass-in-value-2" ,
769
795
},
770
796
"id_generator" : "TEST_GENERATOR" ,
771
- "logging_enabled " : True ,
797
+ "setup_logging_handler " : True ,
772
798
}
773
799
_initialize_components (** kwargs )
774
800
@@ -810,6 +836,7 @@ def test_initialize_components_kwargs(
810
836
logging_mock .assert_called_once_with (
811
837
"TEST_LOG_EXPORTERS_DICT" ,
812
838
"TEST_RESOURCE" ,
839
+ True ,
813
840
)
814
841
815
842
0 commit comments