diff --git a/api/all/src/main/java/io/opentelemetry/api/baggage/Baggage.java b/api/all/src/main/java/io/opentelemetry/api/baggage/Baggage.java index 5dd2924d432..f530b790c2a 100644 --- a/api/all/src/main/java/io/opentelemetry/api/baggage/Baggage.java +++ b/api/all/src/main/java/io/opentelemetry/api/baggage/Baggage.java @@ -99,4 +99,22 @@ default boolean isEmpty() { * be set to not use an implicit parent, so any parent assignment must be done manually. */ BaggageBuilder toBuilder(); + + /** + * Returns the {@code BaggageEntry} associated with the given key. + * + * @param entryKey entry key to return the {@code BaggageEntry} for, or {@code null} if no {@code + * Entry} with the given {@code entryKey} is in this {@code Baggage}. + */ + @Nullable + default BaggageEntry getEntry(String entryKey) { + BaggageEntry[] result = new BaggageEntry[] {null}; + forEach( + (key, entry) -> { + if (entryKey.equals(key)) { + result[0] = entry; + } + }); + return result[0]; + } } diff --git a/api/all/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java b/api/all/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java index 7faf4152fe7..d628e89b282 100644 --- a/api/all/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java +++ b/api/all/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java @@ -37,6 +37,13 @@ public String getEntryValue(String entryKey) { return entry != null ? entry.getValue() : null; } + // Overrides the default implementation to provide a more performant implementation. + @Nullable + @Override + public BaggageEntry getEntry(String entryKey) { + return get(entryKey); + } + @Override public BaggageBuilder toBuilder() { return new Builder(new ArrayList<>(data())); diff --git a/api/all/src/main/java/io/opentelemetry/api/baggage/propagation/Parser.java b/api/all/src/main/java/io/opentelemetry/api/baggage/propagation/Parser.java index c9d619c1bf3..a616e8e24d2 100644 --- a/api/all/src/main/java/io/opentelemetry/api/baggage/propagation/Parser.java +++ b/api/all/src/main/java/io/opentelemetry/api/baggage/propagation/Parser.java @@ -37,7 +37,7 @@ private enum State { private boolean skipToNext; - public Parser(String baggageHeader) { + Parser(String baggageHeader) { this.baggageHeader = baggageHeader; reset(0); } diff --git a/api/all/src/test/java/io/opentelemetry/api/baggage/BaggageTest.java b/api/all/src/test/java/io/opentelemetry/api/baggage/BaggageTest.java index 4c141276e1d..f30ee5703cc 100644 --- a/api/all/src/test/java/io/opentelemetry/api/baggage/BaggageTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/baggage/BaggageTest.java @@ -9,6 +9,10 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiConsumer; +import javax.annotation.Nullable; import org.junit.jupiter.api.Test; class BaggageTest { @@ -27,4 +31,45 @@ void current() { assertThat(result.getEntryValue("foo")).isEqualTo("bar"); } } + + @Test + void getEntryDefault() { + BaggageEntryMetadata metadata = BaggageEntryMetadata.create("flib"); + Map result = new HashMap<>(); + result.put("a", ImmutableEntry.create("b", metadata)); + // Implementation that only implements asMap() which is used by getEntry() + Baggage baggage = + new Baggage() { + + @Override + public Map asMap() { + return result; + } + + @Override + public int size() { + return 0; + } + + @Override + public void forEach(BiConsumer consumer) { + result.forEach(consumer); + } + + @Nullable + @Override + public String getEntryValue(String entryKey) { + return null; + } + + @Override + public BaggageBuilder toBuilder() { + return null; + } + }; + + BaggageEntry entry = baggage.getEntry("a"); + assertThat(entry.getValue()).isEqualTo("b"); + assertThat(entry.getMetadata().getValue()).isEqualTo("flib"); + } } diff --git a/api/all/src/test/java/io/opentelemetry/api/baggage/ImmutableBaggageTest.java b/api/all/src/test/java/io/opentelemetry/api/baggage/ImmutableBaggageTest.java index 311c8bae337..a10d2ca1902 100644 --- a/api/all/src/test/java/io/opentelemetry/api/baggage/ImmutableBaggageTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/baggage/ImmutableBaggageTest.java @@ -9,6 +9,8 @@ import static org.assertj.core.api.Assertions.entry; import com.google.common.testing.EqualsTester; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; import org.junit.jupiter.api.Test; /** @@ -190,4 +192,15 @@ void testEquals() { .addEqualityGroup(baggage2, baggage3) .testEquals(); } + + @Test + void getEntry() { + BaggageEntryMetadata metadata = BaggageEntryMetadata.create("flib"); + try (Scope scope = + Context.root().with(Baggage.builder().put("a", "b", metadata).build()).makeCurrent()) { + Baggage result = Baggage.current(); + assertThat(result.getEntry("a").getValue()).isEqualTo("b"); + assertThat(result.getEntry("a").getMetadata().getValue()).isEqualTo("flib"); + } + } } diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt index 547ea83e519..fb5d54460b0 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt @@ -1,2 +1,5 @@ Comparing source compatibility of opentelemetry-api-1.43.0-SNAPSHOT.jar against opentelemetry-api-1.42.1.jar -No changes. \ No newline at end of file +*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.baggage.Baggage (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.baggage.BaggageEntry getEntry(java.lang.String) + +++ NEW ANNOTATION: javax.annotation.Nullable