Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.api.baggage;

import io.opentelemetry.common.impl.ApiUsageLogger;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ImplicitContextKeyed;
import java.util.Map;
Expand Down Expand Up @@ -52,6 +53,10 @@ static Baggage current() {
* Baggage} if there is no baggage in the context.
*/
static Baggage fromContext(Context context) {
if (context == null) {
ApiUsageLogger.logNullParam(Baggage.class, "fromContext", "context");
return empty();
}
Baggage baggage = context.get(BaggageContextKey.KEY);
return baggage != null ? baggage : empty();
}
Expand All @@ -62,6 +67,10 @@ static Baggage fromContext(Context context) {
*/
@Nullable
static Baggage fromContextOrNull(Context context) {
if (context == null) {
ApiUsageLogger.logNullParam(Baggage.class, "fromContextOrNull", "context");
return null;
}
return context.get(BaggageContextKey.KEY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@

import static org.assertj.core.api.Assertions.assertThat;

import io.github.netmikey.logunit.api.LogCapturer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.internal.testing.slf4j.SuppressLogger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;

@SuppressLogger(loggerName = "io.opentelemetry.usage")
class BaggageContextTest {

@RegisterExtension
LogCapturer logCapturer =
LogCapturer.create().captureForLogger("io.opentelemetry.usage", Level.TRACE);

@Test
void testGetCurrentBaggage_Default() {
try (Scope s = Context.root().makeCurrent()) {
Expand Down Expand Up @@ -42,6 +51,20 @@ void testGetBaggage_ExplicitContext() {
assertThat(Baggage.fromContext(context)).isSameAs(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

Baggage result = Baggage.fromContext(null);
assertThat(result).isEqualTo(Baggage.empty());
logCapturer.assertContains("context is null");
}

@Test
void fromContextOrNull_null() {
Baggage result = Baggage.fromContextOrNull(null);
assertThat(result).isNull();
logCapturer.assertContains("context is null");
}

@Test
void testGetBaggageWithoutDefault_DefaultContext() {
Baggage baggage = Baggage.fromContextOrNull(Context.root());
Expand Down
Loading