Repository navigation
fix(sdk): wire MeterProvider in add_metric_reader and allocate reader component ID once - #5897
Conversation
… component ID once
Pull request dashboard statusWaiting on reviewers · refreshed 2026-11-08 04:00 UTC Review the latest changes. Status above doesn't look right?
|
|
Hi @herin049, could you please take a look at this PR when you have some time? This addresses defects 1 and 2 from #5890:
(Note: defect 3 regarding Regression tests (with proper cleanup isolation) and Towncrier changelog fragment are included, and EasyCLA is signed. Thanks! |
|
Took a look through the changes in opentelemetry-sdk. Calling metric_reader._set_meter_provider(self) directly in MeterProvider.add_metric_reader() makes sense and brings dynamically added readers to parity with readers supplied at provider initialization. Initializing self._metrics as NoOpMetricReaderMetrics() and having create_metric_reader_metrics return early when passed NoOpMeterProvider cleanly avoids the double-increment bug on _component_counter. Allocating self._otel_component_id once and reusing it across re-bindings keeps the naming predictable (/0 for the first instance). The regression tests in est_in_memory_metric_reader.py and est_periodic_exporting_metric_reader.py verify both the initial state and dynamic registration cleanly, and using self.addCleanup for reader and provider shutdowns ensures tests stay properly isolated. Looks solid to me! |
|
Thanks for taking the time to go through this and for the detailed feedback! Glad the approach and the tests look good. I wanted to make sure dynamically added readers behave consistently with readers passed during provider initialization, without introducing any extra component ID increments. Appreciate you taking a look! @knowprasad |
Description
Fixes defects 1 and 2 of #5890. (Note: defect 3 is excluded as it is actively being addressed in open PR #5778).
MeterProvider.add_metric_reader()now callsmetric_reader._set_meter_provider(self):Previously,
add_metric_reader()calledmetric_reader._set_collect_callback(...)but never calledmetric_reader._set_meter_provider(self), leavingmetric_reader._metricsbound toNoOpMeterProvider()and failing to produce reader collection duration self-observability metrics (otel.sdk.metric_reader.collection.duration).Allocates
MetricReadercomponent ID once per instance:Previously,
MetricReader.__init__calledcreate_metric_reader_metrics(..., NoOpMeterProvider(), ...), which prematurely incremented_component_counter[component_type]from0to1with a no-op provider. WhenMeterProvidersubsequently calledmetric_reader._set_meter_provider(self),create_metric_reader_metricswas called a second time, incrementing the counter to2and naming the first instance<type>/1.Now,
MetricReader.__init__initializesself._metricsasNoOpMetricReaderMetrics(),create_metric_reader_metricsreturnsNoOpMetricReaderMetrics()when passed aNoOpMeterProvider, andMetricReaderallocates and storesself._otel_component_idonce upon registration (<type>/0for the first instance).Related Issues
error.typefor failed metric reader collections #5778Type of Change