Promote getAll to TextMapGetter stable API (#7267)
This commit is contained in:
parent
8a998225d0
commit
ccfcecf8fe
|
@ -16,7 +16,6 @@ import io.opentelemetry.context.Context;
|
||||||
import io.opentelemetry.context.propagation.TextMapGetter;
|
import io.opentelemetry.context.propagation.TextMapGetter;
|
||||||
import io.opentelemetry.context.propagation.TextMapPropagator;
|
import io.opentelemetry.context.propagation.TextMapPropagator;
|
||||||
import io.opentelemetry.context.propagation.TextMapSetter;
|
import io.opentelemetry.context.propagation.TextMapSetter;
|
||||||
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -97,33 +96,11 @@ public final class W3CBaggagePropagator implements TextMapPropagator {
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getter instanceof ExtendedTextMapGetter) {
|
return extractMulti(context, carrier, getter);
|
||||||
return extractMulti(context, carrier, (ExtendedTextMapGetter<C>) getter);
|
|
||||||
}
|
|
||||||
return extractSingle(context, carrier, getter);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <C> Context extractSingle(
|
|
||||||
Context context, @Nullable C carrier, TextMapGetter<C> getter) {
|
|
||||||
String baggageHeader = getter.get(carrier, FIELD);
|
|
||||||
if (baggageHeader == null) {
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
if (baggageHeader.isEmpty()) {
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
BaggageBuilder baggageBuilder = Baggage.builder();
|
|
||||||
try {
|
|
||||||
extractEntries(baggageHeader, baggageBuilder);
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
return context.with(baggageBuilder.build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <C> Context extractMulti(
|
private static <C> Context extractMulti(
|
||||||
Context context, @Nullable C carrier, ExtendedTextMapGetter<C> getter) {
|
Context context, @Nullable C carrier, TextMapGetter<C> getter) {
|
||||||
Iterator<String> baggageHeaders = getter.getAll(carrier, FIELD);
|
Iterator<String> baggageHeaders = getter.getAll(carrier, FIELD);
|
||||||
if (baggageHeaders == null) {
|
if (baggageHeaders == null) {
|
||||||
return context;
|
return context;
|
||||||
|
|
|
@ -14,7 +14,6 @@ import io.opentelemetry.api.baggage.Baggage;
|
||||||
import io.opentelemetry.api.baggage.BaggageEntryMetadata;
|
import io.opentelemetry.api.baggage.BaggageEntryMetadata;
|
||||||
import io.opentelemetry.context.Context;
|
import io.opentelemetry.context.Context;
|
||||||
import io.opentelemetry.context.propagation.TextMapGetter;
|
import io.opentelemetry.context.propagation.TextMapGetter;
|
||||||
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -40,8 +39,8 @@ class W3CBaggagePropagatorTest {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private static final ExtendedTextMapGetter<Map<String, List<String>>> multiGetter =
|
private static final TextMapGetter<Map<String, List<String>>> multiGetter =
|
||||||
new ExtendedTextMapGetter<Map<String, List<String>>>() {
|
new TextMapGetter<Map<String, List<String>>>() {
|
||||||
@Override
|
@Override
|
||||||
public Iterable<String> keys(Map<String, List<String>> carrier) {
|
public Iterable<String> keys(Map<String, List<String>> carrier) {
|
||||||
return carrier.keySet();
|
return carrier.keySet();
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
package io.opentelemetry.context.propagation;
|
package io.opentelemetry.context.propagation;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,4 +35,23 @@ public interface TextMapGetter<C> {
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
String get(@Nullable C carrier, String key);
|
String get(@Nullable C carrier, String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,39 +6,16 @@
|
||||||
package io.opentelemetry.context.propagation.internal;
|
package io.opentelemetry.context.propagation.internal;
|
||||||
|
|
||||||
import io.opentelemetry.context.propagation.TextMapGetter;
|
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.
|
* Extended {@link TextMapGetter} with experimental APIs.
|
||||||
*
|
*
|
||||||
* <p>This class is internal and experimental. Its APIs are unstable and can change at any time. Its
|
* <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
|
* APIs (or a version of them) may be promoted to the public stable API in the future, but no
|
||||||
* guarantees are made.
|
* guarantees are made.
|
||||||
*
|
|
||||||
* @param <C> carrier of propagation fields, such as an http request.
|
|
||||||
*/
|
*/
|
||||||
public interface ExtendedTextMapGetter<C> extends TextMapGetter<C> {
|
public interface ExtendedTextMapGetter<C> extends TextMapGetter<C> {
|
||||||
/**
|
|
||||||
* If implemented, returns all values for a given {@code key} in order, or returns an empty list.
|
// keep this class even if it is empty, since experimental methods may be added in the future.
|
||||||
*
|
|
||||||
* <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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,12 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.opentelemetry.context.propagation.internal;
|
package io.opentelemetry.context.propagation;
|
||||||
|
|
||||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -15,9 +16,9 @@ import java.util.List;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class ExtendedTextMapGetterTest {
|
class TextMapGetterTest {
|
||||||
|
|
||||||
final ExtendedTextMapGetter<Void> nullGet =
|
final TextMapGetter<Void> nullGet =
|
||||||
new ExtendedTextMapGetter<Void>() {
|
new ExtendedTextMapGetter<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public Iterable<String> keys(Void carrier) {
|
public Iterable<String> keys(Void carrier) {
|
||||||
|
@ -31,7 +32,7 @@ class ExtendedTextMapGetterTest {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
final ExtendedTextMapGetter<Void> nonNullGet =
|
final TextMapGetter<Void> nonNullGet =
|
||||||
new ExtendedTextMapGetter<Void>() {
|
new ExtendedTextMapGetter<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public Iterable<String> keys(Void carrier) {
|
public Iterable<String> keys(Void carrier) {
|
|
@ -1,2 +1,5 @@
|
||||||
Comparing source compatibility of opentelemetry-context-1.50.0-SNAPSHOT.jar against opentelemetry-context-1.49.0.jar
|
Comparing source compatibility of opentelemetry-context-1.50.0-SNAPSHOT.jar against opentelemetry-context-1.49.0.jar
|
||||||
No changes.
|
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.context.propagation.TextMapGetter (not serializable)
|
||||||
|
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
|
||||||
|
GENERIC TEMPLATES: === C:java.lang.Object
|
||||||
|
+++ NEW METHOD: PUBLIC(+) java.util.Iterator<java.lang.String> getAll(java.lang.Object, java.lang.String)
|
||||||
|
|
Loading…
Reference in New Issue