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

Commit 6e522f7

Browse files
authored
Implement attach rate statsbeat metrics (#1053)
1 parent 1ca6015 commit 6e522f7

File tree

12 files changed

+558
-506
lines changed

12 files changed

+558
-506
lines changed

contrib/opencensus-ext-azure/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Enable AAD authorization via TokenCredential
66
([#1021](https://github.com/census-instrumentation/opencensus-python/pull/1021))
7+
- Implement attach rate metrics via Statbeat
8+
([#1053](https://github.com/census-instrumentation/opencensus-python/pull/1053))
79

810
## 1.0.8
911
Released 2021-05-13

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ def __init__(self, *args, **kwargs):
108108

109109
_default = BaseObject(
110110
connection_string=None,
111-
credential=None,
111+
credential=None, # Credential class used by AAD auth
112112
enable_local_storage=True,
113-
enable_standard_metrics=True,
113+
enable_standard_metrics=True, # Used by metrics exporter, True to send standard metrics # noqa: E501
114114
endpoint='https://dc.services.visualstudio.com/v2/track',
115115
export_interval=15.0,
116116
grace_period=5.0,
117117
instrumentation_key=None,
118-
logging_sampling_rate=1.0,
118+
logging_sampling_rate=1.0, # Used by log exporter, controls sampling
119119
max_batch_size=100,
120120
minimum_retry_interval=60, # minimum retry interval in seconds
121121
proxies=None, # string maps url schemes to the url of the proxies

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
)
3131
from opencensus.ext.azure.common.storage import LocalFileStorage
3232
from opencensus.ext.azure.common.transport import TransportMixin
33+
from opencensus.ext.azure.metrics_exporter import statsbeat_metrics
3334
from opencensus.trace import execution_context
3435

3536
logger = logging.getLogger(__name__)
@@ -61,6 +62,10 @@ def __init__(self, **options):
6162
self._queue = Queue(capacity=self.options.queue_capacity)
6263
self._worker = Worker(self._queue, self)
6364
self._worker.start()
65+
# start statsbeat on exporter instantiation
66+
statsbeat_metrics.collect_statsbeat_metrics(
67+
self.options.instrumentation_key
68+
)
6469

6570
def _export(self, batch, event=None): # pragma: NO COVER
6671
try:

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
class MetricsExporter(TransportMixin, ProcessorMixin):
4040
"""Metrics exporter for Microsoft Azure Monitor."""
4141

42-
def __init__(self, **options):
42+
def __init__(self, is_stats=False, **options):
4343
self.options = Options(**options)
44+
self._is_stats = is_stats
4445
utils.validate_instrumentation_key(self.options.instrumentation_key)
4546
if self.options.max_batch_size <= 0:
4647
raise ValueError('Max batch size must be at least 1.')
@@ -131,7 +132,10 @@ def _create_envelope(self, data_point, timestamp, properties):
131132
tags=dict(utils.azure_monitor_context),
132133
time=timestamp.isoformat(),
133134
)
134-
envelope.name = "Microsoft.ApplicationInsights.Metric"
135+
if self._is_stats:
136+
envelope.name = "Statsbeat"
137+
else:
138+
envelope.name = "Microsoft.ApplicationInsights.Metric"
135139
data = MetricData(
136140
metrics=[data_point],
137141
properties=properties
@@ -157,4 +161,9 @@ def new_metrics_exporter(**options):
157161
producers,
158162
exporter,
159163
interval=exporter.options.export_interval)
164+
from opencensus.ext.azure.metrics_exporter import statsbeat_metrics
165+
# Stats will track the user's ikey
166+
statsbeat_metrics.collect_statsbeat_metrics(
167+
exporter.options.instrumentation_key
168+
)
160169
return exporter

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

contrib/opencensus-ext-azure/opencensus/ext/azure/metrics_exporter/heartbeat_metrics/heartbeat.py

Lines changed: 0 additions & 132 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2020, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import threading
16+
17+
from opencensus.ext.azure.metrics_exporter import MetricsExporter
18+
from opencensus.ext.azure.metrics_exporter.statsbeat_metrics.statsbeat import (
19+
_STATS_CONNECTION_STRING,
20+
_STATS_SHORT_EXPORT_INTERVAL,
21+
_StatsbeatMetrics,
22+
)
23+
from opencensus.metrics import transport
24+
from opencensus.metrics.export.metric_producer import MetricProducer
25+
26+
_STATSBEAT_METRICS = None
27+
_STATSBEAT_LOCK = threading.Lock()
28+
29+
30+
def collect_statsbeat_metrics(ikey):
31+
with _STATSBEAT_LOCK:
32+
# Only start statsbeat if did not exist before
33+
global _STATSBEAT_METRICS # pylint: disable=global-statement
34+
if _STATSBEAT_METRICS is None:
35+
exporter = MetricsExporter(
36+
is_stats=True,
37+
connection_string=_STATS_CONNECTION_STRING,
38+
export_interval=_STATS_SHORT_EXPORT_INTERVAL, # 15 minutes by default # noqa: E501
39+
)
40+
# The user's ikey is the one being tracked
41+
producer = _AzureStatsbeatMetricsProducer(
42+
instrumentation_key=ikey
43+
)
44+
_STATSBEAT_METRICS = producer
45+
exporter.exporter_thread = \
46+
transport.get_exporter_thread([_STATSBEAT_METRICS],
47+
exporter,
48+
exporter.options.export_interval)
49+
50+
51+
class _AzureStatsbeatMetricsProducer(MetricProducer):
52+
"""Implementation of the producer of statsbeat metrics.
53+
54+
Includes Azure attach rate metrics, implemented using gauges.
55+
"""
56+
def __init__(self, instrumentation_key):
57+
self._statsbeat = _StatsbeatMetrics(instrumentation_key)
58+
59+
def get_metrics(self):
60+
return self._statsbeat.get_metrics()

0 commit comments

Comments
 (0)