Skip to content

Fix Handle null context in Baggage.fromContext() and Baggage.fromContextOrNull() - #8667

Open
NithinU2802 wants to merge 2 commits into
open-telemetry:mainfrom
NithinU2802:fix/baggage-null-context-handling
Open

Fix Handle null context in Baggage.fromContext() and Baggage.fromContextOrNull()#8667
NithinU2802 wants to merge 2 commits into
open-telemetry:mainfrom
NithinU2802:fix/baggage-null-context-handling

Conversation

@NithinU2802

Copy link
Copy Markdown
Contributor

In Baggage, to add null checks to Baggage.fromContext(context) and Baggage.fromContextOrNull(context) with log using ApiUsageLogger and for Baggage.fromContext(context) now returns empty() when the context is null, also for Baggage.fromContextOrNull(context) returns null. Additionally, added unit tests to cover these scenarios.

I'm happy to update any changes if needed.

Closes #8665

@NithinU2802
NithinU2802 requested a review from a team as a code owner July 28, 2026 11:36
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Jul 28, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-07-28 16:27 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.46%. Comparing base (ff91c02) to head (195ebde).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8667      +/-   ##
============================================
- Coverage     91.46%   91.46%   -0.01%     
- Complexity    10456    10458       +2     
============================================
  Files          1021     1021              
  Lines         27647    27653       +6     
  Branches       3242     3242              
============================================
+ Hits          25288    25293       +5     
  Misses         1616     1616              
- Partials        743      744       +1     

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

@Nullable
static Baggage fromContextOrNull(Context context) {
if (context == null) {
ApiUsageLogger.logNullParam(Context.class, "fromContextOrNull", "context");

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.

Small inconsistency here: this passes Context.class while fromContext above passes Baggage.class. Span.fromContextOrNull uses the declaring class too, and since the message is built from it, users would see a warning naming Context.fromContextOrNull, which isn't a real method on Context.

Suggested change
ApiUsageLogger.logNullParam(Context.class, "fromContextOrNull", "context");
ApiUsageLogger.logNullParam(Baggage.class, "fromContextOrNull", "context");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah, that's correct had updated from Context to Baggage.

}

@Test
void fromContext_null() {

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.

The issue lists the ApiUsageLogger output as part of the expected behavior, but the test only asserts return values. Adding a LogCapturer assertion on the api usage logger would cover it, and would have caught the Context.class / Baggage.class mismatch above. Minor: might read better split into fromContext_null and fromContextOrNull_null.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have split tests into fromContext_null and fromContextOrNull_null also added LogCapturer to verify the ApiUsageLogger output✌️

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.

LGTM, both points addressed. Needs a maintainer's sign-off though, I'm just a fellow contributor, not a code owner

@jack-berg can do the rest

…l and fromContextOrNull_null tests, and add LogCapturer assertions
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.

Fix to add null handling to Baggage.fromContext() and Baggage.fromContextOrNull()

2 participants