Handle nulls in noop / multi (#2939)
* Handle nulls in noop / multi * Remove old file
This commit is contained in:
parent
bd6cfd85fb
commit
0b8131c79e
|
@ -45,6 +45,9 @@ final class MultiTextMapPropagator implements TextMapPropagator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
|
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
|
||||||
|
if (context == null || setter == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
for (TextMapPropagator textPropagator : textPropagators) {
|
for (TextMapPropagator textPropagator : textPropagators) {
|
||||||
textPropagator.inject(context, carrier, setter);
|
textPropagator.inject(context, carrier, setter);
|
||||||
}
|
}
|
||||||
|
@ -52,6 +55,12 @@ final class MultiTextMapPropagator implements TextMapPropagator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
|
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
|
||||||
|
if (context == null) {
|
||||||
|
return Context.root();
|
||||||
|
}
|
||||||
|
if (getter == null) {
|
||||||
|
return context;
|
||||||
|
}
|
||||||
for (TextMapPropagator textPropagator : textPropagators) {
|
for (TextMapPropagator textPropagator : textPropagators) {
|
||||||
context = textPropagator.extract(context, carrier, getter);
|
context = textPropagator.extract(context, carrier, getter);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,9 @@ final class NoopTextMapPropagator implements TextMapPropagator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
|
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
|
||||||
|
if (context == null) {
|
||||||
|
return Context.root();
|
||||||
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,9 +12,12 @@ import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import io.opentelemetry.context.Context;
|
import io.opentelemetry.context.Context;
|
||||||
|
import io.opentelemetry.context.ContextKey;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
@ -26,6 +29,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class MultiTextMapPropagatorTest {
|
class MultiTextMapPropagatorTest {
|
||||||
|
|
||||||
|
private static final ContextKey<String> KEY = ContextKey.named("key");
|
||||||
|
|
||||||
@Mock private TextMapPropagator propagator1;
|
@Mock private TextMapPropagator propagator1;
|
||||||
@Mock private TextMapPropagator propagator2;
|
@Mock private TextMapPropagator propagator2;
|
||||||
@Mock private TextMapPropagator propagator3;
|
@Mock private TextMapPropagator propagator3;
|
||||||
|
@ -93,19 +98,6 @@ class MultiTextMapPropagatorTest {
|
||||||
verify(propagator3).inject(context, carrier, setter);
|
verify(propagator3).inject(context, carrier, setter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void inject_nullContextAllDelegated() {
|
|
||||||
Map<String, String> carrier = new HashMap<>();
|
|
||||||
Context context = null;
|
|
||||||
TextMapSetter<Map<String, String>> setter = Map::put;
|
|
||||||
|
|
||||||
TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2, propagator3);
|
|
||||||
prop.inject(context, carrier, setter);
|
|
||||||
verify(propagator1).inject(context, carrier, setter);
|
|
||||||
verify(propagator2).inject(context, carrier, setter);
|
|
||||||
verify(propagator3).inject(context, carrier, setter);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void extract_noPropagators() {
|
void extract_noPropagators() {
|
||||||
Map<String, String> carrier = new HashMap<>();
|
Map<String, String> carrier = new HashMap<>();
|
||||||
|
@ -147,14 +139,33 @@ class MultiTextMapPropagatorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void extract_nullContext() {
|
void extract_nullContext() {
|
||||||
Map<String, String> carrier = new HashMap<>();
|
assertThat(
|
||||||
Context context = null;
|
new MultiTextMapPropagator(propagator1, propagator2)
|
||||||
when(propagator1.extract(context, carrier, getter)).thenReturn(context);
|
.extract(null, Collections.emptyMap(), getter))
|
||||||
when(propagator2.extract(context, carrier, getter)).thenReturn(context);
|
.isSameAs(Context.root());
|
||||||
|
}
|
||||||
|
|
||||||
TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2);
|
@Test
|
||||||
Context result = prop.extract(context, carrier, getter);
|
void extract_nullGetter() {
|
||||||
|
Context context = Context.current().with(KEY, "treasure");
|
||||||
|
assertThat(
|
||||||
|
new MultiTextMapPropagator(propagator1, propagator2)
|
||||||
|
.extract(context, Collections.emptyMap(), null))
|
||||||
|
.isSameAs(context);
|
||||||
|
}
|
||||||
|
|
||||||
assertThat(result).isSameAs(context);
|
@Test
|
||||||
|
void inject_nullContext() {
|
||||||
|
Map<String, String> carrier = new LinkedHashMap<>();
|
||||||
|
new MultiTextMapPropagator(propagator1, propagator2).inject(null, carrier, Map::put);
|
||||||
|
assertThat(carrier).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void inject_nullSetter() {
|
||||||
|
Map<String, String> carrier = new LinkedHashMap<>();
|
||||||
|
Context context = Context.current().with(KEY, "treasure");
|
||||||
|
new MultiTextMapPropagator(propagator1, propagator2).inject(context, carrier, null);
|
||||||
|
assertThat(carrier).isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,18 @@ package io.opentelemetry.context.propagation;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import io.opentelemetry.context.Context;
|
import io.opentelemetry.context.Context;
|
||||||
|
import io.opentelemetry.context.ContextKey;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class NoopTextMapPropagatorTest {
|
class NoopTextMapPropagatorTest {
|
||||||
|
|
||||||
|
private static final ContextKey<String> KEY = ContextKey.named("key");
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void noopFields() {
|
void noopFields() {
|
||||||
assertThat(TextMapPropagator.noop().fields()).isEmpty();
|
assertThat(TextMapPropagator.noop().fields()).isEmpty();
|
||||||
|
@ -23,28 +29,51 @@ class NoopTextMapPropagatorTest {
|
||||||
void extract_contextUnchanged() {
|
void extract_contextUnchanged() {
|
||||||
Context input = Context.current();
|
Context input = Context.current();
|
||||||
Context result =
|
Context result =
|
||||||
TextMapPropagator.noop().extract(input, new HashMap<>(), new HashMapTextMapGetter());
|
TextMapPropagator.noop().extract(input, new HashMap<>(), MapTextMapGetter.INSTANCE);
|
||||||
assertThat(result).isSameAs(input);
|
assertThat(result).isSameAs(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void extract_nullContext() {
|
void extract_nullContext() {
|
||||||
Context input = null;
|
assertThat(
|
||||||
Context result =
|
TextMapPropagator.noop()
|
||||||
TextMapPropagator.noop().extract(input, new HashMap<>(), new HashMapTextMapGetter());
|
.extract(null, Collections.emptyMap(), MapTextMapGetter.INSTANCE))
|
||||||
assertThat(result).isSameAs(input);
|
.isSameAs(Context.root());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class HashMapTextMapGetter
|
@Test
|
||||||
implements TextMapGetter<HashMap<? extends Object, ? extends Object>> {
|
void extract_nullGetter() {
|
||||||
|
Context context = Context.current().with(KEY, "treasure");
|
||||||
|
assertThat(TextMapPropagator.noop().extract(context, Collections.emptyMap(), null))
|
||||||
|
.isSameAs(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void inject_nullContext() {
|
||||||
|
Map<String, String> carrier = new LinkedHashMap<>();
|
||||||
|
TextMapPropagator.noop().inject(null, carrier, Map::put);
|
||||||
|
assertThat(carrier).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void inject_nullSetter() {
|
||||||
|
Map<String, String> carrier = new LinkedHashMap<>();
|
||||||
|
Context context = Context.current().with(KEY, "treasure");
|
||||||
|
TextMapPropagator.noop().inject(context, carrier, null);
|
||||||
|
assertThat(carrier).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MapTextMapGetter implements TextMapGetter<Map<? extends Object, ? extends Object>> {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<String> keys(HashMap<?, ?> carrier) {
|
public Iterable<String> keys(Map<?, ?> carrier) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public String get(@Nullable HashMap<?, ?> carrier, String key) {
|
public String get(@Nullable Map<?, ?> carrier, String key) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue