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() {
Comment thread
NithinU2802 marked this conversation as resolved.
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