File: test_base_customer_sdkstats.py

package info (click to toggle)
python-azure 20251118%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 783,356 kB
  • sloc: python: 6,474,533; ansic: 804; javascript: 287; sh: 205; makefile: 198; xml: 109
file content (401 lines) | stat: -rw-r--r-- 20,778 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os
import shutil
import unittest
from unittest import mock
from datetime import datetime

from azure.core.exceptions import HttpResponseError, ServiceRequestError
from requests.exceptions import ConnectionError
from azure.monitor.opentelemetry.exporter.export._base import (
    BaseExporter,
    ExportResult,
)
from azure.monitor.opentelemetry.exporter._generated import AzureMonitorClient
from azure.monitor.opentelemetry.exporter._generated.models import (
    TelemetryItem,
    TrackResponse,
    TelemetryErrorDetails,
)
from azure.monitor.opentelemetry.exporter.statsbeat.customer._manager import (
    CustomerSdkStatsManager,
)
from azure.monitor.opentelemetry.exporter.statsbeat.customer._state import (
    get_customer_stats_manager,
)
from azure.monitor.opentelemetry.exporter._constants import (
    DropCode,
    RetryCode,
    _exception_categories,
)

from azure.monitor.opentelemetry.exporter.statsbeat.customer._utils import (
    track_successful_items,
    track_dropped_items,
    track_retry_items,
    track_dropped_items_from_storage,
)

class MockResponse:
    """Mock response object for HTTP requests"""
    def __init__(self, status_code, content):
        self.status_code = status_code
        self.content = content
        self.text = content
        self.headers = {}
        self.raw = mock.Mock()  # Add the raw attribute that Azure SDK expects
        self.raw.enforce_content_length = True
        self.reason = "Mock Reason"  # Add the reason attribute
        self.url = "http://mock-url.com"  # Add the url attribute


class TestBaseExporterCustomerSdkStats(unittest.TestCase):
    """Test integration between BaseExporter and customer sdkstats tracking functions"""

    @classmethod
    def setUpClass(cls):
        """Set up class-level resources including a single customer stats manager"""
        from azure.monitor.opentelemetry.exporter._generated.models import TelemetryEventData, MonitorBase
        
        os.environ.pop("APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW", None)
        os.environ["APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW"] = "true"
        
        # Patch _should_collect_customer_sdkstats instance method to always return True for all tests
        cls._should_collect_patch = mock.patch(
            'azure.monitor.opentelemetry.exporter.export._base.BaseExporter._should_collect_customer_sdkstats',
            return_value=True
        )
        cls._should_collect_patch.start()
        
        # Patch collect_customer_sdkstats to prevent actual initialization
        cls._collect_customer_sdkstats_patch = mock.patch(
            'azure.monitor.opentelemetry.exporter.statsbeat.customer.collect_customer_sdkstats'
        )
        cls._collect_customer_sdkstats_patch.start()
            
        # Create reusable test data structure for TelemetryItem
        base_data = TelemetryEventData(
            name="test_event",
            properties={"test_property": "test_value"}
        )
        monitor_base = MonitorBase(
            base_type="EventData",
            base_data=base_data
        )
        
        cls._envelopes_to_export = [
            TelemetryItem(
                name="test_envelope",
                time=datetime.now(),
                data=monitor_base,
                tags={"ai.internal.sdkVersion": "test_version"},
                instrumentation_key="test_key",
            )
        ]

    @classmethod
    def tearDownClass(cls):
        """Clean up class-level resources"""
        # Stop the patches
        cls._should_collect_patch.stop()
        cls._collect_customer_sdkstats_patch.stop()
        
        # Clean up environment
        os.environ.pop("APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW", None)

    def _create_exporter_with_customer_sdkstats_enabled(self, disable_offline_storage=True):
        """Helper method to create an exporter with customer sdkstats enabled"""
        
        exporter = BaseExporter(
            connection_string="InstrumentationKey=12345678-1234-5678-abcd-12345678abcd",
            disable_offline_storage=disable_offline_storage,
        )
            
        return exporter

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_successful_items")
    def test_transmit_200_customer_sdkstats_track_successful_items(self, track_successful_mock):
        """Test that track_successful_items is called on 200 success response"""
        
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            track_response = TrackResponse(
                items_received=1,
                items_accepted=1,
                errors=[],
            )
            track_mock.return_value = track_response
            result = exporter._transmit(self._envelopes_to_export)
        
        track_successful_mock.assert_called_once_with(self._envelopes_to_export)
        self.assertEqual(result, ExportResult.SUCCESS)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_retry_items")
    def test_transmit_206_customer_sdkstats_track_retry_items(self, track_retry_mock):
        """Test that _track_retry_items is called on 206 partial success with retryable errors"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            track_mock.return_value = TrackResponse(
                items_received=2,
                items_accepted=1,
                errors=[
                    TelemetryErrorDetails(index=0, status_code=500, message="should retry"),
                ],
            )
            result = exporter._transmit(self._envelopes_to_export * 2)

        track_retry_mock.assert_called_once()
        # With storage disabled by default, retryable errors become non-retryable
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_206_customer_sdkstats_track_dropped_items(self, track_dropped_mock):
        """Test that _track_dropped_items is called on 206 partial success with non-retryable errors"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            track_mock.return_value = TrackResponse(
                items_received=2,
                items_accepted=1,
                errors=[
                    TelemetryErrorDetails(index=0, status_code=400, message="should drop"),
                ],
            )
            result = exporter._transmit(self._envelopes_to_export * 2)

        track_dropped_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)


    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_retry_items")
    def test_transmit_retryable_http_error_customer_sdkstats_track_retry_items(self, track_retry_mock):
        """Test that _track_retry_items is called on retryable HTTP errors (e.g., 408, 502, 503, 504)"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        with mock.patch("requests.Session.request") as request_mock:
            request_mock.return_value = MockResponse(408, "{}")
            result = exporter._transmit(self._envelopes_to_export)

        track_retry_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_throttle_http_error_customer_sdkstats_track_dropped_items(self, track_dropped_mock):
        """Test that _track_dropped_items is called on throttle HTTP errors (e.g., 402, 439)"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()

        # Verify that _should_collect_customer_sdkstats is properly patched
        self.assertTrue(exporter._should_collect_customer_sdkstats())

        # Simulate a throttle HTTP error using HttpResponseError
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            error_response = mock.Mock()
            error_response.status_code = 402  # Use actual throttle code
            track_mock.side_effect = HttpResponseError("Throttling error", response=error_response)
            result = exporter._transmit(self._envelopes_to_export)

        track_dropped_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_invalid_http_error_customer_sdkstats_track_dropped_items_and_shutdown(self, track_dropped_mock):
        """Test that _track_dropped_items is called and customer sdkstats is shutdown on invalid HTTP errors (e.g., 400)"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        with mock.patch("requests.Session.request") as request_mock, \
             mock.patch("azure.monitor.opentelemetry.exporter.statsbeat.customer.shutdown_customer_sdkstats_metrics") as shutdown_mock:
            request_mock.return_value = MockResponse(400, "{}")
            result = exporter._transmit(self._envelopes_to_export)

        track_dropped_mock.assert_called_once()
        shutdown_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_retry_items")
    def test_transmit_service_request_error_customer_sdkstats_track_retry_items(self, track_retry_mock):
        """Test that _track_retry_items is called on ServiceRequestError"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        with mock.patch.object(AzureMonitorClient, "track", side_effect=ServiceRequestError("Connection error")):
            result = exporter._transmit(self._envelopes_to_export)

        track_retry_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_general_exception_customer_sdkstats_track_dropped_items(self, track_dropped_mock):
        """Test that _track_dropped_items is called on general exceptions"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        with mock.patch.object(AzureMonitorClient, "track", side_effect=Exception(_exception_categories.CLIENT_EXCEPTION.value)):
            result = exporter._transmit(self._envelopes_to_export)

        track_dropped_mock.assert_called_once()
        # We're not going to verify the specific argument values since they can change
        # Just make sure the function was called
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)


    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_storage_disabled_customer_sdkstats_track_dropped_items(self, track_dropped_mock):
        """Test that _track_dropped_items is called when offline storage is disabled and items would be retried"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            track_mock.return_value = TrackResponse(
                items_received=1,
                items_accepted=0,
                errors=[
                    TelemetryErrorDetails(index=0, status_code=500, message="should retry but storage disabled"),
                ],
            )
            result = exporter._transmit(self._envelopes_to_export)

        track_dropped_mock.assert_called_once()
        # The arguments structure has changed in the new implementation
        # No need to verify specific arguments as the function signature has changed
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)


    @mock.patch('azure.monitor.opentelemetry.exporter.export._base.track_dropped_items')
    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items_from_storage")
    def test_transmit_from_storage_customer_sdkstats_track_dropped_items_from_storage(self, track_dropped_storage_mock, track_dropped_items_mock):
        """Test that _track_dropped_items_from_storage is called during storage operations"""
        from azure.monitor.opentelemetry.exporter._storage import StorageExportResult
        
        exporter = self._create_exporter_with_customer_sdkstats_enabled(disable_offline_storage=False)
        
        # Set up side_effect for track_dropped_items_from_storage to match the new signature
        def track_dropped_storage_side_effect(result_from_storage_put, envelopes):
            # Import here to avoid import error
            # Using imported track_dropped_items_from_storage
            # Call the real function which will use our mocked track_dropped_items
            track_dropped_items_from_storage(result_from_storage_put, envelopes)
            
        track_dropped_storage_mock.side_effect = track_dropped_storage_side_effect
        
        # Mock _track_dropped_items to simulate a successful call
        track_dropped_items_mock.return_value = None
        
        # Simulate a scenario where storage operations would happen
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            track_mock.return_value = TrackResponse(
                items_received=1,
                items_accepted=0,
                errors=[
                    TelemetryErrorDetails(index=0, status_code=500, message="should retry"),
                ],
            )
            
            # Mock the storage to simulate storage operations - simulate storage error
            with mock.patch.object(exporter.storage, "put", return_value="storage_error") as put_mock, \
                 mock.patch.object(exporter.storage, "gets", return_value=["stored_envelope"]) as gets_mock:
                # We don't need to mock StorageExportResult anymore
                result = exporter._transmit(self._envelopes_to_export)

        track_dropped_storage_mock.assert_called_once()
        
        # No need to verify specific arguments as the function signature has changed
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)  # Storage makes it NOT_RETRYABLE

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_redirect_parsing_error_customer_sdkstats_track_dropped_items(self, track_dropped_mock):
        """Test that track_dropped_items is called on redirect errors with invalid headers/parsing errors"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        
        # Simulate a redirect HTTP error using HttpResponseError without proper headers
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            error_response = mock.Mock()
            error_response.status_code = 307  # Redirect status code
            error_response.headers = None  # No headers to cause parsing error
            track_mock.side_effect = HttpResponseError("Redirect error", response=error_response)
            result = exporter._transmit(self._envelopes_to_export)

        track_dropped_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_circular_redirect_customer_sdkstats_track_dropped_items(self, track_dropped_mock):
        """Test that track_dropped_items is called on circular redirect errors"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        
        # Mock the consecutive redirects counter to simulate exceeding max redirects
        exporter._consecutive_redirects = 10  # Set to a high value to simulate circular redirects
        
        # Simulate redirect responses that would cause circular redirects
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            error_response = mock.Mock()
            error_response.status_code = 307  # Redirect status code
            error_response.headers = {"location": "https://example.com/redirect"}
            track_mock.side_effect = HttpResponseError("Redirect error", response=error_response)
            result = exporter._transmit(self._envelopes_to_export)

        track_dropped_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_retry_items")
    def test_transmit_403_forbidden_error_customer_sdkstats_track_retry_items(self, track_retry_mock):
        """Test that track_retry_items is called on 403 Forbidden HTTP errors"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()

        # Simulate a 403 Forbidden HTTP error using HttpResponseError
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            error_response = mock.Mock()
            error_response.status_code = 403  # Forbidden code
            track_mock.side_effect = HttpResponseError("Forbidden error", response=error_response)
            result = exporter._transmit(self._envelopes_to_export)

        track_retry_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_retry_items")
    def test_transmit_401_unauthorized_error_customer_sdkstats_track_retry_items(self, track_retry_mock):
        """Test that track_retry_items is called on 401 Unauthorized HTTP errors"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()

        # Simulate a 401 Unauthorized HTTP error using HttpResponseError
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            error_response = mock.Mock()
            error_response.status_code = 401  # Unauthorized code
            track_mock.side_effect = HttpResponseError("Unauthorized error", response=error_response)
            result = exporter._transmit(self._envelopes_to_export)

        track_retry_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_redirect_invalid_location_header_customer_sdkstats_track_dropped_items(self, track_dropped_mock):
        """Test that track_dropped_items is called when redirect has invalid location header"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled()
        
        # Simulate a redirect HTTP error with invalid location header
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            error_response = mock.Mock()
            error_response.status_code = 307  # Redirect status code
            error_response.headers = {"location": "invalid-url"}  # Invalid URL format
            track_mock.side_effect = HttpResponseError("Redirect error", response=error_response)
            result = exporter._transmit(self._envelopes_to_export)

        track_dropped_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)

    @mock.patch("azure.monitor.opentelemetry.exporter.export._base.track_dropped_items")
    def test_transmit_from_storage_failure_customer_sdkstats_track_dropped_items(self, track_dropped_mock):
        """Test that track_dropped_items is called when _transmit_from_storage operations fail"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled(disable_offline_storage=False)
        
        # Mock storage operations to simulate a successful initial transmit that triggers storage operations
        with mock.patch.object(AzureMonitorClient, "track") as track_mock:
            track_response = TrackResponse(
                items_received=1,
                items_accepted=1,
                errors=[],
            )
            track_mock.return_value = track_response
            
            # Mock _transmit_from_storage to raise an exception
            with mock.patch.object(exporter, '_transmit_from_storage', side_effect=Exception("Storage operation failed")):
                result = exporter._transmit(self._envelopes_to_export)

        # Should still succeed for the main transmission
        self.assertEqual(result, ExportResult.SUCCESS)

if __name__ == "__main__":
    unittest.main()