11# Copyright (c) Microsoft Corporation. All rights reserved.
22# Licensed under the MIT License.
33
4-
54import json
65import os
76import platform
@@ -67,14 +66,10 @@ def tearDownClass(cls):
6766 def test_constructor (self ):
6867 """Test the constructor."""
6968 tp = trace .TracerProvider ()
70- set_tracer_provider (tp )
7169 exporter = AzureMonitorTraceExporter (
7270 connection_string = "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" ,
7371 )
74- self .assertEqual (
75- exporter ._tracer_provider ,
76- tp ,
77- )
72+ self .assertIsNone (exporter ._tracer_provider )
7873 self .assertEqual (
7974 exporter ._instrumentation_key ,
8075 "4321abcd-5678-4efa-8abc-1234567890ab" ,
@@ -83,15 +78,13 @@ def test_constructor(self):
8378 def test_constructor_tracer_provider (self ):
8479 """Test the constructor."""
8580 tp = trace .TracerProvider ()
86- tp2 = trace .TracerProvider ()
87- set_tracer_provider (tp )
8881 exporter = AzureMonitorTraceExporter (
8982 connection_string = "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" ,
90- tracer_provider = tp2 ,
83+ tracer_provider = tp ,
9184 )
9285 self .assertEqual (
9386 exporter ._tracer_provider ,
94- tp2 ,
87+ tp ,
9588 )
9689 self .assertEqual (
9790 exporter ._instrumentation_key ,
@@ -157,6 +150,72 @@ def test_export_success(self):
157150 self .assertEqual (result , SpanExportResult .SUCCESS )
158151 self .assertEqual (storage_mock .call_count , 1 )
159152
153+ def test_export_with_tracer_provider (self ):
154+ mock_resource = mock .Mock ()
155+ tp = trace .TracerProvider (
156+ resource = mock_resource ,
157+ )
158+ exporter = AzureMonitorTraceExporter (
159+ connection_string = "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" ,
160+ tracer_provider = tp ,
161+ )
162+ test_span = trace ._Span (
163+ name = "test" ,
164+ context = SpanContext (
165+ trace_id = 36873507687745823477771305566750195431 ,
166+ span_id = 12030755672171557338 ,
167+ is_remote = False ,
168+ ),
169+ )
170+ test_span .start ()
171+ test_span .end ()
172+ with mock .patch (
173+ "azure.monitor.opentelemetry.exporter.AzureMonitorTraceExporter._transmit"
174+ ) as transmit : # noqa: E501
175+ transmit .return_value = ExportResult .SUCCESS
176+ storage_mock = mock .Mock ()
177+ exporter ._transmit_from_storage = storage_mock
178+ with mock .patch (
179+ "azure.monitor.opentelemetry.exporter.AzureMonitorTraceExporter._get_otel_resource_envelope"
180+ ) as resource_patch : # noqa: E501
181+ result = exporter .export ([test_span ])
182+ resource_patch .assert_called_once_with (mock_resource )
183+ self .assertEqual (result , SpanExportResult .SUCCESS )
184+ self .assertEqual (storage_mock .call_count , 1 )
185+
186+ def test_export_with_tracer_provider_global (self ):
187+ mock_resource = mock .Mock ()
188+ tp = trace .TracerProvider (
189+ resource = mock_resource ,
190+ )
191+ set_tracer_provider (tp )
192+ exporter = AzureMonitorTraceExporter (
193+ connection_string = "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" ,
194+ )
195+ test_span = trace ._Span (
196+ name = "test" ,
197+ context = SpanContext (
198+ trace_id = 36873507687745823477771305566750195431 ,
199+ span_id = 12030755672171557338 ,
200+ is_remote = False ,
201+ ),
202+ )
203+ test_span .start ()
204+ test_span .end ()
205+ with mock .patch (
206+ "azure.monitor.opentelemetry.exporter.AzureMonitorTraceExporter._transmit"
207+ ) as transmit : # noqa: E501
208+ transmit .return_value = ExportResult .SUCCESS
209+ storage_mock = mock .Mock ()
210+ exporter ._transmit_from_storage = storage_mock
211+ with mock .patch (
212+ "azure.monitor.opentelemetry.exporter.AzureMonitorTraceExporter._get_otel_resource_envelope"
213+ ) as resource_patch : # noqa: E501
214+ result = exporter .export ([test_span ])
215+ resource_patch .assert_called_once_with (mock_resource )
216+ self .assertEqual (result , SpanExportResult .SUCCESS )
217+ self .assertEqual (storage_mock .call_count , 1 )
218+
160219 @mock .patch ("azure.monitor.opentelemetry.exporter.export.trace._exporter._logger" )
161220 def test_export_exception (self , logger_mock ):
162221 test_span = trace ._Span (
0 commit comments