Skip to content

New APIs to add/remove metric readers at run-time#4863

Open
JP-MY wants to merge 7 commits into
open-telemetry:mainfrom
JP-MY:main
Open

New APIs to add/remove metric readers at run-time#4863
JP-MY wants to merge 7 commits into
open-telemetry:mainfrom
JP-MY:main

Conversation

@JP-MY
Copy link
Copy Markdown

@JP-MY JP-MY commented Dec 18, 2025

Description

This change adds two public functions to MeterProvider that allow registering and deleting metric readers at run-time.

Fixes #4818

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

  • It has been tested via new unit test added to ensure correct behavior for addition and removal of metric readers at run-time.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@JP-MY JP-MY requested a review from a team as a code owner December 18, 2025 14:20
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Dec 18, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: JP-MY / name: Mani Yazdankhah (8c6e48d)
  • ✅ login: xrmx / name: Riccardo Magliocchetti (f8415fd)

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
Copy link
Copy Markdown
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, sorry for the delay here.

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
@JP-MY JP-MY force-pushed the main branch 2 times, most recently from ef20f0e to 9bb1567 Compare February 13, 2026 07:58
@JP-MY
Copy link
Copy Markdown
Author

JP-MY commented Feb 14, 2026

@aabmass @herin049 I'm not sure why the lint is failing for protected access, I can see the same protected access in the same file. Should I add a pylint: disable= rule or is there a config for this that I need to update? also the contrib test fails don't seem related (failed to get a specific commit?) but I'm not sure if I messed something up to cause that or not.

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
Copy link
Copy Markdown
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the concurrency issues, LGTM!

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
Comment thread opentelemetry-sdk/tests/metrics/test_metrics.py
Comment thread opentelemetry-sdk/tests/metrics/test_metrics.py
@tammy-baylis-swi tammy-baylis-swi moved this to Reviewed PRs that need fixes in Python PR digest Feb 26, 2026
@tammy-baylis-swi tammy-baylis-swi moved this from Reviewed PRs that need fixes to Approved PRs that need fixes in Python PR digest Feb 26, 2026
@JP-MY JP-MY force-pushed the main branch 3 times, most recently from 8a5c6ef to 04a652a Compare March 21, 2026 12:55
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py Outdated
Copy link
Copy Markdown
Contributor

@xrmx xrmx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are introducing the last two typing errors, could you please take a look at them? The first one is the same as the second one so same fix should apply.

  /opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py:536:17 - error: Argument of type "(metric_reader: MetricReader, timeout_millis: float = 10000) -> (MetricsData | None)" cannot be assigned to parameter "func" of type "(MetricReader, AggregationTemporality) -> MetricsData" in function "_set_collect_callback"
    Type "(metric_reader: MetricReader, timeout_millis: float = 10000) -> (MetricsData | None)" is not assignable to type "(MetricReader, AggregationTemporality) -> MetricsData"
      Function return type "MetricsData | None" is incompatible with type "MetricsData"
        Type "MetricsData | None" is not assignable to type "MetricsData"
          "None" is not assignable to "MetricsData" (reportArgumentType)
  /opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py:704:17 - error: Argument of type "(metric_reader: MetricReader, timeout_millis: float = 10000) -> (MetricsData | None)" cannot be assigned to parameter "func" of type "(MetricReader, AggregationTemporality) -> MetricsData" in function "_set_collect_callback"
    Type "(metric_reader: MetricReader, timeout_millis: float = 10000) -> (MetricsData | None)" is not assignable to type "(MetricReader, AggregationTemporality) -> MetricsData"
      Function return type "MetricsData | None" is incompatible with type "MetricsData"
        Type "MetricsData | None" is not assignable to type "MetricsData"
          "None" is not assignable to "MetricsData" (reportArgumentType)
  /opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py:720:49 - error: Argument of type "None" cannot be assigned to parameter "func" of type "(MetricReader, AggregationTemporality) -> MetricsData" in function "_set_collect_callback"
    Type "None" is not assignable to type "(MetricReader, AggregationTemporality) -> MetricsData" (reportArgumentType)

You can replicate with tox -e typecheck and

diff --git a/pyproject.toml b/pyproject.toml
index b9bcd3453..951aeb598 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -129,7 +129,6 @@ include = [
 exclude = [
   "opentelemetry-sdk/tests",
   "opentelemetry-sdk/src/opentelemetry/sdk/_events",
-  "opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py",
   "opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py",
   "opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py",
   "opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py",

@github-project-automation github-project-automation Bot moved this from Approved PRs that need fixes to Approved PRs in Python PR digest Apr 21, 2026
@xrmx xrmx requested a review from herin049 April 21, 2026 08:17
@xrmx
Copy link
Copy Markdown
Contributor

xrmx commented Apr 21, 2026

@JP-MY Please add a changelog entry

@aabmass
Copy link
Copy Markdown
Member

aabmass commented Apr 23, 2026

@JP-MY we can't push to this PR unfortunately, probably because it's off of your main branch. Could you fix the merge conflict with

diff --cc opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py
index e6cb5a3a,4109e129..a3388b06
--- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py
@@@ -58,8 -58,7 +58,8 @@@ class MeasurementConsumer(ABC)
  class SynchronousMeasurementConsumer(MeasurementConsumer):
      def __init__(
          self,
-         sdk_config: "opentelemetry.sdk.metrics._internal.SdkConfiguration",
+         sdk_config: "opentelemetry.sdk.metrics._internal.sdk_configuration.SdkConfiguration",
 +        metric_readers: Iterable["opentelemetry.sdk.metrics.MetricReader"],
      ) -> None:
          self._lock = Lock()
          self._sdk_config = sdk_config

And add changelog?

@aabmass aabmass moved this from Approved PRs to Approved PRs that need fixes in Python PR digest May 7, 2026
@JP-MY
Copy link
Copy Markdown
Author

JP-MY commented May 21, 2026

Sorry I wasn't checking this PR, I will resolve the conflict and add the requested changelog today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved PRs that need fixes

Development

Successfully merging this pull request may close these issues.

Exposing public APIs to for addition and removal of metric readers at run-time

6 participants