56
56
increment_and_check_statsbeat_failure_count ,
57
57
is_statsbeat_enabled ,
58
58
set_statsbeat_initial_success ,
59
+ is_customer_sdkstats_enabled ,
59
60
)
60
61
from azure .monitor .opentelemetry .exporter .statsbeat ._utils import (
61
62
_update_requests_map ,
63
+ _track_dropped_items_from_storage ,
62
64
_track_dropped_items ,
63
65
_track_retry_items ,
64
66
_track_successful_items ,
65
- _track_dropped_items_from_storage ,
66
67
)
67
68
68
69
@@ -181,8 +182,7 @@ def __init__(self, **kwargs: Any) -> None:
181
182
except Exception as e : # pylint: disable=broad-except
182
183
logger .warning ("Failed to initialize statsbeat metrics: %s" , e )
183
184
184
- # Initialize customer sdkstats if enabled
185
- self ._customer_sdkstats_metrics = None
185
+ # customer sdkstats initialization
186
186
if self ._should_collect_customer_sdkstats ():
187
187
188
188
from azure .monitor .opentelemetry .exporter .statsbeat ._customer_sdkstats import collect_customer_sdkstats
@@ -209,16 +209,16 @@ def _handle_transmit_from_storage(self, envelopes: List[TelemetryItem], result:
209
209
if result == ExportResult .FAILED_RETRYABLE :
210
210
envelopes_to_store = [x .as_dict () for x in envelopes ]
211
211
result_from_storage_put = self .storage .put (envelopes_to_store )
212
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
213
- _track_dropped_items_from_storage (self . _customer_sdkstats_metrics , result_from_storage_put , envelopes )
212
+ if self ._should_collect_customer_sdkstats ():
213
+ _track_dropped_items_from_storage (result_from_storage_put , envelopes )
214
214
elif result == ExportResult .SUCCESS :
215
215
# Try to send any cached events
216
216
self ._transmit_from_storage ()
217
217
218
218
else :
219
219
# Track items that would have been retried but are dropped since client has local storage disabled
220
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
221
- _track_dropped_items (self . _customer_sdkstats_metrics , envelopes , DropCode .CLIENT_STORAGE_DISABLED )
220
+ if self ._should_collect_customer_sdkstats ():
221
+ _track_dropped_items (envelopes , DropCode .CLIENT_STORAGE_DISABLED )
222
222
223
223
# pylint: disable=too-many-branches
224
224
# pylint: disable=too-many-nested-blocks
@@ -256,23 +256,23 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
256
256
result = ExportResult .SUCCESS
257
257
258
258
# Track successful items in customer sdkstats
259
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
260
- _track_successful_items (self . _customer_sdkstats_metrics , envelopes )
259
+ if self ._should_collect_customer_sdkstats ():
260
+ _track_successful_items (envelopes )
261
261
else : # 206
262
262
reach_ingestion = True
263
263
resend_envelopes = []
264
264
for error in track_response .errors :
265
265
if _is_retryable_code (error .status_code ):
266
266
resend_envelopes .append (envelopes [error .index ]) # type: ignore
267
267
# Track retried items in customer sdkstats
268
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
269
- _track_retry_items (self . _customer_sdkstats_metrics , resend_envelopes , error )
268
+ if self ._should_collect_customer_sdkstats ():
269
+ _track_retry_items (resend_envelopes , error )
270
270
else :
271
271
if not self ._is_stats_exporter ():
272
272
# Track dropped items in customer sdkstats, non-retryable scenario
273
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
274
- if error is not None and hasattr (error , "index" ) and error .index is not None :
275
- _track_dropped_items (self . _customer_sdkstats_metrics , [envelopes [error .index ]], error .status_code )
273
+ if self ._should_collect_customer_sdkstats ():
274
+ if error is not None and hasattr (error , "index" ) and error .index is not None and isinstance ( error . status_code , int ) :
275
+ _track_dropped_items ([envelopes [error .index ]], error .status_code )
276
276
logger .error (
277
277
"Data drop %s: %s %s." ,
278
278
error .status_code ,
@@ -282,13 +282,13 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
282
282
if self .storage and resend_envelopes :
283
283
envelopes_to_store = [x .as_dict () for x in resend_envelopes ]
284
284
result_from_storage = self .storage .put (envelopes_to_store , 0 )
285
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
286
- _track_dropped_items_from_storage (self . _customer_sdkstats_metrics , result_from_storage , resend_envelopes )
285
+ if self ._should_collect_customer_sdkstats ():
286
+ _track_dropped_items_from_storage (result_from_storage , resend_envelopes )
287
287
self ._consecutive_redirects = 0
288
288
elif resend_envelopes :
289
289
# Track items that would have been retried but are dropped since client has local storage disabled
290
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
291
- _track_dropped_items (self . _customer_sdkstats_metrics , resend_envelopes , DropCode .CLIENT_STORAGE_DISABLED )
290
+ if self ._should_collect_customer_sdkstats ():
291
+ _track_dropped_items (resend_envelopes , DropCode .CLIENT_STORAGE_DISABLED )
292
292
# Mark as not retryable because we already write to storage here
293
293
result = ExportResult .FAILED_NOT_RETRYABLE
294
294
except HttpResponseError as response_error :
@@ -301,8 +301,8 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
301
301
result = ExportResult .FAILED_RETRYABLE
302
302
# Log error for 401: Unauthorized, 403: Forbidden to assist with customer troubleshooting
303
303
if not self ._is_stats_exporter ():
304
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
305
- _track_retry_items (self . _customer_sdkstats_metrics , envelopes , response_error )
304
+ if self ._should_collect_customer_sdkstats ():
305
+ _track_retry_items (envelopes , response_error )
306
306
if response_error .status_code == 401 :
307
307
logger .error (
308
308
"Retryable server side error: %s. " \
@@ -311,8 +311,8 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
311
311
response_error .message ,
312
312
)
313
313
elif response_error .status_code == 403 :
314
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
315
- _track_retry_items (self . _customer_sdkstats_metrics , envelopes , response_error )
314
+ if self ._should_collect_customer_sdkstats ():
315
+ _track_retry_items (envelopes , response_error )
316
316
logger .error (
317
317
"Retryable server side error: %s. " \
318
318
"Your application may be configured with a token credential " \
@@ -327,8 +327,8 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
327
327
result = ExportResult .FAILED_NOT_RETRYABLE
328
328
329
329
if not self ._is_stats_exporter ():
330
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ( ):
331
- _track_dropped_items (self . _customer_sdkstats_metrics , envelopes , response_error .status_code )
330
+ if self ._should_collect_customer_sdkstats () and isinstance ( response_error . status_code , int ):
331
+ _track_dropped_items (envelopes , response_error .status_code )
332
332
elif _is_redirect_code (response_error .status_code ):
333
333
self ._consecutive_redirects = self ._consecutive_redirects + 1
334
334
# pylint: disable=W0212
@@ -346,18 +346,17 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
346
346
result = self ._transmit (envelopes )
347
347
else :
348
348
if not self ._is_stats_exporter ():
349
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
350
- _track_dropped_items (self . _customer_sdkstats_metrics , envelopes , DropCode .CLIENT_EXCEPTION , _exception_categories .CLIENT_EXCEPTION .value )
349
+ if self ._should_collect_customer_sdkstats ():
350
+ _track_dropped_items (envelopes , DropCode .CLIENT_EXCEPTION , _exception_categories .CLIENT_EXCEPTION .value )
351
351
logger .error (
352
352
"Error parsing redirect information." ,
353
353
)
354
354
result = ExportResult .FAILED_NOT_RETRYABLE
355
355
else :
356
356
if not self ._is_stats_exporter ():
357
357
# Track dropped items in customer sdkstats, non-retryable scenario
358
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
358
+ if self ._should_collect_customer_sdkstats ():
359
359
_track_dropped_items (
360
- self ._customer_sdkstats_metrics ,
361
360
envelopes ,
362
361
DropCode .CLIENT_EXCEPTION ,
363
362
_exception_categories .CLIENT_EXCEPTION .value
@@ -382,8 +381,8 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
382
381
response_error .message ,
383
382
)
384
383
# Track dropped items in customer sdkstats, non-retryable scenario
385
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ( ):
386
- _track_dropped_items (self . _customer_sdkstats_metrics , envelopes , response_error .status_code )
384
+ if self ._should_collect_customer_sdkstats () and isinstance ( response_error . status_code , int ):
385
+ _track_dropped_items (envelopes , response_error .status_code )
387
386
if _is_invalid_code (response_error .status_code ):
388
387
# Shutdown statsbeat on invalid code from customer endpoint
389
388
# Import here to avoid circular dependencies
@@ -405,8 +404,8 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
405
404
logger .warning ("Retrying due to server request error: %s." , request_error .message )
406
405
407
406
# Track retry items in customer sdkstats for client-side exceptions
408
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
409
- _track_retry_items (self . _customer_sdkstats_metrics , envelopes , request_error )
407
+ if self ._should_collect_customer_sdkstats ():
408
+ _track_retry_items (envelopes , request_error )
410
409
411
410
if self ._should_collect_stats ():
412
411
exc_type = request_error .exc_type
@@ -418,8 +417,8 @@ def _transmit(self, envelopes: List[TelemetryItem]) -> ExportResult:
418
417
logger .exception ("Envelopes could not be exported and are not retryable: %s." , ex ) # pylint: disable=C4769
419
418
420
419
# Track dropped items in customer sdkstats for general exceptions
421
- if self ._customer_sdkstats_metrics and self . _should_collect_customer_sdkstats ():
422
- _track_dropped_items (self . _customer_sdkstats_metrics , envelopes , DropCode .CLIENT_EXCEPTION , _exception_categories .CLIENT_EXCEPTION .value )
420
+ if self ._should_collect_customer_sdkstats ():
421
+ _track_dropped_items (envelopes , DropCode .CLIENT_EXCEPTION , _exception_categories .CLIENT_EXCEPTION .value )
423
422
424
423
if self ._should_collect_stats ():
425
424
_update_requests_map (_REQ_EXCEPTION_NAME [1 ], value = ex .__class__ .__name__ )
@@ -466,11 +465,9 @@ def _should_collect_stats(self):
466
465
467
466
# check to see whether its the case of customer sdkstats collection
468
467
def _should_collect_customer_sdkstats (self ):
469
- env_value = os .environ .get ("APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW" , "" )
470
- is_customer_sdkstats_enabled = env_value .lower () == "true"
471
468
# Don't collect customer sdkstats for instrumentation collection, sdkstats exporter or customer sdkstats exporter
472
469
return (
473
- is_customer_sdkstats_enabled
470
+ is_customer_sdkstats_enabled ()
474
471
and not get_customer_sdkstats_shutdown ()
475
472
and not self ._is_stats_exporter ()
476
473
and not self ._is_customer_sdkstats_exporter ()
0 commit comments