File: test_base_customer_sdkstats.py

package info (click to toggle)
python-azure 20250829%2Bgit-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 756,824 kB
  • sloc: python: 6,224,989; ansic: 804; javascript: 287; makefile: 198; sh: 195; xml: 109
file content (336 lines) | stat: -rw-r--r-- 15,710 bytes parent folder | download
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
# 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 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_sdkstats import (
    CustomerSdkStatsMetrics,
    DropCode,
)


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"""

    def setUp(self):
        from azure.monitor.opentelemetry.exporter._generated.models import TelemetryEventData
        self._envelopes_to_export = [
            TelemetryItem(
                name="test_envelope",
                time=datetime.now(),
                data=TelemetryEventData(
                    name="test_event",
                    properties={"test_property": "test_value"}
                ),
                tags={"ai.internal.sdkVersion": "test_version"},
                instrumentation_key="test_key",
            )
        ]

    def tearDown(self):
        # Clean up any environment variables
        for key in ["APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW"]:
            if key in os.environ:
                del os.environ[key]
        # Clean up any temp directories
        if hasattr(self, "_temp_dir") and os.path.exists(self._temp_dir):
            shutil.rmtree(self._temp_dir, ignore_errors=True)

    def _create_exporter_with_customer_sdkstats_enabled(self, disable_offline_storage=True):
        """Helper method to create an exporter with customer sdkstats enabled"""
        # Mock the customer sdkstats metrics from the correct import location
        with mock.patch("azure.monitor.opentelemetry.exporter.statsbeat._customer_sdkstats.CustomerSdkStatsMetrics") as customer_sdkstats_mock:
            customer_sdkstats_instance = mock.Mock(spec=CustomerSdkStatsMetrics)
            customer_sdkstats_mock.return_value = customer_sdkstats_instance
            
            exporter = BaseExporter(
                connection_string="InstrumentationKey=12345678-1234-5678-abcd-12345678abcd",
                disable_offline_storage=disable_offline_storage,
            )
            
            # Set up the mocked customer sdkstats metrics instance
            exporter._customer_sdkstats_metrics = customer_sdkstats_instance
            
            # Mock the should_collect method to return True
            exporter._should_collect_customer_sdkstats = mock.Mock(return_value=True)
            
            return exporter

    @mock.patch.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    def test_customer_sdkstats_feature_flag_disabled(self):
        """Test that customer sdkstats tracking is not called when feature flag is disabled"""
        # Remove the environment variable to simulate disabled state
        del os.environ["APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW"]
        
        exporter = BaseExporter(connection_string="InstrumentationKey=12345678-1234-5678-abcd-12345678abcd")
        # Verify that customer sdkstats metrics is None when feature is disabled
        self.assertIsNone(exporter._customer_sdkstats_metrics)
        self.assertFalse(exporter._should_collect_customer_sdkstats())

    @mock.patch.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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(exporter._customer_sdkstats_metrics, self._envelopes_to_export)
        self.assertEqual(result, ExportResult.SUCCESS)

    @mock.patch.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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()

        # 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.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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_sdkstats.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.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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("General error")):
            result = exporter._transmit(self._envelopes_to_export)

        track_dropped_mock.assert_called_once()
        # Verify called with CLIENT_EXCEPTION drop code and error message
        args, kwargs = track_dropped_mock.call_args
        self.assertEqual(args[2], DropCode.CLIENT_EXCEPTION)
        self.assertEqual(args[3], "General error")
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)

    @mock.patch.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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()
        # Verify called with CLIENT_STORAGE_DISABLED drop code
        args, kwargs = track_dropped_mock.call_args
        self.assertEqual(args[2], DropCode.CLIENT_STORAGE_DISABLED)
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)

    @mock.patch.dict(
        os.environ,
        {
            "APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true",
        },
    )
    @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):
        """Test that _track_dropped_items_from_storage is called during storage operations"""
        exporter = self._create_exporter_with_customer_sdkstats_enabled(disable_offline_storage=False)
        
        # 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
            with mock.patch.object(exporter.storage, "put") as put_mock, \
                 mock.patch.object(exporter.storage, "gets", return_value=["stored_envelope"]) as gets_mock:
                result = exporter._transmit(self._envelopes_to_export)

        track_dropped_storage_mock.assert_called_once()
        self.assertEqual(result, ExportResult.FAILED_NOT_RETRYABLE)  # Storage makes it NOT_RETRYABLE

    def test_should_collect_customer_sdkstats_with_metrics(self):
        """Test _should_collect_customer_sdkstats returns True when metrics exist and feature is enabled"""
        with mock.patch.dict(os.environ, {"APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW": "true"}):
            exporter = self._create_exporter_with_customer_sdkstats_enabled()
            self.assertTrue(exporter._should_collect_customer_sdkstats())

    def test_should_collect_customer_sdkstats_without_metrics(self):
        """Test _should_collect_customer_sdkstats returns False when no metrics exist"""
        # Don't patch the environment variable - let it be disabled by default
        exporter = BaseExporter(connection_string="InstrumentationKey=12345678-1234-5678-abcd-12345678abcd")
        exporter._customer_sdkstats_metrics = None
        self.assertFalse(exporter._should_collect_customer_sdkstats())


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