Visitar URL original
Add null guards 2 by jack-berg · Pull Request #8864 · open-telemetry/opentelemetry-java · GitHub
Skip to content

Add null guards 2 - #8864

Merged
jack-berg merged 2 commits into
open-telemetry:mainfrom
jack-berg:null-guard-applied-2
Oct 7, 2026
Merged

jack-berg merged 2 commits into
open-telemetry:mainfrom
jack-berg:null-guard-applied-2

Conversation

@jack-berg

Copy link
Copy Markdown
Member

Related to #8802.

See #8863 description for methodology.

  • io.opentelemetry.api.common.Attributes
  • io.opentelemetry.api.common.AttributesBuilder
  • io.opentelemetry.api.common.Value
  • io.opentelemetry.api.logs.DefaultLoggerProvider
  • io.opentelemetry.api.logs.LogRecordBuilder
  • io.opentelemetry.api.logs.Logger
  • io.opentelemetry.api.logs.LoggerProvider
  • io.opentelemetry.api.metrics.DefaultMeterProvider
  • io.opentelemetry.api.metrics.DoubleHistogramBuilder
  • io.opentelemetry.api.metrics.LongHistogramBuilder

- io.opentelemetry.api.common.Attributes
- io.opentelemetry.api.common.AttributesBuilder
- io.opentelemetry.api.common.Value
- io.opentelemetry.api.logs.DefaultLoggerProvider
- io.opentelemetry.api.logs.LogRecordBuilder
- io.opentelemetry.api.logs.Logger
- io.opentelemetry.api.logs.LoggerProvider
- io.opentelemetry.api.metrics.DefaultMeterProvider
- io.opentelemetry.api.metrics.DoubleHistogramBuilder
- io.opentelemetry.api.metrics.LongHistogramBuilder
@jack-berg
jack-berg requested a review from a team as a code owner September 25, 2026 19:30
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-10-07 16:27 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

AttributeKey<V> key3,
V value3) {
if (key1 == null) {
ApiUsageLogger.logNullParam(Attributes.class, "of", "key1");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These illustrate an interesting point: in the first pass, they checked for null, logged a warning, and returned Attributes.empty().

But attributes.empty() is a breaking behavior change and not necessary because sortAndFilterToAttributes can tolerate / filter null entries.

So while we want to return a sensible noop-ish / empty / null value, we also want to retain existing behavior.

@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 11.59420% with 183 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.55%. Comparing base (86c7423) to head (c141a22).
⚠️ Report is 27 commits behind head on main.

Files with missing lines Patch % Lines
...n/java/io/opentelemetry/api/common/Attributes.java 11.49% 39 Missing and 38 partials ⚠️
...io/opentelemetry/api/common/AttributesBuilder.java 11.36% 26 Missing and 13 partials ⚠️
...va/io/opentelemetry/api/logs/LogRecordBuilder.java 8.82% 21 Missing and 10 partials ⚠️
.../opentelemetry/api/logs/DefaultLoggerProvider.java 0.00% 6 Missing and 3 partials ⚠️
...rc/main/java/io/opentelemetry/api/logs/Logger.java 0.00% 8 Missing and 1 partial ⚠️
...pentelemetry/api/metrics/DefaultMeterProvider.java 0.00% 6 Missing and 3 partials ⚠️
...java/io/opentelemetry/api/logs/LoggerProvider.java 0.00% 2 Missing and 1 partial ⚠️
...ntelemetry/api/metrics/DoubleHistogramBuilder.java 0.00% 2 Missing and 1 partial ⚠️
...pentelemetry/api/metrics/LongHistogramBuilder.java 0.00% 3 Missing ⚠️

❌ Your patch check has failed because the patch coverage (11.59%) 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    #8864      +/-   ##
============================================
- Coverage     91.32%   90.55%   -0.78%     
- Complexity    10673    10706      +33     
============================================
  Files          1007     1007              
  Lines         28760    29118     +358     
  Branches       3684     3753      +69     
============================================
+ Hits          26266    26368     +102     
- Misses         1687     1851     +164     
- Partials        807      899      +92     

☔ View full report in Codecov by Harness.
📢 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.

assertThatThrownBy(() -> Value.of((Map<String, Value<?>>) null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("value must not be null");
assertThat(Value.of((String) null)).isEqualTo(Value.empty());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

any concern with this behavior change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good call. Yes there is concern. The loosening from throwing to returning empty isn't problematic itself, but the solution implemented here isn't actually type safe.

Value.of((String) null) is supposed to return Value<String> but actually returns Value<Empty>. In order to be type safe, we would need a type-specific empty variant for each value type. I.e. "" for string, Map.of() for map, etc, which is a questionable design.

For now, I've reverted this to the existing NPE behavior, and adjusted the NullGuardTest to special case this from requiring a RUNTIME guard (i.e. graceful degradation) to a FAIL_FAST guard (i.e. requireNonNull).

@jack-berg
jack-berg merged commit 68e7784 into open-telemetry:main Oct 7, 2026
33 of 34 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.

2 participants