1010from unittest import mock
1111
1212# pylint: disable=import-error
13+ from opentelemetry .trace import get_tracer_provider , set_tracer_provider
1314from opentelemetry .sdk import trace , resources
1415from opentelemetry .sdk .trace .export import SpanExportResult
1516from opentelemetry .sdk .util .instrumentation import InstrumentationScope
@@ -55,17 +56,43 @@ def setUpClass(cls):
5556 ] = "1234abcd-5678-4efa-8abc-1234567890ab"
5657 os .environ ["APPLICATIONINSIGHTS_STATSBEAT_DISABLED_ALL" ] = "true"
5758 os .environ ["APPLICATIONINSIGHTS_OPENTELEMETRY_RESOURCE_METRIC_DISABLED" ] = "true"
58- cls ._exporter = AzureMonitorTraceExporter ()
59+ cls ._exporter = AzureMonitorTraceExporter (
60+ tracer_provider = trace .TracerProvider (),
61+ )
5962
6063 @classmethod
6164 def tearDownClass (cls ):
6265 shutil .rmtree (cls ._exporter .storage ._path , True )
6366
6467 def test_constructor (self ):
6568 """Test the constructor."""
69+ tp = trace .TracerProvider ()
70+ set_tracer_provider (tp )
6671 exporter = AzureMonitorTraceExporter (
6772 connection_string = "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" ,
6873 )
74+ self .assertEqual (
75+ exporter ._tracer_provider ,
76+ tp ,
77+ )
78+ self .assertEqual (
79+ exporter ._instrumentation_key ,
80+ "4321abcd-5678-4efa-8abc-1234567890ab" ,
81+ )
82+
83+ def test_constructor_tracer_provider (self ):
84+ """Test the constructor."""
85+ tp = trace .TracerProvider ()
86+ tp2 = trace .TracerProvider ()
87+ set_tracer_provider (tp )
88+ exporter = AzureMonitorTraceExporter (
89+ connection_string = "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" ,
90+ tracer_provider = tp2 ,
91+ )
92+ self .assertEqual (
93+ exporter ._tracer_provider ,
94+ tp2 ,
95+ )
6996 self .assertEqual (
7097 exporter ._instrumentation_key ,
7198 "4321abcd-5678-4efa-8abc-1234567890ab" ,
@@ -1326,7 +1353,6 @@ def test_export_otel_resource_metric(self, mock_get_tracer_provider):
13261353 del os .environ ["APPLICATIONINSIGHTS_OPENTELEMETRY_RESOURCE_METRIC_DISABLED" ]
13271354 mock_tracer_provider = mock .Mock ()
13281355 mock_get_tracer_provider .return_value = mock_tracer_provider
1329- exporter = self ._exporter
13301356 test_resource = resources .Resource (
13311357 attributes = {
13321358 "string_test_key" : "string_value" ,
@@ -1347,6 +1373,9 @@ def test_export_otel_resource_metric(self, mock_get_tracer_provider):
13471373 )
13481374 test_span .start ()
13491375 test_span .end ()
1376+ exporter = AzureMonitorTraceExporter (
1377+ _disable_offline_storage = True ,
1378+ )
13501379 with mock .patch (
13511380 "azure.monitor.opentelemetry.exporter.AzureMonitorTraceExporter._transmit"
13521381 ) as transmit : # noqa: E501
@@ -1438,4 +1467,4 @@ def test_check_instrumentation_span_azure_sdk(self):
14381467 "azure.monitor.opentelemetry.exporter._utils.add_instrumentation"
14391468 ) as add :
14401469 _check_instrumentation_span (span )
1441- add .assert_called_once_with (_AZURE_SDK_OPENTELEMETRY_NAME )
1470+ add .assert_called_once_with (_AZURE_SDK_OPENTELEMETRY_NAME )
0 commit comments