11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT License.
3- """Customer Statsbeat implementation for Azure Monitor OpenTelemetry Exporter.
3+ """Customer SDKStats implementation for Azure Monitor OpenTelemetry Exporter.
44
5- This module provides the implementation for collecting and reporting customer statsbeat
5+ This module provides the implementation for collecting and reporting Customer SDKStats
66metrics that track the usage and performance of the Azure Monitor OpenTelemetry Exporter.
77"""
88
1515from opentelemetry .sdk .metrics .export import PeriodicExportingMetricReader
1616
1717from azure .monitor .opentelemetry .exporter ._constants import (
18- _APPLICATIONINSIGHTS_STATSBEAT_ENABLED_PREVIEW ,
19- CustomerStatsbeatProperties ,
18+ _APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW ,
19+ CustomerSdkStatsProperties ,
2020 DropCode ,
2121 DropCodeType ,
2222 RetryCode ,
2323 RetryCodeType ,
24- CustomerStatsbeatMetricName ,
25- _CUSTOMER_STATSBEAT_LANGUAGE ,
24+ CustomerSdkStatsMetricName ,
25+ _CUSTOMER_SDKSTATS_LANGUAGE ,
2626)
2727
2828from azure .monitor .opentelemetry .exporter .export .metrics ._exporter import AzureMonitorMetricExporter
3838from azure .monitor .opentelemetry .exporter import VERSION
3939
4040from azure .monitor .opentelemetry .exporter .statsbeat ._state import (
41- _CUSTOMER_STATSBEAT_STATE ,
42- _CUSTOMER_STATSBEAT_STATE_LOCK ,
41+ _CUSTOMER_SDKSTATS_STATE ,
42+ _CUSTOMER_SDKSTATS_STATE_LOCK ,
4343)
4444
45- _CUSTOMER_STATSBEAT_MAP_LOCK = threading .Lock ()
46-
47- class _CustomerStatsbeatTelemetryCounters :
45+ class _CustomerSdkStatsTelemetryCounters :
4846 def __init__ (self ):
4947 self .total_item_success_count : Dict [str , Any ] = {}
5048 self .total_item_drop_count : Dict [str , Dict [DropCodeType , Dict [str , int ]]] = {}
5149 self .total_item_retry_count : Dict [str , Dict [RetryCodeType , Dict [str , int ]]] = {}
5250
53- class CustomerStatsbeatMetrics (metaclass = Singleton ): # pylint: disable=too-many-instance-attributes
51+ class CustomerSdkStatsMetrics (metaclass = Singleton ): # pylint: disable=too-many-instance-attributes
5452 def __init__ (self , connection_string ):
55- self ._counters = _CustomerStatsbeatTelemetryCounters ()
56- self ._language = _CUSTOMER_STATSBEAT_LANGUAGE
57- self ._is_enabled = os .environ .get (_APPLICATIONINSIGHTS_STATSBEAT_ENABLED_PREVIEW , "" ).lower () in ("true" )
53+ self ._counters = _CustomerSdkStatsTelemetryCounters ()
54+ self ._language = _CUSTOMER_SDKSTATS_LANGUAGE
55+ self ._is_enabled = os .environ .get (_APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW , "" ).lower () in ("true" )
5856 if not self ._is_enabled :
5957 return
6058
@@ -63,36 +61,36 @@ def __init__(self, connection_string):
6361 "instrumentation_collection" : True , # Prevent circular dependency
6462 }
6563
66- self ._customer_statsbeat_exporter = AzureMonitorMetricExporter (** exporter_config )
67- self ._customer_statsbeat_exporter . _is_customer_statsbeat = True
64+ self ._customer_sdkstats_exporter = AzureMonitorMetricExporter (** exporter_config )
65+ self ._customer_sdkstats_exporter . _is_customer_sdkstats = True
6866 metric_reader_options = {
69- "exporter" : self ._customer_statsbeat_exporter ,
67+ "exporter" : self ._customer_sdkstats_exporter ,
7068 "export_interval_millis" : _get_customer_sdkstats_export_interval ()
7169 }
72- self ._customer_statsbeat_metric_reader = PeriodicExportingMetricReader (** metric_reader_options )
73- self ._customer_statsbeat_meter_provider = MeterProvider (
74- metric_readers = [self ._customer_statsbeat_metric_reader ]
70+ self ._customer_sdkstats_metric_reader = PeriodicExportingMetricReader (** metric_reader_options )
71+ self ._customer_sdkstats_meter_provider = MeterProvider (
72+ metric_readers = [self ._customer_sdkstats_metric_reader ]
7573 )
76- self ._customer_statsbeat_meter = self ._customer_statsbeat_meter_provider .get_meter (__name__ )
74+ self ._customer_sdkstats_meter = self ._customer_sdkstats_meter_provider .get_meter (__name__ )
7775
78- self ._customer_properties = CustomerStatsbeatProperties (
76+ self ._customer_properties = CustomerSdkStatsProperties (
7977 language = self ._language ,
8078 version = VERSION ,
8179 compute_type = get_compute_type (),
8280 )
8381
84- self ._success_gauge = self ._customer_statsbeat_meter .create_observable_gauge (
85- name = CustomerStatsbeatMetricName .ITEM_SUCCESS_COUNT .value ,
82+ self ._success_gauge = self ._customer_sdkstats_meter .create_observable_gauge (
83+ name = CustomerSdkStatsMetricName .ITEM_SUCCESS_COUNT .value ,
8684 description = "Tracks successful telemetry items sent to Azure Monitor" ,
8785 callbacks = [self ._item_success_callback ]
8886 )
89- self ._dropped_gauge = self ._customer_statsbeat_meter .create_observable_gauge (
90- name = CustomerStatsbeatMetricName .ITEM_DROP_COUNT .value ,
87+ self ._dropped_gauge = self ._customer_sdkstats_meter .create_observable_gauge (
88+ name = CustomerSdkStatsMetricName .ITEM_DROP_COUNT .value ,
9189 description = "Tracks dropped telemetry items sent to Azure Monitor" ,
9290 callbacks = [self ._item_drop_callback ]
9391 )
94- self ._retry_gauge = self ._customer_statsbeat_meter .create_observable_gauge (
95- name = CustomerStatsbeatMetricName .ITEM_RETRY_COUNT .value ,
92+ self ._retry_gauge = self ._customer_sdkstats_meter .create_observable_gauge (
93+ name = CustomerSdkStatsMetricName .ITEM_RETRY_COUNT .value ,
9694 description = "Tracks retry attempts for telemetry items sent to Azure Monitor" ,
9795 callbacks = [self ._item_retry_callback ]
9896 )
@@ -236,44 +234,44 @@ def _get_retry_reason(self, retry_code: RetryCodeType, exception_message: Option
236234 }
237235 return retry_code_reasons .get (retry_code , "unknown_reason" )
238236
239- # Global customer statsbeat singleton
240- _CUSTOMER_STATSBEAT_METRICS = None
241- _CUSTOMER_STATSBEAT_LOCK = threading .Lock ()
237+ # Global customer sdkstats singleton
238+ _CUSTOMER_SDKSTATS_METRICS = None
239+ _CUSTOMER_SDKSTATS_LOCK = threading .Lock ()
242240
243241
244242# pylint: disable=global-statement
245243# pylint: disable=protected-access
246- def collect_customer_statsbeat (exporter ):
247- global _CUSTOMER_STATSBEAT_METRICS
248- # Only start customer statsbeat if did not exist before
249- if _CUSTOMER_STATSBEAT_METRICS is None :
250- with _CUSTOMER_STATSBEAT_LOCK :
244+ def collect_customer_sdkstats (exporter ):
245+ global _CUSTOMER_SDKSTATS_METRICS
246+ # Only start customer sdkstats if did not exist before
247+ if _CUSTOMER_SDKSTATS_METRICS is None :
248+ with _CUSTOMER_SDKSTATS_LOCK :
251249 # Double-check inside the lock to avoid race conditions
252- if _CUSTOMER_STATSBEAT_METRICS is None :
250+ if _CUSTOMER_SDKSTATS_METRICS is None :
253251
254252 connection_string = (
255253 f"InstrumentationKey={ exporter ._instrumentation_key } ;"
256254 f"IngestionEndpoint={ exporter ._endpoint } "
257255 )
258- _CUSTOMER_STATSBEAT_METRICS = CustomerStatsbeatMetrics (connection_string )
256+ _CUSTOMER_SDKSTATS_METRICS = CustomerSdkStatsMetrics (connection_string )
259257
260- exporter ._customer_statsbeat_metrics = _CUSTOMER_STATSBEAT_METRICS
258+ exporter ._customer_sdkstats_metrics = _CUSTOMER_SDKSTATS_METRICS
261259 if hasattr (exporter , 'storage' ) and exporter .storage :
262- exporter .storage ._customer_statsbeat_metrics = _CUSTOMER_STATSBEAT_METRICS
260+ exporter .storage ._customer_sdkstats_metrics = _CUSTOMER_SDKSTATS_METRICS
263261
264262
265- def shutdown_customer_statsbeat_metrics () -> None :
266- global _CUSTOMER_STATSBEAT_METRICS
263+ def shutdown_customer_sdkstats_metrics () -> None :
264+ global _CUSTOMER_SDKSTATS_METRICS
267265 shutdown_success = False
268- if _CUSTOMER_STATSBEAT_METRICS is not None :
269- with _CUSTOMER_STATSBEAT_LOCK :
266+ if _CUSTOMER_SDKSTATS_METRICS is not None :
267+ with _CUSTOMER_SDKSTATS_LOCK :
270268 try :
271- if _CUSTOMER_STATSBEAT_METRICS . _meter_provider is not None :
272- _CUSTOMER_STATSBEAT_METRICS . _meter_provider .shutdown ()
273- _CUSTOMER_STATSBEAT_METRICS = None
269+ if _CUSTOMER_SDKSTATS_METRICS . _customer_sdkstats_meter_provider is not None :
270+ _CUSTOMER_SDKSTATS_METRICS . _customer_sdkstats_meter_provider .shutdown ()
271+ _CUSTOMER_SDKSTATS_METRICS = None
274272 shutdown_success = True
275273 except : # pylint: disable=bare-except
276274 pass
277275 if shutdown_success :
278- with _CUSTOMER_STATSBEAT_STATE_LOCK :
279- _CUSTOMER_STATSBEAT_STATE ["SHUTDOWN" ] = True
276+ with _CUSTOMER_SDKSTATS_STATE_LOCK :
277+ _CUSTOMER_SDKSTATS_STATE ["SHUTDOWN" ] = True
0 commit comments