Skip to content

Add metrics for PeriodicMetricReader#8038

Open
anuraaga wants to merge 6 commits intoopen-telemetry:mainfrom
anuraaga:metric-reader-metrics
Open

Add metrics for PeriodicMetricReader#8038
anuraaga wants to merge 6 commits intoopen-telemetry:mainfrom
anuraaga:metric-reader-metrics

Conversation

@anuraaga
Copy link
Contributor

This adds the last SDK metric, for the metric reader. Been a while since previous PR since I forgot about it 😅

https://opentelemetry.io/docs/specs/semconv/otel/sdk-metrics/#metric-otelsdkmetric_readercollectionduration

@anuraaga anuraaga requested a review from a team as a code owner January 30, 2026 06:04

/**
* Sets the {@link MeterProvider} to export metrics about this {@link PeriodicMetricReader} to.
* Automatically called by the meter provide the reader is registered to.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Couldn't think of a reason to not do this

try {
metricData = collectionRegistration.collectAllMetrics();
} catch (Throwable t) {
error = t.getClass().getName();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this is actually possible in practice

Copy link
Member

Choose a reason for hiding this comment

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

It would be a programming bug

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, since we also suppress exceptions for observable instruments. In that case this is overkill so I removed it

@codecov
Copy link

codecov bot commented Jan 30, 2026

Codecov Report

❌ Patch coverage is 90.90909% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.24%. Comparing base (b56bc54) to head (bcf2b2b).
⚠️ Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
...io/opentelemetry/sdk/metrics/SdkMeterProvider.java 83.33% 2 Missing ⚠️
...dk/metrics/export/MetricReaderInstrumentation.java 88.88% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8038      +/-   ##
============================================
+ Coverage     90.16%   90.24%   +0.07%     
- Complexity     7478     7608     +130     
============================================
  Files           834      839       +5     
  Lines         22559    22846     +287     
  Branches       2239     2276      +37     
============================================
+ Hits          20341    20618     +277     
- Misses         1515     1519       +4     
- Partials        703      709       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.


private Scheduled() {}

void setMeterProvider(MeterProvider meterProvider) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
void setMeterProvider(MeterProvider meterProvider) {
private void setMeterProvider(MeterProvider meterProvider) {

}

/** Reflectively sets the meter provider for a PeriodicMetricReader to export metrics to. */
public static void setMeterProvider(
Copy link
Member

Choose a reason for hiding this comment

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

I think this is the only meter provider setter which isn't public. Any reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, conceptually it's important to "wire metrics into traces, logs", but there doesn't seem to be a need to support "wire metrics into metrics", and instead we just automatically call this internally.

There's also a different inconsistency in setMeterProvider being on the reader, not reader builder. The latter would have the same chicken-egg issue as ConfigProvider, but in a public API so even worse. So if keeping them all public, we would make setMeterProvider on the reader (not builder) public, but at that point it felt fine to just keep it private

That being said, I realized that MetricExporterBuilder does have setMeterProvider, and our code is doing the atomic reference dance to use it. I would be surprised if anyone in the wild is calling that method though since the ergonomics look poor

Copy link
Member

Choose a reason for hiding this comment

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

Could have the internal reflection API in SdkMeterProvider to avoid any public API surface area. SdkMeterProviderUtil has historically functioned as a mechanism for users to access experimental APIs which are intended to eventually be public / user facing, but not quite ready yet. With this, it seems like there is no reason for the method to ever be promoted to the public API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good call, moved it


/**
* Sets the {@link MeterProvider} to export metrics about this {@link PeriodicMetricReader} to.
* Automatically called by the meter provider the reader is registered to.
Copy link
Member

@jack-berg jack-berg Feb 5, 2026

Choose a reason for hiding this comment

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

Makes me think that SdkTracerProvider, SdkLoggerProvider should cascade set meter provider on all child components (i.e. BatchSpanProcessor, exporters, etc). Problem is, the setMeterProvider methods on child components are on the builders... Probably could have had something more elegant with some foresight. Too bad.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants