Skip to content

Commit

Permalink
Adds Baggage.getEntry(String key) (#6765)
Browse files Browse the repository at this point in the history
Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
Co-authored-by: Jack Berg <jberg@newrelic.com>
  • Loading branch information
3 people authored Oct 11, 2024
1 parent e6eceb5 commit b6badb2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
18 changes: 18 additions & 0 deletions api/all/src/main/java/io/opentelemetry/api/baggage/Baggage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private enum State {

private boolean skipToNext;

public Parser(String baggageHeader) {
Parser(String baggageHeader) {
this.baggageHeader = baggageHeader;
reset(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,4 +31,45 @@ void current() {
assertThat(result.getEntryValue("foo")).isEqualTo("bar");
}
}

@Test
void getEntryDefault() {
BaggageEntryMetadata metadata = BaggageEntryMetadata.create("flib");
Map<String, BaggageEntry> 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<String, BaggageEntry> asMap() {
return result;
}

@Override
public int size() {
return 0;
}

@Override
public void forEach(BiConsumer<? super String, ? super BaggageEntry> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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");
}
}
}
5 changes: 4 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-api.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Comparing source compatibility of opentelemetry-api-1.43.0-SNAPSHOT.jar against opentelemetry-api-1.42.1.jar
No changes.
*** 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

0 comments on commit b6badb2

Please sign in to comment.