Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 85a3284

Browse files
authored
Fix statsbeat bugs (#1116)
1 parent 9ffa48a commit 85a3284

File tree

6 files changed

+67
-51
lines changed

6 files changed

+67
-51
lines changed

contrib/opencensus-ext-azure/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
## Unreleased
44

5-
- Statsbeat bug fixes
5+
- Statsbeat bug fixes - status codes
66
([#1113](https://github.com/census-instrumentation/opencensus-python/pull/1113))
7+
- Statsbeat bug fixes - do not log if statsbeat
8+
([#1116](https://github.com/census-instrumentation/opencensus-python/pull/1116))
79

810
## 1.1.3
911
Released 2022-03-03

contrib/opencensus-ext-azure/opencensus/ext/azure/common/transport.py

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class TransportMixin(object):
4141
def _check_stats_collection(self):
4242
return not os.environ.get("APPLICATIONINSIGHTS_STATSBEAT_DISABLED_ALL") and (not hasattr(self, '_is_stats') or not self._is_stats) # noqa: E501
4343

44+
def _is_stats_exporter(self):
45+
return hasattr(self, '_is_stats') and self._is_stats
46+
4447
def _transmit_from_storage(self):
4548
if self.storage:
4649
for blob in self.storage.gets():
@@ -88,23 +91,28 @@ def _transmit(self, envelopes):
8891
allow_redirects=False,
8992
)
9093
except requests.Timeout:
91-
logger.warning(
92-
'Request time out. Ingestion may be backed up. Retrying.')
94+
if not self._is_stats_exporter():
95+
logger.warning(
96+
'Request time out. Ingestion may be backed up. Retrying.')
9397
exception = self.options.minimum_retry_interval
9498
except requests.RequestException as ex:
95-
logger.warning(
96-
'Retrying due to transient client side error %s.', ex)
99+
if not self._is_stats_exporter():
100+
logger.warning(
101+
'Retrying due to transient client side error %s.', ex)
97102
# client side error (retryable)
98103
exception = self.options.minimum_retry_interval
99104
except CredentialUnavailableError as ex:
100-
logger.warning('Credential error. %s. Dropping telemetry.', ex)
105+
if not self._is_stats_exporter():
106+
logger.warning('Credential error. %s. Dropping telemetry.', ex)
101107
exception = -1
102108
except ClientAuthenticationError as ex:
103-
logger.warning('Authentication error %s', ex)
109+
if not self._is_stats_exporter():
110+
logger.warning('Authentication error %s', ex)
104111
exception = self.options.minimum_retry_interval
105112
except Exception as ex:
106-
logger.warning(
107-
'Error when sending request %s. Dropping telemetry.', ex)
113+
if not self._is_stats_exporter():
114+
logger.warning(
115+
'Error when sending request %s. Dropping telemetry.', ex)
108116
# Extraneous error (non-retryable)
109117
exception = -1
110118
finally:
@@ -128,7 +136,8 @@ def _transmit(self, envelopes):
128136
try:
129137
text = response.text
130138
except Exception as ex:
131-
logger.warning('Error while reading response body %s.', ex)
139+
if not self._is_stats_exporter():
140+
logger.warning('Error while reading response body %s.', ex)
132141
else:
133142
try:
134143
data = json.loads(text)
@@ -169,12 +178,13 @@ def _transmit(self, envelopes):
169178
_requests_map['retry'] = _requests_map.get('retry', 0) + 1 # noqa: E501
170179
self.storage.put(resend_envelopes)
171180
except Exception as ex:
172-
logger.error(
173-
'Error while processing %s: %s %s.',
174-
response.status_code,
175-
text,
176-
ex,
177-
)
181+
if not self._is_stats_exporter():
182+
logger.error(
183+
'Error while processing %s: %s %s.',
184+
response.status_code,
185+
text,
186+
ex,
187+
)
178188
return -response.status_code
179189
# cannot parse response body, fallback to retry
180190
if response.status_code in (
@@ -183,23 +193,25 @@ def _transmit(self, envelopes):
183193
500, # Internal Server Error
184194
503, # Service Unavailable
185195
):
186-
logger.warning(
187-
'Transient server side error %s: %s.',
188-
response.status_code,
189-
text,
190-
)
196+
if not self._is_stats_exporter():
197+
logger.warning(
198+
'Transient server side error %s: %s.',
199+
response.status_code,
200+
text,
201+
)
191202
# server side error (retryable)
192203
if self._check_stats_collection():
193204
with _requests_lock:
194205
_requests_map['retry'] = _requests_map.get('retry', 0) + 1 # noqa: E501
195206
return self.options.minimum_retry_interval
196207
# Authentication error
197208
if response.status_code == 401:
198-
logger.warning(
199-
'Authentication error %s: %s.',
200-
response.status_code,
201-
text,
202-
)
209+
if not self._is_stats_exporter():
210+
logger.warning(
211+
'Authentication error %s: %s.',
212+
response.status_code,
213+
text,
214+
)
203215
if self._check_stats_collection():
204216
with _requests_lock:
205217
_requests_map['retry'] = _requests_map.get('retry', 0) + 1 # noqa: E501
@@ -208,11 +220,12 @@ def _transmit(self, envelopes):
208220
# Can occur when v2 endpoint is used while AI resource is configured
209221
# with disableLocalAuth
210222
if response.status_code == 403:
211-
logger.warning(
212-
'Forbidden error %s: %s.',
213-
response.status_code,
214-
text,
215-
)
223+
if not self._is_stats_exporter():
224+
logger.warning(
225+
'Forbidden error %s: %s.',
226+
response.status_code,
227+
text,
228+
)
216229
if self._check_stats_collection():
217230
with _requests_lock:
218231
_requests_map['retry'] = _requests_map.get('retry', 0) + 1 # noqa: E501
@@ -230,24 +243,27 @@ def _transmit(self, envelopes):
230243
self.options.endpoint = "{}://{}".format(url.scheme, url.netloc) # noqa: E501
231244
# Attempt to export again
232245
return self._transmit(envelopes)
233-
logger.error(
234-
"Error parsing redirect information."
235-
)
246+
if not self._is_stats_exporter():
247+
logger.error(
248+
"Error parsing redirect information."
249+
)
236250
else:
237-
logger.error(
238-
"Error sending telemetry because of circular redirects."
239-
" Please check the integrity of your connection string."
240-
)
251+
if not self._is_stats_exporter():
252+
logger.error(
253+
"Error sending telemetry because of circular redirects." # noqa: E501
254+
" Please check the integrity of your connection string." # noqa: E501
255+
)
241256
# If redirect but did not return, exception occured
242257
if self._check_stats_collection():
243258
with _requests_lock:
244259
_requests_map['exception'] = _requests_map.get('exception', 0) + 1 # noqa: E501
245260
# Other, server side error (non-retryable)
246-
logger.error(
247-
'Non-retryable server side error %s: %s.',
248-
response.status_code,
249-
text,
250-
)
261+
if not self._is_stats_exporter():
262+
logger.error(
263+
'Non-retryable server side error %s: %s.',
264+
response.status_code,
265+
text,
266+
)
251267
if self._check_stats_collection():
252268
if response.status_code == 402 or response.status_code == 439:
253269
# 402: Monthly Quota Exceeded (new SDK)

contrib/opencensus-ext-azure/opencensus/ext/azure/metrics_exporter/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import atexit
16-
import logging
1716
import os
1817

1918
from opencensus.common import utils as common_utils
@@ -34,8 +33,6 @@
3433

3534
__all__ = ['MetricsExporter', 'new_metrics_exporter']
3635

37-
logger = logging.getLogger(__name__)
38-
3936

4037
class MetricsExporter(TransportMixin, ProcessorMixin):
4138
"""Metrics exporter for Microsoft Azure Monitor."""

contrib/opencensus-ext-azure/opencensus/ext/azure/metrics_exporter/statsbeat_metrics/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from opencensus.metrics import transport
2424
from opencensus.metrics.export.metric_producer import MetricProducer
25+
from opencensus.trace import execution_context
2526

2627
_STATSBEAT_METRICS = None
2728
_STATSBEAT_EXPORTER = None
@@ -46,7 +47,9 @@ def collect_statsbeat_metrics(options):
4647
producer = _AzureStatsbeatMetricsProducer(options)
4748
_STATSBEAT_METRICS = producer
4849
# Export some initial stats on program start
50+
execution_context.set_is_exporter(True)
4951
exporter.export_metrics(_STATSBEAT_METRICS.get_initial_metrics())
52+
execution_context.set_is_exporter(False)
5053
exporter.exporter_thread = \
5154
transport.get_exporter_thread([_STATSBEAT_METRICS],
5255
exporter,

contrib/opencensus-ext-azure/opencensus/ext/azure/metrics_exporter/statsbeat_metrics/statsbeat.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import datetime
1616
import json
17-
import logging
1817
import os
1918
import platform
2019
import re
@@ -68,8 +67,6 @@
6867

6968
_HOST_PATTERN = re.compile('^https?://(?:www\\.)?([^/.]+)')
7069

71-
_logger = logging.getLogger(__name__)
72-
7370

7471
class _FEATURE_TYPES:
7572
FEATURE = 0
@@ -315,8 +312,8 @@ def get_metrics(self):
315312
self._long_threshold_count = 0
316313
network_metrics = self._get_network_metrics()
317314
metrics.extend(network_metrics)
318-
except Exception as ex:
319-
_logger.warning('Error while exporting stats metrics %s.', ex)
315+
except Exception:
316+
pass
320317

321318
return metrics
322319

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ deps =
3939
unit,lint,bandit: -e contrib/opencensus-ext-mysql
4040
unit,lint,bandit: -e contrib/opencensus-ext-ocagent
4141
unit,lint,bandit: -e contrib/opencensus-ext-postgresql
42+
py{36,37,38,39}-unit,lint,bandit: prometheus-client==0.13.1
4243
unit,lint,bandit: -e contrib/opencensus-ext-prometheus
4344
unit,lint,bandit: -e contrib/opencensus-ext-pymongo
4445
unit,lint,bandit: -e contrib/opencensus-ext-pymysql

0 commit comments

Comments
 (0)