Add a method to the Baggage interface to return the current baggage in the context (#1913)
* Add a method to the Baggage interface to return the current baggage in the context. * formatting * Fix tests broken by not clearing state * don't try to code while overly tired. the result is scopes that aren't closed. * remove the scope to make sure the root scope has no baggage
This commit is contained in:
parent
9cbd5c5984
commit
ed75175578
|
@ -31,6 +31,14 @@ public interface Baggage extends ImplicitContextKeyed {
|
|||
return ImmutableBaggage.builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Baggage from the current {@link Context}, falling back to empty Baggage if none is in
|
||||
* the current Context.
|
||||
*/
|
||||
static Baggage current() {
|
||||
return BaggageUtils.getCurrentBaggage();
|
||||
}
|
||||
|
||||
@Override
|
||||
default Context storeInContext(Context context) {
|
||||
return BaggageUtils.withBaggage(this, context);
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.api.baggage;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.context.Scope;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class BaggageTest {
|
||||
@Test
|
||||
void current_empty() {
|
||||
try (Scope scope = Context.root().makeCurrent()) {
|
||||
assertThat(Baggage.current()).isEqualTo(Baggage.empty());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void current() {
|
||||
try (Scope scope =
|
||||
Context.root()
|
||||
.with(Baggage.builder().put("foo", "bar").setNoParent().build())
|
||||
.makeCurrent()) {
|
||||
Baggage result = Baggage.current();
|
||||
assertThat(result.getEntryValue("foo")).isEqualTo("bar");
|
||||
assertThat(result).isEqualTo(Baggage.builder().setNoParent().put("foo", "bar").build());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,41 +15,43 @@ class BaggageUtilsTest {
|
|||
|
||||
@Test
|
||||
void testGetCurrentBaggage_Default() {
|
||||
Baggage baggage = BaggageUtils.getCurrentBaggage();
|
||||
assertThat(baggage).isSameAs(Baggage.empty());
|
||||
try (Scope s = Context.root().makeCurrent()) {
|
||||
Baggage baggage = BaggageUtils.getCurrentBaggage();
|
||||
assertThat(baggage).isSameAs(Baggage.empty());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetCurrentBaggage_SetCorrContext() {
|
||||
Baggage baggage = Baggage.empty();
|
||||
try (Scope ignored = BaggageUtils.withBaggage(baggage, Context.current()).makeCurrent()) {
|
||||
try (Scope ignored = BaggageUtils.withBaggage(baggage, Context.root()).makeCurrent()) {
|
||||
assertThat(BaggageUtils.getCurrentBaggage()).isSameAs(baggage);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetBaggage_DefaultContext() {
|
||||
Baggage baggage = BaggageUtils.getBaggage(Context.current());
|
||||
Baggage baggage = BaggageUtils.getBaggage(Context.root());
|
||||
assertThat(baggage).isSameAs(Baggage.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetBaggage_ExplicitContext() {
|
||||
Baggage baggage = Baggage.empty();
|
||||
Context context = BaggageUtils.withBaggage(baggage, Context.current());
|
||||
Context context = BaggageUtils.withBaggage(baggage, Context.root());
|
||||
assertThat(BaggageUtils.getBaggage(context)).isSameAs(baggage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetBaggageWithoutDefault_DefaultContext() {
|
||||
Baggage baggage = BaggageUtils.getBaggageWithoutDefault(Context.current());
|
||||
Baggage baggage = BaggageUtils.getBaggageWithoutDefault(Context.root());
|
||||
assertThat(baggage).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetBaggageWithoutDefault_ExplicitContext() {
|
||||
Baggage baggage = Baggage.empty();
|
||||
Context context = BaggageUtils.withBaggage(baggage, Context.current());
|
||||
Context context = BaggageUtils.withBaggage(baggage, Context.root());
|
||||
assertThat(BaggageUtils.getBaggage(context)).isSameAs(baggage);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -96,14 +96,14 @@ class ImmutableBaggageTest {
|
|||
|
||||
@Test
|
||||
void setParent_fromContext() {
|
||||
Context context = Context.current().with(listToBaggage(T2));
|
||||
Context context = Context.root().with(listToBaggage(T2));
|
||||
Baggage baggage = Baggage.builder().setParent(context).build();
|
||||
assertThat(baggage.getEntries()).containsExactly(T2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setParent_fromEmptyContext() {
|
||||
Context emptyContext = Context.current();
|
||||
Context emptyContext = Context.root();
|
||||
Baggage parent = listToBaggage(T1);
|
||||
try (Scope scope = BaggageUtils.currentContextWith(parent)) {
|
||||
Baggage baggage = Baggage.builder().setParent(emptyContext).build();
|
||||
|
|
Loading…
Reference in New Issue