40
40
_init_metrics ,
41
41
_init_tracing ,
42
42
_initialize_components ,
43
+ _OTelSDKConfigurator ,
43
44
)
44
45
from opentelemetry .sdk ._logs import LoggingHandler
45
46
from opentelemetry .sdk ._logs .export import ConsoleLogExporter
@@ -645,7 +646,7 @@ def test_logging_init_exporter(self):
645
646
@patch ("opentelemetry.sdk._configuration._init_tracing" )
646
647
@patch ("opentelemetry.sdk._configuration._init_logging" )
647
648
def test_logging_init_disable_default (self , logging_mock , tracing_mock ):
648
- _initialize_components ("auto-version" )
649
+ _initialize_components (auto_instrumentation_version = "auto-version" )
649
650
self .assertEqual (logging_mock .call_count , 0 )
650
651
self .assertEqual (tracing_mock .call_count , 1 )
651
652
@@ -660,7 +661,7 @@ def test_logging_init_disable_default(self, logging_mock, tracing_mock):
660
661
@patch ("opentelemetry.sdk._configuration._init_logging" )
661
662
def test_logging_init_enable_env (self , logging_mock , tracing_mock ):
662
663
with self .assertLogs (level = WARNING ):
663
- _initialize_components ("auto-version" )
664
+ _initialize_components (auto_instrumentation_version = "auto-version" )
664
665
self .assertEqual (logging_mock .call_count , 1 )
665
666
self .assertEqual (tracing_mock .call_count , 1 )
666
667
@@ -677,7 +678,7 @@ def test_logging_init_enable_env(self, logging_mock, tracing_mock):
677
678
def test_initialize_components_resource (
678
679
self , metrics_mock , logging_mock , tracing_mock
679
680
):
680
- _initialize_components ("auto-version" )
681
+ _initialize_components (auto_instrumentation_version = "auto-version" )
681
682
self .assertEqual (logging_mock .call_count , 1 )
682
683
self .assertEqual (tracing_mock .call_count , 1 )
683
684
self .assertEqual (metrics_mock .call_count , 1 )
@@ -692,6 +693,101 @@ def test_initialize_components_resource(
692
693
self .assertEqual (logging_resource , metrics_resource )
693
694
self .assertEqual (tracing_resource , metrics_resource )
694
695
696
+ @patch .dict (
697
+ environ ,
698
+ {
699
+ "OTEL_TRACES_EXPORTER" : _EXPORTER_OTLP ,
700
+ "OTEL_METRICS_EXPORTER" : _EXPORTER_OTLP_PROTO_GRPC ,
701
+ "OTEL_LOGS_EXPORTER" : _EXPORTER_OTLP_PROTO_HTTP ,
702
+ },
703
+ )
704
+ @patch .dict (
705
+ environ ,
706
+ {
707
+ "OTEL_RESOURCE_ATTRIBUTES" : "service.name=otlp-service, custom.key.1=env-value" ,
708
+ "OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED" : "False" ,
709
+ },
710
+ )
711
+ @patch ("opentelemetry.sdk._configuration.Resource" )
712
+ @patch ("opentelemetry.sdk._configuration._import_exporters" )
713
+ @patch ("opentelemetry.sdk._configuration._get_exporter_names" )
714
+ @patch ("opentelemetry.sdk._configuration._init_tracing" )
715
+ @patch ("opentelemetry.sdk._configuration._init_logging" )
716
+ @patch ("opentelemetry.sdk._configuration._init_metrics" )
717
+ def test_initialize_components_kwargs (
718
+ self ,
719
+ metrics_mock ,
720
+ logging_mock ,
721
+ tracing_mock ,
722
+ exporter_names_mock ,
723
+ import_exporters_mock ,
724
+ resource_mock ,
725
+ ):
726
+ exporter_names_mock .return_value = [
727
+ "env_var_exporter_1" ,
728
+ "env_var_exporter_2" ,
729
+ ]
730
+ import_exporters_mock .return_value = (
731
+ "TEST_SPAN_EXPORTERS_DICT" ,
732
+ "TEST_METRICS_EXPORTERS_DICT" ,
733
+ "TEST_LOG_EXPORTERS_DICT" ,
734
+ )
735
+ resource_mock .create .return_value = "TEST_RESOURCE"
736
+ kwargs = {
737
+ "auto_instrumentation_version" : "auto-version" ,
738
+ "trace_exporter_names" : ["custom_span_exporter" ],
739
+ "metric_exporter_names" : ["custom_metric_exporter" ],
740
+ "log_exporter_names" : ["custom_log_exporter" ],
741
+ "sampler" : "TEST_SAMPLER" ,
742
+ "resource_attributes" : {
743
+ "custom.key.1" : "pass-in-value-1" ,
744
+ "custom.key.2" : "pass-in-value-2" ,
745
+ },
746
+ "id_generator" : "TEST_GENERATOR" ,
747
+ "logging_enabled" : True ,
748
+ }
749
+ _initialize_components (** kwargs )
750
+
751
+ import_exporters_mock .assert_called_once_with (
752
+ [
753
+ "custom_span_exporter" ,
754
+ "env_var_exporter_1" ,
755
+ "env_var_exporter_2" ,
756
+ ],
757
+ [
758
+ "custom_metric_exporter" ,
759
+ "env_var_exporter_1" ,
760
+ "env_var_exporter_2" ,
761
+ ],
762
+ [
763
+ "custom_log_exporter" ,
764
+ "env_var_exporter_1" ,
765
+ "env_var_exporter_2" ,
766
+ ],
767
+ )
768
+ resource_mock .create .assert_called_once_with (
769
+ {
770
+ "telemetry.auto.version" : "auto-version" ,
771
+ "custom.key.1" : "pass-in-value-1" ,
772
+ "custom.key.2" : "pass-in-value-2" ,
773
+ }
774
+ )
775
+ # Resource is checked separates
776
+ tracing_mock .assert_called_once_with (
777
+ exporters = "TEST_SPAN_EXPORTERS_DICT" ,
778
+ id_generator = "TEST_GENERATOR" ,
779
+ sampler = "TEST_SAMPLER" ,
780
+ resource = "TEST_RESOURCE" ,
781
+ )
782
+ metrics_mock .assert_called_once_with (
783
+ "TEST_METRICS_EXPORTERS_DICT" ,
784
+ "TEST_RESOURCE" ,
785
+ )
786
+ logging_mock .assert_called_once_with (
787
+ "TEST_LOG_EXPORTERS_DICT" ,
788
+ "TEST_RESOURCE" ,
789
+ )
790
+
695
791
696
792
class TestMetricsInit (TestCase ):
697
793
def setUp (self ):
@@ -910,3 +1006,22 @@ def test__import_config_components_missing_component(
910
1006
str (error .value ),
911
1007
"Requested component 'a' not found in entry point 'name'" ,
912
1008
)
1009
+
1010
+
1011
+ class TestConfigurator (TestCase ):
1012
+ class CustomConfigurator (_OTelSDKConfigurator ):
1013
+ def _configure (self , ** kwargs ):
1014
+ kwargs ["sampler" ] = "TEST_SAMPLER"
1015
+ super ()._configure (** kwargs )
1016
+
1017
+ @patch ("opentelemetry.sdk._configuration._initialize_components" )
1018
+ def test_custom_configurator (self , mock_init_comp ):
1019
+ custom_configurator = TestConfigurator .CustomConfigurator ()
1020
+ custom_configurator ._configure (
1021
+ auto_instrumentation_version = "TEST_VERSION2"
1022
+ )
1023
+ kwargs = {
1024
+ "auto_instrumentation_version" : "TEST_VERSION2" ,
1025
+ "sampler" : "TEST_SAMPLER" ,
1026
+ }
1027
+ mock_init_comp .assert_called_once_with (** kwargs )
0 commit comments