-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extends TextMapGetter with GetAll() method, implement usage in W3CBag…
…gagePropagator (#6852) Co-authored-by: Jack Berg <jberg@newrelic.com>
- Loading branch information
1 parent
ee8d735
commit b07dab3
Showing
4 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...xt/src/main/java/io/opentelemetry/context/propagation/internal/ExtendedTextMapGetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.context.propagation.internal; | ||
|
||
import io.opentelemetry.context.propagation.TextMapGetter; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Extends {@link TextMapGetter} to return possibly multiple values for a given key. | ||
* | ||
* <p>This class is internal and experimental. Its APIs are unstable and can change at any time. Its | ||
* APIs (or a version of them) may be promoted to the public stable API in the future, but no | ||
* guarantees are made. | ||
* | ||
* @param <C> carrier of propagation fields, such as an http request. | ||
*/ | ||
public interface ExtendedTextMapGetter<C> extends TextMapGetter<C> { | ||
/** | ||
* If implemented, returns all values for a given {@code key} in order, or returns an empty list. | ||
* | ||
* <p>The default method returns the first value of the given propagation {@code key} as a | ||
* singleton list, or returns an empty list. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
* | ||
* @param carrier carrier of propagation fields, such as an http request. | ||
* @param key the key of the field. | ||
* @return all values for a given {@code key} in order, or returns an empty list. Default method | ||
* wraps {@code get()} as an {@link Iterator}. | ||
*/ | ||
default Iterator<String> getAll(@Nullable C carrier, String key) { | ||
String first = get(carrier, key); | ||
if (first == null) { | ||
return Collections.emptyIterator(); | ||
} | ||
return Collections.singleton(first).iterator(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...rc/test/java/io/opentelemetry/context/propagation/internal/ExtendedTextMapGetterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.context.propagation.internal; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ExtendedTextMapGetterTest { | ||
|
||
final ExtendedTextMapGetter<Void> nullGet = | ||
new ExtendedTextMapGetter<Void>() { | ||
@Override | ||
public Iterable<String> keys(Void carrier) { | ||
return ImmutableList.of("key"); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String get(@Nullable Void carrier, String key) { | ||
return null; | ||
} | ||
}; | ||
|
||
final ExtendedTextMapGetter<Void> nonNullGet = | ||
new ExtendedTextMapGetter<Void>() { | ||
@Override | ||
public Iterable<String> keys(Void carrier) { | ||
return ImmutableList.of("key"); | ||
} | ||
|
||
@Override | ||
public String get(@Nullable Void carrier, String key) { | ||
return "123"; | ||
} | ||
}; | ||
|
||
@Test | ||
void extendedTextMapGetterdefaultMethod_returnsEmpty() { | ||
Iterator<String> result = nullGet.getAll(null, "key"); | ||
assertThat(result).isNotNull(); | ||
List<String> values = iterToList(result); | ||
assertThat(values).isEqualTo(Collections.emptyList()); | ||
} | ||
|
||
@Test | ||
void extendedTextMapGetterdefaultMethod_returnsSingleVal() { | ||
Iterator<String> result = nonNullGet.getAll(null, "key"); | ||
assertThat(result).isNotNull(); | ||
List<String> values = iterToList(result); | ||
assertThat(values).isEqualTo(Collections.singletonList("123")); | ||
} | ||
|
||
private static <T> List<T> iterToList(Iterator<T> iter) { | ||
List<T> list = new ArrayList<>(); | ||
while (iter.hasNext()) { | ||
list.add(iter.next()); | ||
} | ||
return list; | ||
} | ||
} |