Remove BaggageUtils (#1931)
This commit is contained in:
parent
7ceb7d0a3d
commit
e8c6b1dac8
|
|
@ -36,7 +36,7 @@ public interface Baggage extends ImplicitContextKeyed {
|
||||||
* the current Context.
|
* the current Context.
|
||||||
*/
|
*/
|
||||||
static Baggage current() {
|
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.
|
* Baggage} if there is no baggage in the context.
|
||||||
*/
|
*/
|
||||||
static Baggage fromContext(Context 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
|
@Nullable
|
||||||
static Baggage fromContextOrNull(Context context) {
|
static Baggage fromContextOrNull(Context context) {
|
||||||
return BaggageUtils.getBaggageWithoutDefault(context);
|
return context.get(ImmutableBaggage.BAGGAGE_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default Context storeInContext(Context context) {
|
default Context storeInContext(Context context) {
|
||||||
return BaggageUtils.withBaggage(this, context);
|
return context.with(ImmutableBaggage.BAGGAGE_KEY, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -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> 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() {}
|
|
||||||
}
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
package io.opentelemetry.api.baggage;
|
package io.opentelemetry.api.baggage;
|
||||||
|
|
||||||
import io.opentelemetry.context.Context;
|
import io.opentelemetry.context.Context;
|
||||||
|
import io.opentelemetry.context.ContextKey;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
@ -21,6 +22,8 @@ class ImmutableBaggage implements Baggage {
|
||||||
|
|
||||||
static final Baggage EMPTY = new ImmutableBaggage.Builder().build();
|
static final Baggage EMPTY = new ImmutableBaggage.Builder().build();
|
||||||
|
|
||||||
|
static final ContextKey<Baggage> BAGGAGE_KEY = ContextKey.named("opentelemetry-baggage-key");
|
||||||
|
|
||||||
// The types of the EntryKey and Entry must match for each entry.
|
// The types of the EntryKey and Entry must match for each entry.
|
||||||
private final Map<String, Entry> entries;
|
private final Map<String, Entry> entries;
|
||||||
@Nullable private final Baggage parent;
|
@Nullable private final Baggage parent;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import io.opentelemetry.context.Context;
|
||||||
import io.opentelemetry.context.Scope;
|
import io.opentelemetry.context.Scope;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class BaggageUtilsTest {
|
class BaggageContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetCurrentBaggage_Default() {
|
void testGetCurrentBaggage_Default() {
|
||||||
|
|
@ -24,7 +24,7 @@ class BaggageUtilsTest {
|
||||||
@Test
|
@Test
|
||||||
void testGetCurrentBaggage_SetCorrContext() {
|
void testGetCurrentBaggage_SetCorrContext() {
|
||||||
Baggage baggage = Baggage.empty();
|
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);
|
assertThat(Baggage.current()).isSameAs(baggage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +38,7 @@ class BaggageUtilsTest {
|
||||||
@Test
|
@Test
|
||||||
void testGetBaggage_ExplicitContext() {
|
void testGetBaggage_ExplicitContext() {
|
||||||
Baggage baggage = Baggage.empty();
|
Baggage baggage = Baggage.empty();
|
||||||
Context context = BaggageUtils.withBaggage(baggage, Context.root());
|
Context context = Context.root().with(baggage);
|
||||||
assertThat(Baggage.fromContext(context)).isSameAs(baggage);
|
assertThat(Baggage.fromContext(context)).isSameAs(baggage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,7 +51,7 @@ class BaggageUtilsTest {
|
||||||
@Test
|
@Test
|
||||||
void testGetBaggageWithoutDefault_ExplicitContext() {
|
void testGetBaggageWithoutDefault_ExplicitContext() {
|
||||||
Baggage baggage = Baggage.empty();
|
Baggage baggage = Baggage.empty();
|
||||||
Context context = BaggageUtils.withBaggage(baggage, Context.root());
|
Context context = Context.root().with(baggage);
|
||||||
assertThat(Baggage.fromContext(context)).isSameAs(baggage);
|
assertThat(Baggage.fromContext(context)).isSameAs(baggage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -10,10 +10,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import io.opentelemetry.context.Scope;
|
import io.opentelemetry.context.Scope;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/** Unit tests for the methods in {@link Baggage} that interact with the current {@link Baggage}. */
|
||||||
* Unit tests for the methods in {@link BaggageUtils} and {@link Baggage} that interact with the
|
|
||||||
* current {@link Baggage}.
|
|
||||||
*/
|
|
||||||
class ScopedBaggageTest {
|
class ScopedBaggageTest {
|
||||||
|
|
||||||
private static final String KEY_1 = "key 1";
|
private static final String KEY_1 = "key 1";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue