Skip to content

Fix Otlp*MetricExporterBuilderTests#7313

Merged
jack-berg merged 2 commits intoopen-telemetry:mainfrom
jack-berg:fix-exporter-metrics-tests
May 6, 2025
Merged

Fix Otlp*MetricExporterBuilderTests#7313
jack-berg merged 2 commits intoopen-telemetry:mainfrom
jack-berg:fix-exporter-metrics-tests

Conversation

@jack-berg
Copy link
Member

I ran the build locally for the first time after merging #7255 and noticed a variety of issues:

  • Instances where GlobalOpenTelemetry is not reset
  • Tests export real data via OTLP without setting up an OTLP receiver, causing error logs
  • Test mocks don't account for all the code paths, causing NPEs

Fixing things up here.

@jack-berg jack-berg requested a review from a team as a code owner May 1, 2025 21:05
MeterProvider meterProvider = meterProviderSupplier.get();
if (meterProvider == null) {
meterProvider = MeterProvider.noop();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is unrelated, but I noticed that we don't protect against Supplier<MeterProvider> returning null. This is one of the edge cases which nullaway doesn't catch.

void setMeterProvider() {
when(meterProvider.get(any())).thenReturn(meter);
when(meter.counterBuilder(eq("otlp.exporter.seen"))).thenReturn(counterBuilder);
when(meter.counterBuilder(any())).thenReturn(counterBuilder);
Copy link
Member Author

Choose a reason for hiding this comment

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

ExporterMetrics obtains / increments multiple counters. Only accounting for one of them was causing NPE.

// Collection before MeterProvider is initialized.
when(meterProvider.get(any())).thenReturn(MeterProvider.noop().get("test"));
exporter.export(DATA_SET);
exporter.export(DATA_SET).join(10, TimeUnit.SECONDS);
Copy link
Member Author

Choose a reason for hiding this comment

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

The test wasn't waiting for the export to finish / fail, making it difficult to track down which test was responsible.

OtlpHttpMetricExporter.builder().setMeterProvider(meterProvider).build()) {
OtlpHttpMetricExporter.builder()
.setMeterProvider(meterProvider)
.setEndpoint("http://localhost:" + server.httpPort() + "/v1/metrics")
Copy link
Member Author

Choose a reason for hiding this comment

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

Need to send the data to a real OTLP receiver.

@codecov
Copy link

codecov bot commented May 1, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 90.02%. Comparing base (29523e6) to head (9d4bde1).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
...entelemetry/exporter/internal/ExporterMetrics.java 50.00% 1 Missing and 1 partial ⚠️

❌ Your patch status has failed because the patch coverage (50.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #7313      +/-   ##
============================================
- Coverage     90.03%   90.02%   -0.01%     
  Complexity     6915     6915              
============================================
  Files           787      787              
  Lines         20864    20865       +1     
  Branches       2023     2024       +1     
============================================
- Hits          18784    18783       -1     
- Misses         1441     1442       +1     
- Partials        639      640       +1     

☔ 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.

Copy link
Member

@trask trask 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 reviewing @jaydeluca)

@jack-berg jack-berg merged commit d70fe5b into open-telemetry:main May 6, 2025
26 of 28 checks passed
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.

3 participants