Modify sdkstats manager#47363
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends the statsbeat system in azure-monitor-opentelemetry-exporter to allow SDKs/distros to register additional metric callbacks and have their observations included alongside the built-in statsbeat network metrics.
Changes:
- Add a callback registry and an
_iter_extra_observationshelper to safely aggregate extra observations from registered callbacks. - Add
StatsbeatManager.add_metric_callbackfor registering per-metric callbacks (idempotent per callback instance). - Update statsbeat network metric callbacks to append contributed observations, and add unit tests + changelog entry.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_utils.py | Adds callback registry + iterator helper for contributed observations. |
| sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_manager.py | Adds StatsbeatManager.add_metric_callback API for registering callbacks. |
| sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_statsbeat_metrics.py | Appends extra observations to built-in network metrics callbacks. |
| sdk/monitor/azure-monitor-opentelemetry-exporter/tests/statsbeat/test_metrics.py | Adds tests validating callback registration and observation aggregation behavior. |
| sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md | Documents the new callback capability under Features Added. |
d128d6f to
15b56f1
Compare
973cc6a to
79de717
Compare
| with self._lock: | ||
| return self._initialized | ||
|
|
||
| def add_metric_callback( |
There was a problem hiding this comment.
This will be invoked by the distro.
79de717 to
89679c5
Compare
…r own metric observations to built-in statsbeat metrics
07aa1a0 to
2bd92fa
Compare
| attributes["exceptionType"] = code | ||
| observations.append(Observation(int(count), dict(attributes))) | ||
| _REQUESTS_MAP[_REQ_EXCEPTION_NAME[1]][code] = 0 # type: ignore | ||
| observations.extend(_iter_additional_observations(_REQ_EXCEPTION_NAME[0], options)) |
There was a problem hiding this comment.
yield pattern is good in _iter_additional_observations but observations.extend materializes all yielded items anyways, effectively cancelling out the optimization. You can jus return instead of yield to be consistent.
| @@ -0,0 +1,189 @@ | |||
| ```py | |||
There was a problem hiding this comment.
I am getting some api inconsistency error in the CI/CD pipeline, trying to fix that. EDIT: So, I investigated the cause of this issue and looks like this PR - #47203 adds this new API consistency check which requires us to add the api.md and the metadata file. I believe this should be a one-time occurrence, unless the API is updated and we will have to push that change then.
|
|
||
| - Add `StatsbeatManager.add_metric_callback` to let SDKs/distros add their own metric | ||
| observations to built-in statsbeat metrics | ||
| ([#47363](https://github.com/Azure/azure-sdk-for-python/pull/47363)) |
| :rtype: bool | ||
| """ | ||
| with _ADDITIONAL_CALLBACKS_LOCK: | ||
| callbacks = _ADDITIONAL_CALLBACKS.setdefault(metric_name, []) |
There was a problem hiding this comment.
Let's add additional callbacks globals to state and have gettors / settors pattern just like the other global variables.
There was a problem hiding this comment.
We can then also make the additional callbacks as fields on the manager instead of accessing it directly from globals.
There was a problem hiding this comment.
We can then also make the additional callbacks as fields on the manager instead of accessing it directly from globals.
@lzchen What do you mean by "make additional callbacks as fields on the manager"? Could you please elaborate on that.
| iter_logger = logging.getLogger(__name__) | ||
| for cb in callbacks: | ||
| try: | ||
| yield from cb(options) |
There was a problem hiding this comment.
Is passing in options for future proofing? I don't think our internal callbacks use them right?
The corresponding PR in
microsoft-opentelemetry- microsoft/opentelemetry-distro-python#191, currently the tests fail because they reference theadd_metric_callbackmethod which has been introduced in this PR.Once the exporter is released and the latest version is pinned in the microsoft opentelemetry distro, we would be able to merge the PR.
All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines