1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
4
-
5
4
import json
6
5
import os
7
6
import platform
@@ -67,14 +66,10 @@ def tearDownClass(cls):
67
66
def test_constructor (self ):
68
67
"""Test the constructor."""
69
68
tp = trace .TracerProvider ()
70
- set_tracer_provider (tp )
71
69
exporter = AzureMonitorTraceExporter (
72
70
connection_string = "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" ,
73
71
)
74
- self .assertEqual (
75
- exporter ._tracer_provider ,
76
- tp ,
77
- )
72
+ self .assertIsNone (exporter ._tracer_provider )
78
73
self .assertEqual (
79
74
exporter ._instrumentation_key ,
80
75
"4321abcd-5678-4efa-8abc-1234567890ab" ,
@@ -83,15 +78,13 @@ def test_constructor(self):
83
78
def test_constructor_tracer_provider (self ):
84
79
"""Test the constructor."""
85
80
tp = trace .TracerProvider ()
86
- tp2 = trace .TracerProvider ()
87
- set_tracer_provider (tp )
88
81
exporter = AzureMonitorTraceExporter (
89
82
connection_string = "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" ,
90
- tracer_provider = tp2 ,
83
+ tracer_provider = tp ,
91
84
)
92
85
self .assertEqual (
93
86
exporter ._tracer_provider ,
94
- tp2 ,
87
+ tp ,
95
88
)
96
89
self .assertEqual (
97
90
exporter ._instrumentation_key ,
@@ -157,6 +150,72 @@ def test_export_success(self):
157
150
self .assertEqual (result , SpanExportResult .SUCCESS )
158
151
self .assertEqual (storage_mock .call_count , 1 )
159
152
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
+
160
219
@mock .patch ("azure.monitor.opentelemetry.exporter.export.trace._exporter._logger" )
161
220
def test_export_exception (self , logger_mock ):
162
221
test_span = trace ._Span (
0 commit comments