Skip to content

Commit d63d295

Browse files
authored
Statsbeat changes (#1100)
1 parent 5dd6f2f commit d63d295

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

Diff for: contrib/opencensus-ext-azure/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Statsbeat bug fixes, shorten host in network stats
6+
([#1100](https://github.com/census-instrumentation/opencensus-python/pull/1100))
7+
58
## 1.1.1
69
Released 2022-01-19
710

Diff for: contrib/opencensus-ext-azure/opencensus/ext/azure/common/transport.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,11 @@ def _transmit(self, envelopes):
189189
# server side error (retryable)
190190
if self._check_stats_collection():
191191
with _requests_lock:
192-
_requests_map['retry'] = _requests_map.get('retry', 0) + 1 # noqa: E501
192+
# 429 counts as throttle instead of retry
193193
if response.status_code == 429:
194194
_requests_map['throttle'] = _requests_map.get('throttle', 0) + 1 # noqa: E501
195+
else:
196+
_requests_map['retry'] = _requests_map.get('retry', 0) + 1 # noqa: E501
195197
return self.options.minimum_retry_interval
196198
# Authentication error
197199
if response.status_code == 401:

Diff for: contrib/opencensus-ext-azure/opencensus/ext/azure/metrics_exporter/statsbeat_metrics/statsbeat.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818
import os
1919
import platform
20+
import re
2021
import threading
2122

2223
import requests
@@ -30,7 +31,7 @@
3031
)
3132
from opencensus.metrics.label_key import LabelKey
3233
from opencensus.metrics.label_value import LabelValue
33-
from opencensus.trace.integrations import get_integrations
34+
from opencensus.trace.integrations import _Integrations, get_integrations
3435

3536
_AIMS_URI = "http://169.254.169.254/metadata/instance/compute"
3637
_AIMS_API_VERSION = "api-version=2017-12-01"
@@ -52,6 +53,8 @@
5253
_ENDPOINT_TYPES = ["breeze"]
5354
_RP_NAMES = ["appsvc", "functions", "vm", "unknown"]
5455

56+
_HOST_PATTERN = re.compile('^https?://(?:www\\.)?([^/.]+)')
57+
5558
_logger = logging.getLogger(__name__)
5659

5760

@@ -183,6 +186,15 @@ def _get_exception_count_value():
183186
return interval_count
184187

185188

189+
def _shorten_host(host):
190+
if not host:
191+
host = ""
192+
match = _HOST_PATTERN.match(host)
193+
if match:
194+
host = match.group(1)
195+
return host
196+
197+
186198
class _StatsbeatMetrics:
187199

188200
def __init__(self, options):
@@ -301,7 +313,8 @@ def get_metrics(self):
301313
def _get_network_metrics(self):
302314
properties = self._get_common_properties()
303315
properties.append(LabelValue(_ENDPOINT_TYPES[0])) # endpoint
304-
properties.append(LabelValue(self._options.endpoint)) # host
316+
host = _shorten_host(self._options.endpoint)
317+
properties.append(LabelValue(host)) # host
305318
metrics = []
306319
for fn, metric in self._network_metrics.items():
307320
# NOTE: A time series is a set of unique label values
@@ -315,13 +328,20 @@ def _get_network_metrics(self):
315328
return metrics
316329

317330
def _get_feature_metric(self):
331+
# Don't export if value is 0
332+
if self._feature is _StatsbeatFeature.NONE:
333+
return None
318334
properties = self._get_common_properties()
319335
properties.insert(4, LabelValue(self._feature)) # feature long
320336
properties.insert(4, LabelValue(_FEATURE_TYPES.FEATURE)) # type
321337
self._feature_metric.get_or_create_time_series(properties)
322338
return self._feature_metric.get_metric(datetime.datetime.utcnow())
323339

324340
def _get_instrumentation_metric(self):
341+
integrations = get_integrations()
342+
# Don't export if value is 0
343+
if integrations is _Integrations.NONE:
344+
return None
325345
properties = self._get_common_properties()
326346
properties.insert(4, LabelValue(get_integrations())) # instr long
327347
properties.insert(4, LabelValue(_FEATURE_TYPES.INSTRUMENTATION)) # type # noqa: E501

Diff for: contrib/opencensus-ext-azure/tests/test_azure_statsbeat_metrics.py

+42-13
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
_get_retry_count_value,
4040
_get_success_count_value,
4141
_get_throttle_count_value,
42+
_shorten_host,
4243
_StatsbeatMetrics,
4344
)
4445
from opencensus.metrics.export.gauge import (
@@ -51,7 +52,7 @@
5152
_OPTIONS = Options(
5253
instrumentation_key="ikey",
5354
enable_local_storage=True,
54-
endpoint="test-endpoint",
55+
endpoint="https://eastus-1.in.applicationinsights.azure.com/",
5556
credential=None,
5657
)
5758

@@ -348,6 +349,17 @@ def test_get_feature_metric_wtih_aad(self):
348349
self.assertEqual(
349350
properties[8].value, ext_version) # noqa: E501
350351

352+
def test_get_feature_metric_zero(self):
353+
# pylint: disable=protected-access
354+
options = Options(
355+
instrumentation_key="ikey",
356+
enable_local_storage=False,
357+
credential=None,
358+
)
359+
stats = _StatsbeatMetrics(options)
360+
metric = stats._get_feature_metric()
361+
self.assertIsNone(metric)
362+
351363
def test_get_instrumentation_metric(self):
352364
original_integrations = integrations._INTEGRATIONS_BIT_MASK
353365
integrations._INTEGRATIONS_BIT_MASK = 1024
@@ -367,6 +379,15 @@ def test_get_instrumentation_metric(self):
367379
properties[8].value, ext_version) # noqa: E501
368380
integrations._INTEGRATIONS_BIT_MASK = original_integrations
369381

382+
def test_get_instrumentation_metrics_zero(self):
383+
# pylint: disable=protected-access
384+
original_integrations = integrations._INTEGRATIONS_BIT_MASK
385+
integrations._INTEGRATIONS_BIT_MASK = 0
386+
stats = _StatsbeatMetrics(_OPTIONS)
387+
metric = stats._get_instrumentation_metric()
388+
self.assertIsNone(metric)
389+
integrations._INTEGRATIONS_BIT_MASK = original_integrations
390+
370391
@mock.patch(
371392
'opencensus.ext.azure.metrics_exporter.statsbeat_metrics.statsbeat._get_exception_count_value') # noqa: E501
372393
@mock.patch(
@@ -407,7 +428,8 @@ def test_get_network_metrics(self, mock1, mock2, mock3, mock4, mock5, mock6): #
407428
self.assertEqual(properties[5].value, "python")
408429
self.assertEqual(properties[6].value, ext_version)
409430
self.assertEqual(properties[7].value, _ENDPOINT_TYPES[0])
410-
self.assertEqual(properties[8].value, _OPTIONS.endpoint)
431+
short_host = _shorten_host(_OPTIONS.endpoint)
432+
self.assertEqual(properties[8].value, short_host)
411433

412434
@mock.patch(
413435
'opencensus.ext.azure.metrics_exporter.statsbeat_metrics.statsbeat._get_success_count_value') # noqa: E501
@@ -417,17 +439,6 @@ def test_get_network_metrics_zero(self, suc_mock):
417439
suc_mock.return_value = 0
418440
metrics = stats._get_network_metrics()
419441
self.assertEqual(len(metrics), 0)
420-
for metric in metrics:
421-
properties = metric._time_series[0]._label_values
422-
self.assertEqual(len(properties), 7)
423-
self.assertEqual(properties[0].value, _RP_NAMES[3])
424-
self.assertEqual(properties[1].value, "sdk")
425-
self.assertEqual(properties[2].value, "ikey")
426-
self.assertEqual(properties[3].value, platform.python_version())
427-
self.assertEqual(properties[4].value, platform.system())
428-
self.assertEqual(properties[5].value, "python")
429-
self.assertEqual(
430-
properties[6].value, ext_version)
431442

432443
@mock.patch.dict(
433444
os.environ,
@@ -583,3 +594,21 @@ def test_get_azure_compute_metadata__vm_retry(self):
583594
self.assertFalse(vm_result)
584595
self.assertEqual(len(stats._vm_data), 0)
585596
self.assertTrue(stats._vm_retry)
597+
598+
def test_shorten_host(self):
599+
url = "https://fakehost-1.example.com/"
600+
self.assertEqual(_shorten_host(url), "fakehost-1")
601+
url = "https://fakehost-2.example.com/"
602+
self.assertEqual(_shorten_host(url), "fakehost-2")
603+
url = "http://www.fakehost-3.example.com/"
604+
self.assertEqual(_shorten_host(url), "fakehost-3")
605+
url = "http://www.fakehost.com/v2/track"
606+
self.assertEqual(_shorten_host(url), "fakehost")
607+
url = "https://www.fakehost0-4.com/"
608+
self.assertEqual(_shorten_host(url), "fakehost0-4")
609+
url = "https://www.fakehost-5.com"
610+
self.assertEqual(_shorten_host(url), "fakehost-5")
611+
url = "https://fakehost.com"
612+
self.assertEqual(_shorten_host(url), "fakehost")
613+
url = "http://fakehost-5/"
614+
self.assertEqual(_shorten_host(url), "fakehost-5")

0 commit comments

Comments
 (0)