From 8d16ab9b0196c95cfef5d298cf398c01032d2fac Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 4 Oct 2024 10:20:49 -0700 Subject: [PATCH 1/7] remove public constructor from package-private class --- .../java/io/opentelemetry/api/baggage/propagation/Parser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); } From 020016c71efe8bc870be440fd0638f0287b87d70 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 4 Oct 2024 10:29:23 -0700 Subject: [PATCH 2/7] add Baggage.getEntry --- .../java/io/opentelemetry/api/baggage/Baggage.java | 9 +++++++++ .../opentelemetry/api/baggage/ImmutableBaggage.java | 7 +++++++ .../io/opentelemetry/api/baggage/BaggageTest.java | 11 +++++++++++ 3 files changed, 27 insertions(+) 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..86fe34fd7ec 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,13 @@ 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 + BaggageEntry getEntry(String entryKey); } 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..c28c3f6cdfd 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 + @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/test/java/io/opentelemetry/api/baggage/BaggageTest.java b/api/all/src/test/java/io/opentelemetry/api/baggage/BaggageTest.java index 4c141276e1d..5ecec3eb370 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 @@ -27,4 +27,15 @@ void current() { assertThat(result.getEntryValue("foo")).isEqualTo("bar"); } } + + @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"); + } + } } From 2662a0cf3f748caa55274bc64ebc763825526bf0 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 4 Oct 2024 11:00:16 -0700 Subject: [PATCH 3/7] jApiCmp --- docs/apidiffs/current_vs_latest/opentelemetry-api.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt index 547ea83e519..97727252341 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(+) ABSTRACT(+) io.opentelemetry.api.baggage.BaggageEntry getEntry(java.lang.String) + +++ NEW ANNOTATION: javax.annotation.Nullable From 9a3763b2eb548ef4249c63f29b7968479607db20 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 4 Oct 2024 11:03:15 -0700 Subject: [PATCH 4/7] provide default implementation --- .../src/main/java/io/opentelemetry/api/baggage/Baggage.java | 4 +++- docs/apidiffs/current_vs_latest/opentelemetry-api.txt | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) 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 86fe34fd7ec..7cd829033ca 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 @@ -107,5 +107,7 @@ default boolean isEmpty() { * Entry} with the given {@code entryKey} is in this {@code Baggage}. */ @Nullable - BaggageEntry getEntry(String entryKey); + default BaggageEntry getEntry(String entryKey) { + return asMap().get(entryKey); + } } diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt index 97727252341..fb5d54460b0 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt @@ -1,5 +1,5 @@ Comparing source compatibility of opentelemetry-api-1.43.0-SNAPSHOT.jar against opentelemetry-api-1.42.1.jar -**** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.baggage.Baggage (not serializable) +*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.baggage.Baggage (not serializable) === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 - +++* NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.api.baggage.BaggageEntry getEntry(java.lang.String) + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.baggage.BaggageEntry getEntry(java.lang.String) +++ NEW ANNOTATION: javax.annotation.Nullable From 6ece3884d205ac0b2d4f4adf378c0c77b90589b1 Mon Sep 17 00:00:00 2001 From: jason plumb <75337021+breedx-splk@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:22:26 -0700 Subject: [PATCH 5/7] Update api/all/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java Co-authored-by: Trask Stalnaker --- .../java/io/opentelemetry/api/baggage/ImmutableBaggage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c28c3f6cdfd..e4183591445 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,7 +37,7 @@ public String getEntryValue(String entryKey) { return entry != null ? entry.getValue() : null; } - // Overrides the + // Overrides the default implementation to provide a more performant implementation @Nullable @Override public BaggageEntry getEntry(String entryKey) { From 1db05d0a22698ce1baf5272e2b07742f9622e780 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 4 Oct 2024 13:12:23 -0700 Subject: [PATCH 6/7] get coverage for default getEntry() method --- .../api/baggage/ImmutableBaggage.java | 2 +- .../api/baggage/BaggageTest.java | 46 ++++++++++++++++--- .../api/baggage/ImmutableBaggageTest.java | 13 ++++++ 3 files changed, 53 insertions(+), 8 deletions(-) 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 e4183591445..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,7 +37,7 @@ public String getEntryValue(String entryKey) { return entry != null ? entry.getValue() : null; } - // Overrides the default implementation to provide a more performant implementation + // Overrides the default implementation to provide a more performant implementation. @Nullable @Override public BaggageEntry getEntry(String entryKey) { 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 5ecec3eb370..054fb8c4cbb 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 { @@ -29,13 +33,41 @@ void current() { } @Test - void getEntry() { + void getEntryDefault() { 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"); - } + // Implementation that only implements asMap() which is used by getEntry() + Baggage baggage = + new Baggage() { + + @Override + public Map asMap() { + Map result = new HashMap<>(); + result.put("a", ImmutableEntry.create("b", metadata)); + return result; + } + + @Override + public int size() { + return 0; + } + + @Override + public void forEach(BiConsumer 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"); + } + } } From 5d1fd65b4be1b9f04016e280c289a1b5ae2acb26 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Fri, 11 Oct 2024 09:10:38 -0500 Subject: [PATCH 7/7] Reduce allocations for Baggage#getEntry default implementation --- .../main/java/io/opentelemetry/api/baggage/Baggage.java | 9 ++++++++- .../java/io/opentelemetry/api/baggage/BaggageTest.java | 8 +++++--- 2 files changed, 13 insertions(+), 4 deletions(-) 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 7cd829033ca..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 @@ -108,6 +108,13 @@ default boolean isEmpty() { */ @Nullable default BaggageEntry getEntry(String entryKey) { - return asMap().get(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/test/java/io/opentelemetry/api/baggage/BaggageTest.java b/api/all/src/test/java/io/opentelemetry/api/baggage/BaggageTest.java index 054fb8c4cbb..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 @@ -35,14 +35,14 @@ void current() { @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() { - Map result = new HashMap<>(); - result.put("a", ImmutableEntry.create("b", metadata)); return result; } @@ -52,7 +52,9 @@ public int size() { } @Override - public void forEach(BiConsumer consumer) {} + public void forEach(BiConsumer consumer) { + result.forEach(consumer); + } @Nullable @Override