Skip to content

Commit ccfcecf

Browse files
authored
Promote getAll to TextMapGetter stable API (#7267)
1 parent 8a99822 commit ccfcecf

File tree

6 files changed

+38
-60
lines changed

6 files changed

+38
-60
lines changed

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io.opentelemetry.context.propagation.TextMapGetter;
1717
import io.opentelemetry.context.propagation.TextMapPropagator;
1818
import io.opentelemetry.context.propagation.TextMapSetter;
19-
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
2019
import java.util.Collection;
2120
import java.util.Iterator;
2221
import java.util.List;
@@ -97,33 +96,11 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
9796
return context;
9897
}
9998

100-
if (getter instanceof ExtendedTextMapGetter) {
101-
return extractMulti(context, carrier, (ExtendedTextMapGetter<C>) getter);
102-
}
103-
return extractSingle(context, carrier, getter);
104-
}
105-
106-
private static <C> Context extractSingle(
107-
Context context, @Nullable C carrier, TextMapGetter<C> getter) {
108-
String baggageHeader = getter.get(carrier, FIELD);
109-
if (baggageHeader == null) {
110-
return context;
111-
}
112-
if (baggageHeader.isEmpty()) {
113-
return context;
114-
}
115-
116-
BaggageBuilder baggageBuilder = Baggage.builder();
117-
try {
118-
extractEntries(baggageHeader, baggageBuilder);
119-
} catch (RuntimeException e) {
120-
return context;
121-
}
122-
return context.with(baggageBuilder.build());
99+
return extractMulti(context, carrier, getter);
123100
}
124101

125102
private static <C> Context extractMulti(
126-
Context context, @Nullable C carrier, ExtendedTextMapGetter<C> getter) {
103+
Context context, @Nullable C carrier, TextMapGetter<C> getter) {
127104
Iterator<String> baggageHeaders = getter.getAll(carrier, FIELD);
128105
if (baggageHeaders == null) {
129106
return context;

api/all/src/test/java/io/opentelemetry/api/baggage/propagation/W3CBaggagePropagatorTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import io.opentelemetry.api.baggage.BaggageEntryMetadata;
1515
import io.opentelemetry.context.Context;
1616
import io.opentelemetry.context.propagation.TextMapGetter;
17-
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
1817
import java.util.Collections;
1918
import java.util.HashMap;
2019
import java.util.Iterator;
@@ -40,8 +39,8 @@ public String get(Map<String, String> carrier, String key) {
4039
}
4140
};
4241

43-
private static final ExtendedTextMapGetter<Map<String, List<String>>> multiGetter =
44-
new ExtendedTextMapGetter<Map<String, List<String>>>() {
42+
private static final TextMapGetter<Map<String, List<String>>> multiGetter =
43+
new TextMapGetter<Map<String, List<String>>>() {
4544
@Override
4645
public Iterable<String> keys(Map<String, List<String>> carrier) {
4746
return carrier.keySet();

context/src/main/java/io/opentelemetry/context/propagation/TextMapGetter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.context.propagation;
77

8+
import java.util.Collections;
9+
import java.util.Iterator;
810
import javax.annotation.Nullable;
911

1012
/**
@@ -33,4 +35,23 @@ public interface TextMapGetter<C> {
3335
*/
3436
@Nullable
3537
String get(@Nullable C carrier, String key);
38+
39+
/**
40+
* If implemented, returns all values for a given {@code key} in order, or returns an empty list.
41+
*
42+
* <p>The default method returns the first value of the given propagation {@code key} as a
43+
* singleton list, or returns an empty list.
44+
*
45+
* @param carrier carrier of propagation fields, such as an http request.
46+
* @param key the key of the field.
47+
* @return all values for a given {@code key} in order, or returns an empty list. Default method
48+
* wraps {@code get()} as an {@link Iterator}.
49+
*/
50+
default Iterator<String> getAll(@Nullable C carrier, String key) {
51+
String first = get(carrier, key);
52+
if (first == null) {
53+
return Collections.emptyIterator();
54+
}
55+
return Collections.singleton(first).iterator();
56+
}
3657
}

context/src/main/java/io/opentelemetry/context/propagation/internal/ExtendedTextMapGetter.java

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,16 @@
66
package io.opentelemetry.context.propagation.internal;
77

88
import io.opentelemetry.context.propagation.TextMapGetter;
9-
import java.util.Collections;
10-
import java.util.Iterator;
11-
import javax.annotation.Nullable;
129

1310
/**
14-
* Extends {@link TextMapGetter} to return possibly multiple values for a given key.
11+
* Extended {@link TextMapGetter} with experimental APIs.
1512
*
1613
* <p>This class is internal and experimental. Its APIs are unstable and can change at any time. Its
1714
* APIs (or a version of them) may be promoted to the public stable API in the future, but no
1815
* guarantees are made.
19-
*
20-
* @param <C> carrier of propagation fields, such as an http request.
2116
*/
2217
public interface ExtendedTextMapGetter<C> extends TextMapGetter<C> {
23-
/**
24-
* If implemented, returns all values for a given {@code key} in order, or returns an empty list.
25-
*
26-
* <p>The default method returns the first value of the given propagation {@code key} as a
27-
* singleton list, or returns an empty list.
28-
*
29-
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
30-
* at any time.
31-
*
32-
* @param carrier carrier of propagation fields, such as an http request.
33-
* @param key the key of the field.
34-
* @return all values for a given {@code key} in order, or returns an empty list. Default method
35-
* wraps {@code get()} as an {@link Iterator}.
36-
*/
37-
default Iterator<String> getAll(@Nullable C carrier, String key) {
38-
String first = get(carrier, key);
39-
if (first == null) {
40-
return Collections.emptyIterator();
41-
}
42-
return Collections.singleton(first).iterator();
43-
}
18+
19+
// keep this class even if it is empty, since experimental methods may be added in the future.
20+
4421
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.context.propagation.internal;
6+
package io.opentelemetry.context.propagation;
77

88
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
99

1010
import com.google.common.collect.ImmutableList;
11+
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
1112
import java.util.ArrayList;
1213
import java.util.Collections;
1314
import java.util.Iterator;
1415
import java.util.List;
1516
import javax.annotation.Nullable;
1617
import org.junit.jupiter.api.Test;
1718

18-
class ExtendedTextMapGetterTest {
19+
class TextMapGetterTest {
1920

20-
final ExtendedTextMapGetter<Void> nullGet =
21+
final TextMapGetter<Void> nullGet =
2122
new ExtendedTextMapGetter<Void>() {
2223
@Override
2324
public Iterable<String> keys(Void carrier) {
@@ -31,7 +32,7 @@ public String get(@Nullable Void carrier, String key) {
3132
}
3233
};
3334

34-
final ExtendedTextMapGetter<Void> nonNullGet =
35+
final TextMapGetter<Void> nonNullGet =
3536
new ExtendedTextMapGetter<Void>() {
3637
@Override
3738
public Iterable<String> keys(Void carrier) {
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
Comparing source compatibility of opentelemetry-context-1.50.0-SNAPSHOT.jar against opentelemetry-context-1.49.0.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.context.propagation.TextMapGetter (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
GENERIC TEMPLATES: === C:java.lang.Object
5+
+++ NEW METHOD: PUBLIC(+) java.util.Iterator<java.lang.String> getAll(java.lang.Object, java.lang.String)

0 commit comments

Comments
 (0)