-
Notifications
You must be signed in to change notification settings - Fork 916
Extends TextMapGetter with GetAll() method, implement usage in W3CBaggagePropagator #6852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jack-berg
merged 8 commits into
open-telemetry:main
from
jamesmoessis:propagator-extract-multiple-headers
Dec 5, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
79213ca
adds getList() method to TextMapGetter, with default method
jamesmoessis 46fa12d
changes W3CBaggagePropagator to handle multiple headers for baggage, …
jamesmoessis bb5f97b
fix checkstyle
jamesmoessis db8e4d4
refactors the PR so that we have a separate internal ExtendedTextMapG…
jamesmoessis 28c8491
implement jack's suggestion of moving the ExtendedTextMapGetter from …
jamesmoessis 15d429d
Change behaviour of default getAll() method, so if get() returns null…
jamesmoessis 54474fd
add tests for extracting multiple baggage headers, when some are inva…
jamesmoessis c95cec8
PR feedback
jack-berg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
43 changes: 43 additions & 0 deletions
43
...xt/src/main/java/io/opentelemetry/context/propagation/internal/ExtendedTextMapGetter.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * 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 is hence not for public use. Its APIs are unstable and can change | ||
| * at any time. | ||
| * | ||
| * @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 hidden or 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; | ||
|
|
||
| public class ExtendedTextMapGetterTest { | ||
jack-berg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.