Skip to content

Commit ad9f948

Browse files
committed
Address feedback
1 parent 8413be1 commit ad9f948

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_manager.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,38 @@ def __init__(self) -> None:
162162
# Set during first initialization, preserved in shutdown for potential re-initialization
163163
self._config: Optional[StatsbeatConfig] = None # type: ignore
164164

165-
# Extra observation callbacks contributed by SDKs/distros. Keyed by built-in
166-
# statsbeat metric name. Registered directly on the singleton instance, e.g.
167-
# ``StatsbeatManager()._additional_callbacks.setdefault(name, []).append(cb)``.
165+
# Extra observation callbacks contributed by SDKs/distros.
168166
self._additional_callbacks: Dict[str, List[Callable[[CallbackOptions], Iterable[Observation]]]] = {}
169167

168+
def add_additional_metric_callbacks(
169+
self,
170+
metric_name: str,
171+
callback: Callable[[CallbackOptions], Iterable[Observation]],
172+
) -> None:
173+
"""Register additional callbacks for a built-in statsbeat metric.
174+
175+
:param metric_name: Name of the built-in statsbeat metric.
176+
:type metric_name: str
177+
:param callback: Callback that yields observations for the metric.
178+
:type callback: Callable[[~opentelemetry.metrics.CallbackOptions], Iterable[~opentelemetry.metrics.Observation]]
179+
"""
180+
callbacks = self._additional_callbacks.setdefault(metric_name, [])
181+
if callback not in callbacks:
182+
callbacks.append(callback)
183+
184+
def get_additional_metric_callbacks(
185+
self,
186+
metric_name: str,
187+
) -> Iterable[Callable[[CallbackOptions], Iterable[Observation]]]:
188+
"""Return registered callbacks for a built-in statsbeat metric.
189+
190+
:param metric_name: Name of the built-in statsbeat metric.
191+
:type metric_name: str
192+
:return: Registered callbacks for the provided metric name.
193+
:rtype: Iterable[Callable[[~opentelemetry.metrics.CallbackOptions], Iterable[~opentelemetry.metrics.Observation]]] # pylint: disable=line-too-long
194+
"""
195+
return self._additional_callbacks.get(metric_name, ())
196+
170197
@staticmethod
171198
def _validate_config(config: Optional[StatsbeatConfig]) -> bool:
172199
"""Validate that a configuration has all required fields.

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ def _get_additional_observations(metric_name: str, options: CallbackOptions) ->
172172
"""Return observations contributed by extra callbacks registered on :class:`StatsbeatManager`.
173173
174174
Invoked by the built-in ``_StatsbeatMetrics`` callbacks at collection time.
175-
Reads ``StatsbeatManager()._additional_callbacks`` (a live mutable dict on the
176-
singleton instance), which SDKs/distros populate directly. Exceptions raised by
177-
individual callbacks are caught, logged, and skipped.
175+
Reads callbacks registered on the singleton :class:`StatsbeatManager`.
176+
Exceptions raised by individual callbacks are caught, logged, and skipped.
178177
179178
:param metric_name: Name of the built-in statsbeat metric being collected.
180179
:type metric_name: str
@@ -188,7 +187,7 @@ def _get_additional_observations(metric_name: str, options: CallbackOptions) ->
188187
StatsbeatManager,
189188
)
190189

191-
callbacks = StatsbeatManager()._additional_callbacks.get(metric_name, ()) # pylint: disable=protected-access
190+
callbacks = StatsbeatManager().get_additional_metric_callbacks(metric_name)
192191

193192
observations: List[Observation] = []
194193
iter_logger = logging.getLogger(__name__)

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/statsbeat/test_metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ def test_shorten_host(self):
975975

976976
# pylint: disable=protected-access
977977
class TestAdditionalObservationCallbacks(unittest.TestCase):
978-
"""Tests for StatsbeatManager._additional_callbacks and the _get_additional_observations helper."""
978+
"""Tests for statsbeat callback registration and _get_additional_observations."""
979979

980980
def setUp(self):
981981
_REQUESTS_MAP.clear()
@@ -989,7 +989,7 @@ def tearDown(self):
989989

990990
@staticmethod
991991
def _register(metric_name, callback):
992-
StatsbeatManager()._additional_callbacks.setdefault(metric_name, []).append(callback)
992+
StatsbeatManager().add_additional_metric_callbacks(metric_name, callback)
993993

994994
def _make_metric(self):
995995
return _StatsbeatMetrics(

0 commit comments

Comments
 (0)