From e8c6b1dac824c84a52c956946637568b4a4dcb64 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Sat, 31 Oct 2020 03:25:12 +0900 Subject: [PATCH] Remove BaggageUtils (#1931) --- .../io/opentelemetry/api/baggage/Baggage.java | 9 +-- .../api/baggage/BaggageUtils.java | 65 ------------------- .../api/baggage/ImmutableBaggage.java | 3 + ...UtilsTest.java => BaggageContextTest.java} | 8 +-- .../api/baggage/ScopedBaggageTest.java | 5 +- 5 files changed, 13 insertions(+), 77 deletions(-) delete mode 100644 api/src/main/java/io/opentelemetry/api/baggage/BaggageUtils.java rename api/src/test/java/io/opentelemetry/api/baggage/{BaggageUtilsTest.java => BaggageContextTest.java} (83%) diff --git a/api/src/main/java/io/opentelemetry/api/baggage/Baggage.java b/api/src/main/java/io/opentelemetry/api/baggage/Baggage.java index af29fa187c..64c684a9ae 100644 --- a/api/src/main/java/io/opentelemetry/api/baggage/Baggage.java +++ b/api/src/main/java/io/opentelemetry/api/baggage/Baggage.java @@ -36,7 +36,7 @@ public interface Baggage extends ImplicitContextKeyed { * the current Context. */ static Baggage current() { - return BaggageUtils.getCurrentBaggage(); + return fromContext(Context.current()); } /** @@ -44,7 +44,8 @@ public interface Baggage extends ImplicitContextKeyed { * Baggage} if there is no baggage in the context. */ static Baggage fromContext(Context context) { - return BaggageUtils.getBaggage(context); + Baggage baggage = fromContextOrNull(context); + return baggage != null ? baggage : empty(); } /** @@ -53,12 +54,12 @@ public interface Baggage extends ImplicitContextKeyed { */ @Nullable static Baggage fromContextOrNull(Context context) { - return BaggageUtils.getBaggageWithoutDefault(context); + return context.get(ImmutableBaggage.BAGGAGE_KEY); } @Override default Context storeInContext(Context context) { - return BaggageUtils.withBaggage(this, context); + return context.with(ImmutableBaggage.BAGGAGE_KEY, this); } /** diff --git a/api/src/main/java/io/opentelemetry/api/baggage/BaggageUtils.java b/api/src/main/java/io/opentelemetry/api/baggage/BaggageUtils.java deleted file mode 100644 index 11a907681d..0000000000 --- a/api/src/main/java/io/opentelemetry/api/baggage/BaggageUtils.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.baggage; - -import io.opentelemetry.context.Context; -import io.opentelemetry.context.ContextKey; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** Utility methods for accessing the {@link Baggage} contained in the {@link Context}. */ -@Immutable -final class BaggageUtils { - private static final ContextKey BAGGAGE_KEY = - ContextKey.named("opentelemetry-baggage-key"); - - /** - * Creates a new {@code Context} with the given value set. - * - * @param baggage the value to be set. - * @param context the parent {@code Context}. - * @return a new context with the given value set. - */ - static Context withBaggage(Baggage baggage, Context context) { - return context.with(BAGGAGE_KEY, baggage); - } - - /** - * Returns the {@link Baggage} from the {@linkplain Context#current current context}, falling back - * to an empty {@link Baggage}. - * - * @return the {@link Baggage} from the {@linkplain Context#current current context}. - */ - static Baggage getCurrentBaggage() { - return getBaggage(Context.current()); - } - - /** - * Returns the {@link Baggage} from the specified {@code Context}, falling back to an empty {@link - * Baggage}. - * - * @param context the specified {@code Context}. - * @return the {@link Baggage} from the specified {@code Context}. - */ - static Baggage getBaggage(Context context) { - Baggage baggage = context.get(BAGGAGE_KEY); - return baggage == null ? Baggage.empty() : baggage; - } - - /** - * Returns the {@link Baggage} from the specified {@code Context}. If none is found, this method - * returns {code null}. - * - * @param context the specified {@code Context}. - * @return the {@link Baggage} from the specified {@code Context}. - */ - @Nullable - static Baggage getBaggageWithoutDefault(Context context) { - return context.get(BAGGAGE_KEY); - } - - private BaggageUtils() {} -} diff --git a/api/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java b/api/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java index 8809ec17f9..6aa43bf977 100644 --- a/api/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java +++ b/api/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java @@ -6,6 +6,7 @@ package io.opentelemetry.api.baggage; import io.opentelemetry.context.Context; +import io.opentelemetry.context.ContextKey; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -21,6 +22,8 @@ class ImmutableBaggage implements Baggage { static final Baggage EMPTY = new ImmutableBaggage.Builder().build(); + static final ContextKey BAGGAGE_KEY = ContextKey.named("opentelemetry-baggage-key"); + // The types of the EntryKey and Entry must match for each entry. private final Map entries; @Nullable private final Baggage parent; diff --git a/api/src/test/java/io/opentelemetry/api/baggage/BaggageUtilsTest.java b/api/src/test/java/io/opentelemetry/api/baggage/BaggageContextTest.java similarity index 83% rename from api/src/test/java/io/opentelemetry/api/baggage/BaggageUtilsTest.java rename to api/src/test/java/io/opentelemetry/api/baggage/BaggageContextTest.java index 175291007c..ec81d05de4 100644 --- a/api/src/test/java/io/opentelemetry/api/baggage/BaggageUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/api/baggage/BaggageContextTest.java @@ -11,7 +11,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import org.junit.jupiter.api.Test; -class BaggageUtilsTest { +class BaggageContextTest { @Test void testGetCurrentBaggage_Default() { @@ -24,7 +24,7 @@ class BaggageUtilsTest { @Test void testGetCurrentBaggage_SetCorrContext() { Baggage baggage = Baggage.empty(); - try (Scope ignored = BaggageUtils.withBaggage(baggage, Context.root()).makeCurrent()) { + try (Scope ignored = Context.root().with(baggage).makeCurrent()) { assertThat(Baggage.current()).isSameAs(baggage); } } @@ -38,7 +38,7 @@ class BaggageUtilsTest { @Test void testGetBaggage_ExplicitContext() { Baggage baggage = Baggage.empty(); - Context context = BaggageUtils.withBaggage(baggage, Context.root()); + Context context = Context.root().with(baggage); assertThat(Baggage.fromContext(context)).isSameAs(baggage); } @@ -51,7 +51,7 @@ class BaggageUtilsTest { @Test void testGetBaggageWithoutDefault_ExplicitContext() { Baggage baggage = Baggage.empty(); - Context context = BaggageUtils.withBaggage(baggage, Context.root()); + Context context = Context.root().with(baggage); assertThat(Baggage.fromContext(context)).isSameAs(baggage); } } diff --git a/api/src/test/java/io/opentelemetry/api/baggage/ScopedBaggageTest.java b/api/src/test/java/io/opentelemetry/api/baggage/ScopedBaggageTest.java index 47fb7d6935..41c1a7a279 100644 --- a/api/src/test/java/io/opentelemetry/api/baggage/ScopedBaggageTest.java +++ b/api/src/test/java/io/opentelemetry/api/baggage/ScopedBaggageTest.java @@ -10,10 +10,7 @@ import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.context.Scope; import org.junit.jupiter.api.Test; -/** - * Unit tests for the methods in {@link BaggageUtils} and {@link Baggage} that interact with the - * current {@link Baggage}. - */ +/** Unit tests for the methods in {@link Baggage} that interact with the current {@link Baggage}. */ class ScopedBaggageTest { private static final String KEY_1 = "key 1";