File tree Expand file tree Collapse file tree 6 files changed +88
-2
lines changed
main/java/io/opentelemetry/api/baggage
test/java/io/opentelemetry/api/baggage
docs/apidiffs/current_vs_latest Expand file tree Collapse file tree 6 files changed +88
-2
lines changed Original file line number Diff line number Diff line change @@ -99,4 +99,22 @@ default boolean isEmpty() {
9999 * be set to not use an implicit parent, so any parent assignment must be done manually.
100100 */
101101 BaggageBuilder toBuilder ();
102+
103+ /**
104+ * Returns the {@code BaggageEntry} associated with the given key.
105+ *
106+ * @param entryKey entry key to return the {@code BaggageEntry} for, or {@code null} if no {@code
107+ * Entry} with the given {@code entryKey} is in this {@code Baggage}.
108+ */
109+ @ Nullable
110+ default BaggageEntry getEntry (String entryKey ) {
111+ BaggageEntry [] result = new BaggageEntry [] {null };
112+ forEach (
113+ (key , entry ) -> {
114+ if (entryKey .equals (key )) {
115+ result [0 ] = entry ;
116+ }
117+ });
118+ return result [0 ];
119+ }
102120}
Original file line number Diff line number Diff line change @@ -37,6 +37,13 @@ public String getEntryValue(String entryKey) {
3737 return entry != null ? entry .getValue () : null ;
3838 }
3939
40+ // Overrides the default implementation to provide a more performant implementation.
41+ @ Nullable
42+ @ Override
43+ public BaggageEntry getEntry (String entryKey ) {
44+ return get (entryKey );
45+ }
46+
4047 @ Override
4148 public BaggageBuilder toBuilder () {
4249 return new Builder (new ArrayList <>(data ()));
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ private enum State {
3737
3838 private boolean skipToNext ;
3939
40- public Parser (String baggageHeader ) {
40+ Parser (String baggageHeader ) {
4141 this .baggageHeader = baggageHeader ;
4242 reset (0 );
4343 }
Original file line number Diff line number Diff line change 99
1010import io .opentelemetry .context .Context ;
1111import io .opentelemetry .context .Scope ;
12+ import java .util .HashMap ;
13+ import java .util .Map ;
14+ import java .util .function .BiConsumer ;
15+ import javax .annotation .Nullable ;
1216import org .junit .jupiter .api .Test ;
1317
1418class BaggageTest {
@@ -27,4 +31,45 @@ void current() {
2731 assertThat (result .getEntryValue ("foo" )).isEqualTo ("bar" );
2832 }
2933 }
34+
35+ @ Test
36+ void getEntryDefault () {
37+ BaggageEntryMetadata metadata = BaggageEntryMetadata .create ("flib" );
38+ Map <String , BaggageEntry > result = new HashMap <>();
39+ result .put ("a" , ImmutableEntry .create ("b" , metadata ));
40+ // Implementation that only implements asMap() which is used by getEntry()
41+ Baggage baggage =
42+ new Baggage () {
43+
44+ @ Override
45+ public Map <String , BaggageEntry > asMap () {
46+ return result ;
47+ }
48+
49+ @ Override
50+ public int size () {
51+ return 0 ;
52+ }
53+
54+ @ Override
55+ public void forEach (BiConsumer <? super String , ? super BaggageEntry > consumer ) {
56+ result .forEach (consumer );
57+ }
58+
59+ @ Nullable
60+ @ Override
61+ public String getEntryValue (String entryKey ) {
62+ return null ;
63+ }
64+
65+ @ Override
66+ public BaggageBuilder toBuilder () {
67+ return null ;
68+ }
69+ };
70+
71+ BaggageEntry entry = baggage .getEntry ("a" );
72+ assertThat (entry .getValue ()).isEqualTo ("b" );
73+ assertThat (entry .getMetadata ().getValue ()).isEqualTo ("flib" );
74+ }
3075}
Original file line number Diff line number Diff line change 99import static org .assertj .core .api .Assertions .entry ;
1010
1111import com .google .common .testing .EqualsTester ;
12+ import io .opentelemetry .context .Context ;
13+ import io .opentelemetry .context .Scope ;
1214import org .junit .jupiter .api .Test ;
1315
1416/**
@@ -190,4 +192,15 @@ void testEquals() {
190192 .addEqualityGroup (baggage2 , baggage3 )
191193 .testEquals ();
192194 }
195+
196+ @ Test
197+ void getEntry () {
198+ BaggageEntryMetadata metadata = BaggageEntryMetadata .create ("flib" );
199+ try (Scope scope =
200+ Context .root ().with (Baggage .builder ().put ("a" , "b" , metadata ).build ()).makeCurrent ()) {
201+ Baggage result = Baggage .current ();
202+ assertThat (result .getEntry ("a" ).getValue ()).isEqualTo ("b" );
203+ assertThat (result .getEntry ("a" ).getMetadata ().getValue ()).isEqualTo ("flib" );
204+ }
205+ }
193206}
Original file line number Diff line number Diff line change 11Comparing source compatibility of opentelemetry-api-1.43.0-SNAPSHOT.jar against opentelemetry-api-1.42.1.jar
2- No changes.
2+ *** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.baggage.Baggage (not serializable)
3+ === CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+ +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.baggage.BaggageEntry getEntry(java.lang.String)
5+ +++ NEW ANNOTATION: javax.annotation.Nullable
You can’t perform that action at this time.
0 commit comments