Skip to content

Commit b6badb2

Browse files
breedx-splktraskjack-berg
authored
Adds Baggage.getEntry(String key) (#6765)
Co-authored-by: Trask Stalnaker <[email protected]> Co-authored-by: Jack Berg <[email protected]>
1 parent e6eceb5 commit b6badb2

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

api/all/src/main/java/io/opentelemetry/api/baggage/Baggage.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

api/all/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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()));

api/all/src/main/java/io/opentelemetry/api/baggage/propagation/Parser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

api/all/src/test/java/io/opentelemetry/api/baggage/BaggageTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
import io.opentelemetry.context.Context;
1111
import 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;
1216
import org.junit.jupiter.api.Test;
1317

1418
class 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
}

api/all/src/test/java/io/opentelemetry/api/baggage/ImmutableBaggageTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import static org.assertj.core.api.Assertions.entry;
1010

1111
import com.google.common.testing.EqualsTester;
12+
import io.opentelemetry.context.Context;
13+
import io.opentelemetry.context.Scope;
1214
import 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
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
Comparing 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

0 commit comments

Comments
 (0)