3030)
3131from opencensus .metrics .label_key import LabelKey
3232from opencensus .metrics .label_value import LabelValue
33+ from opencensus .trace .integrations import get_integrations
3334
3435_AIMS_URI = "http://169.254.169.254/metadata/instance/compute"
3536_AIMS_API_VERSION = "api-version=2017-12-01"
4041_DEFAULT_STATS_LONG_EXPORT_INTERVAL = 86400 # 24 hours
4142
4243_ATTACH_METRIC_NAME = "Attach"
44+ _FEATURE_METRIC_NAME = "Feature"
4345_REQ_SUC_COUNT_NAME = "Request Success Count"
4446_REQ_FAIL_COUNT_NAME = "Request Failure Count"
4547_REQ_DURATION_NAME = "Request Duration"
4648_REQ_RETRY_NAME = "Request Retry Count"
4749_REQ_THROTTLE_NAME = "Request Throttle Count"
4850_REQ_EXCEPTION_NAME = "Request Exception Count"
4951
50- _RP_NAMES = ["appsvc" , "function" , "vm" , "unknown" ]
52+ _ENDPOINT_TYPES = ["breeze" ]
53+ _RP_NAMES = ["appsvc" , "functions" , "vm" , "unknown" ]
5154
5255_logger = logging .getLogger (__name__ )
5356
5457
58+ class _FEATURE_TYPES :
59+ FEATURE = 0
60+ INSTRUMENTATION = 1
61+
62+
63+ class _StatsbeatFeature :
64+ NONE = 0
65+ DISK_RETRY = 1
66+ AAD_HANDLING = 2
67+
68+
5569def _get_stats_connection_string ():
5670 cs_env = os .environ .get ("APPLICATION_INSIGHTS_STATS_CONNECTION_STRING" )
5771 if cs_env :
@@ -103,6 +117,15 @@ def _get_attach_properties():
103117
104118def _get_network_properties ():
105119 properties = _get_common_properties ()
120+ properties .append (LabelKey ("endpoint" , "ingestion endpoint type" ))
121+ properties .append (LabelKey ("host" , "destination of ingestion endpoint" ))
122+ return properties
123+
124+
125+ def _get_feature_properties ():
126+ properties = _get_common_properties ()
127+ properties .insert (4 , LabelKey ("feature" , 'represents enabled features' ))
128+ properties .insert (4 , LabelKey ("type" , 'type, either feature or instrumentation' )) # noqa: E501
106129 return properties
107130
108131
@@ -162,8 +185,12 @@ def _get_exception_count_value():
162185
163186class _StatsbeatMetrics :
164187
165- def __init__ (self , instrumentation_key ):
166- self ._instrumentation_key = instrumentation_key
188+ def __init__ (self , options ):
189+ self ._options = options
190+ self ._instrumentation_key = options .instrumentation_key
191+ self ._feature = _StatsbeatFeature .NONE
192+ if options .enable_local_storage :
193+ self ._feature |= _StatsbeatFeature .DISK_RETRY
167194 self ._stats_lock = threading .Lock ()
168195 self ._vm_data = {}
169196 self ._vm_retry = True
@@ -218,6 +245,21 @@ def __init__(self, instrumentation_key):
218245 'count' ,
219246 _get_network_properties (),
220247 )
248+ # feature/instrumentation metrics
249+ # metrics related to what features and instrumentations are enabled
250+ self ._feature_metric = LongGauge (
251+ _FEATURE_METRIC_NAME ,
252+ 'Statsbeat metric related to features enabled' , # noqa: E501
253+ 'count' ,
254+ _get_feature_properties (),
255+ )
256+ # Instrumentation metric uses same name/properties as feature
257+ self ._instrumentation_metric = LongGauge (
258+ _FEATURE_METRIC_NAME ,
259+ 'Statsbeat metric related to instrumentations enabled' , # noqa: E501
260+ 'count' ,
261+ _get_feature_properties (),
262+ )
221263
222264 # Metrics that are sent on application start
223265 def get_initial_metrics (self ):
@@ -226,6 +268,14 @@ def get_initial_metrics(self):
226268 attach_metric = self ._get_attach_metric ()
227269 if attach_metric :
228270 stats_metrics .append (attach_metric )
271+ if self ._feature_metric :
272+ feature_metric = self ._get_feature_metric ()
273+ if feature_metric :
274+ stats_metrics .append (feature_metric )
275+ if self ._instrumentation_metric :
276+ instr_metric = self ._get_instrumentation_metric ()
277+ if instr_metric :
278+ stats_metrics .append (instr_metric )
229279 return stats_metrics
230280
231281 # Metrics sent every statsbeat interval
@@ -248,6 +298,8 @@ def get_metrics(self):
248298
249299 def _get_network_metrics (self ):
250300 properties = self ._get_common_properties ()
301+ properties .append (LabelValue (_ENDPOINT_TYPES [0 ])) # endpoint
302+ properties .append (LabelValue (self ._options .endpoint )) # host
251303 metrics = []
252304 for fn , metric in self ._network_metrics .items ():
253305 # NOTE: A time series is a set of unique label values
@@ -260,6 +312,20 @@ def _get_network_metrics(self):
260312 metrics .append (stats_metric )
261313 return metrics
262314
315+ def _get_feature_metric (self ):
316+ properties = self ._get_common_properties ()
317+ properties .insert (4 , LabelValue (self ._feature )) # feature long
318+ properties .insert (4 , LabelValue (_FEATURE_TYPES .FEATURE )) # type
319+ self ._feature_metric .get_or_create_time_series (properties )
320+ return self ._feature_metric .get_metric (datetime .datetime .utcnow ())
321+
322+ def _get_instrumentation_metric (self ):
323+ properties = self ._get_common_properties ()
324+ properties .insert (4 , LabelValue (get_integrations ())) # instr long
325+ properties .insert (4 , LabelValue (_FEATURE_TYPES .INSTRUMENTATION )) # type # noqa: E501
326+ self ._instrumentation_metric .get_or_create_time_series (properties )
327+ return self ._instrumentation_metric .get_metric (datetime .datetime .utcnow ()) # noqa: E501
328+
263329 def _get_attach_metric (self ):
264330 properties = []
265331 rp = ''
@@ -303,9 +369,7 @@ def _get_common_properties(self):
303369 properties .append (LabelValue (platform .python_version ()))
304370 properties .append (LabelValue (self ._os_type or platform .system ())) # os
305371 properties .append (LabelValue ("python" )) # language
306- # version
307- properties .append (
308- LabelValue (ext_version ))
372+ properties .append (LabelValue (ext_version )) # version
309373 return properties
310374
311375 def _get_azure_compute_metadata (self ):
0 commit comments