Skip to content

Commit 0b8131c

Browse files
author
Anuraag Agrawal
authored
Handle nulls in noop / multi (#2939)
* Handle nulls in noop / multi * Remove old file
1 parent bd6cfd8 commit 0b8131c

File tree

4 files changed

+81
-29
lines changed

4 files changed

+81
-29
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ private static List<String> getAllFields(TextMapPropagator[] textPropagators) {
4545

4646
@Override
4747
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
48+
if (context == null || setter == null) {
49+
return;
50+
}
4851
for (TextMapPropagator textPropagator : textPropagators) {
4952
textPropagator.inject(context, carrier, setter);
5053
}
5154
}
5255

5356
@Override
5457
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
58+
if (context == null) {
59+
return Context.root();
60+
}
61+
if (getter == null) {
62+
return context;
63+
}
5564
for (TextMapPropagator textPropagator : textPropagators) {
5665
context = textPropagator.extract(context, carrier, getter);
5766
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> se
2727

2828
@Override
2929
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
30+
if (context == null) {
31+
return Context.root();
32+
}
3033
return context;
3134
}
3235
}

context/src/test/java/io/opentelemetry/context/propagation/MultiTextMapPropagatorTest.java

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
import static org.mockito.Mockito.when;
1313

1414
import io.opentelemetry.context.Context;
15+
import io.opentelemetry.context.ContextKey;
1516
import java.util.Arrays;
1617
import java.util.Collection;
18+
import java.util.Collections;
1719
import java.util.HashMap;
20+
import java.util.LinkedHashMap;
1821
import java.util.List;
1922
import java.util.Map;
2023
import javax.annotation.Nullable;
@@ -26,6 +29,8 @@
2629
@ExtendWith(MockitoExtension.class)
2730
class MultiTextMapPropagatorTest {
2831

32+
private static final ContextKey<String> KEY = ContextKey.named("key");
33+
2934
@Mock private TextMapPropagator propagator1;
3035
@Mock private TextMapPropagator propagator2;
3136
@Mock private TextMapPropagator propagator3;
@@ -93,19 +98,6 @@ void inject_allDelegated() {
9398
verify(propagator3).inject(context, carrier, setter);
9499
}
95100

96-
@Test
97-
void inject_nullContextAllDelegated() {
98-
Map<String, String> carrier = new HashMap<>();
99-
Context context = null;
100-
TextMapSetter<Map<String, String>> setter = Map::put;
101-
102-
TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2, propagator3);
103-
prop.inject(context, carrier, setter);
104-
verify(propagator1).inject(context, carrier, setter);
105-
verify(propagator2).inject(context, carrier, setter);
106-
verify(propagator3).inject(context, carrier, setter);
107-
}
108-
109101
@Test
110102
void extract_noPropagators() {
111103
Map<String, String> carrier = new HashMap<>();
@@ -147,14 +139,33 @@ void extract_notFound() {
147139

148140
@Test
149141
void extract_nullContext() {
150-
Map<String, String> carrier = new HashMap<>();
151-
Context context = null;
152-
when(propagator1.extract(context, carrier, getter)).thenReturn(context);
153-
when(propagator2.extract(context, carrier, getter)).thenReturn(context);
142+
assertThat(
143+
new MultiTextMapPropagator(propagator1, propagator2)
144+
.extract(null, Collections.emptyMap(), getter))
145+
.isSameAs(Context.root());
146+
}
154147

155-
TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2);
156-
Context result = prop.extract(context, carrier, getter);
148+
@Test
149+
void extract_nullGetter() {
150+
Context context = Context.current().with(KEY, "treasure");
151+
assertThat(
152+
new MultiTextMapPropagator(propagator1, propagator2)
153+
.extract(context, Collections.emptyMap(), null))
154+
.isSameAs(context);
155+
}
157156

158-
assertThat(result).isSameAs(context);
157+
@Test
158+
void inject_nullContext() {
159+
Map<String, String> carrier = new LinkedHashMap<>();
160+
new MultiTextMapPropagator(propagator1, propagator2).inject(null, carrier, Map::put);
161+
assertThat(carrier).isEmpty();
162+
}
163+
164+
@Test
165+
void inject_nullSetter() {
166+
Map<String, String> carrier = new LinkedHashMap<>();
167+
Context context = Context.current().with(KEY, "treasure");
168+
new MultiTextMapPropagator(propagator1, propagator2).inject(context, carrier, null);
169+
assertThat(carrier).isEmpty();
159170
}
160171
}

context/src/test/java/io/opentelemetry/context/propagation/NoopTextMapPropagatorTest.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99

1010
import io.opentelemetry.context.Context;
11+
import io.opentelemetry.context.ContextKey;
12+
import java.util.Collections;
1113
import java.util.HashMap;
14+
import java.util.LinkedHashMap;
15+
import java.util.Map;
1216
import javax.annotation.Nullable;
1317
import org.junit.jupiter.api.Test;
1418

1519
class NoopTextMapPropagatorTest {
1620

21+
private static final ContextKey<String> KEY = ContextKey.named("key");
22+
1723
@Test
1824
void noopFields() {
1925
assertThat(TextMapPropagator.noop().fields()).isEmpty();
@@ -23,28 +29,51 @@ void noopFields() {
2329
void extract_contextUnchanged() {
2430
Context input = Context.current();
2531
Context result =
26-
TextMapPropagator.noop().extract(input, new HashMap<>(), new HashMapTextMapGetter());
32+
TextMapPropagator.noop().extract(input, new HashMap<>(), MapTextMapGetter.INSTANCE);
2733
assertThat(result).isSameAs(input);
2834
}
2935

3036
@Test
3137
void extract_nullContext() {
32-
Context input = null;
33-
Context result =
34-
TextMapPropagator.noop().extract(input, new HashMap<>(), new HashMapTextMapGetter());
35-
assertThat(result).isSameAs(input);
38+
assertThat(
39+
TextMapPropagator.noop()
40+
.extract(null, Collections.emptyMap(), MapTextMapGetter.INSTANCE))
41+
.isSameAs(Context.root());
3642
}
3743

38-
private static class HashMapTextMapGetter
39-
implements TextMapGetter<HashMap<? extends Object, ? extends Object>> {
44+
@Test
45+
void extract_nullGetter() {
46+
Context context = Context.current().with(KEY, "treasure");
47+
assertThat(TextMapPropagator.noop().extract(context, Collections.emptyMap(), null))
48+
.isSameAs(context);
49+
}
50+
51+
@Test
52+
void inject_nullContext() {
53+
Map<String, String> carrier = new LinkedHashMap<>();
54+
TextMapPropagator.noop().inject(null, carrier, Map::put);
55+
assertThat(carrier).isEmpty();
56+
}
57+
58+
@Test
59+
void inject_nullSetter() {
60+
Map<String, String> carrier = new LinkedHashMap<>();
61+
Context context = Context.current().with(KEY, "treasure");
62+
TextMapPropagator.noop().inject(context, carrier, null);
63+
assertThat(carrier).isEmpty();
64+
}
65+
66+
enum MapTextMapGetter implements TextMapGetter<Map<? extends Object, ? extends Object>> {
67+
INSTANCE;
68+
4069
@Override
41-
public Iterable<String> keys(HashMap<?, ?> carrier) {
70+
public Iterable<String> keys(Map<?, ?> carrier) {
4271
return null;
4372
}
4473

4574
@Nullable
4675
@Override
47-
public String get(@Nullable HashMap<?, ?> carrier, String key) {
76+
public String get(@Nullable Map<?, ?> carrier, String key) {
4877
return null;
4978
}
5079
}

0 commit comments

Comments
 (0)