Deprecate trace mutli propagator (#2342)
* add deprecation * backfill tests * remove invalid tests * add @ * Update extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/TraceMultiPropagator.java Co-authored-by: John Watson <jkwatson@gmail.com> * Update api/context/src/test/java/io/opentelemetry/context/propagation/MultiTextMapPropagatorTest.java Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com> * Update extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/TraceMultiPropagator.java Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com> * Update api/context/src/test/java/io/opentelemetry/context/propagation/MultiTextMapPropagatorTest.java * suppress warnings * get spotless Co-authored-by: John Watson <jkwatson@gmail.com> Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
This commit is contained in:
		
							parent
							
								
									9f675da85a
								
							
						
					
					
						commit
						8ef80f0db2
					
				| 
						 | 
				
			
			@ -7,6 +7,7 @@ package io.opentelemetry.context.propagation;
 | 
			
		|||
 | 
			
		||||
import io.opentelemetry.context.Context;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
| 
						 | 
				
			
			@ -18,6 +19,10 @@ final class MultiTextMapPropagator implements TextMapPropagator {
 | 
			
		|||
  private final TextMapPropagator[] textPropagators;
 | 
			
		||||
  private final Collection<String> allFields;
 | 
			
		||||
 | 
			
		||||
  MultiTextMapPropagator(TextMapPropagator... textPropagators) {
 | 
			
		||||
    this(Arrays.asList(textPropagators));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  MultiTextMapPropagator(List<TextMapPropagator> textPropagators) {
 | 
			
		||||
    this.textPropagators = new TextMapPropagator[textPropagators.size()];
 | 
			
		||||
    textPropagators.toArray(this.textPropagators);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,136 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright The OpenTelemetry Authors
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package io.opentelemetry.context.propagation;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertThrows;
 | 
			
		||||
import static org.mockito.Mockito.mock;
 | 
			
		||||
import static org.mockito.Mockito.verify;
 | 
			
		||||
import static org.mockito.Mockito.when;
 | 
			
		||||
 | 
			
		||||
import io.opentelemetry.context.Context;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.junit.jupiter.api.extension.ExtendWith;
 | 
			
		||||
import org.mockito.Mock;
 | 
			
		||||
import org.mockito.junit.jupiter.MockitoExtension;
 | 
			
		||||
 | 
			
		||||
@ExtendWith(MockitoExtension.class)
 | 
			
		||||
@SuppressWarnings("deprecation") // we're testing deprecated stuff in here.
 | 
			
		||||
class MultiTextMapPropagatorTest {
 | 
			
		||||
 | 
			
		||||
  @Mock private TextMapPropagator propagator1;
 | 
			
		||||
  @Mock private TextMapPropagator propagator2;
 | 
			
		||||
  @Mock private TextMapPropagator propagator3;
 | 
			
		||||
 | 
			
		||||
  private static final TextMapPropagator.Getter<Map<String, String>> getter =
 | 
			
		||||
      new TextMapPropagator.Getter<Map<String, String>>() {
 | 
			
		||||
        @Override
 | 
			
		||||
        public Iterable<String> keys(Map<String, String> carrier) {
 | 
			
		||||
          return carrier.keySet();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Nullable
 | 
			
		||||
        @Override
 | 
			
		||||
        public String get(Map<String, String> carrier, String key) {
 | 
			
		||||
          return carrier.get(key);
 | 
			
		||||
        }
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  void addPropagator_null() {
 | 
			
		||||
    assertThrows(
 | 
			
		||||
        NullPointerException.class,
 | 
			
		||||
        () -> new MultiTextMapPropagator((List<TextMapPropagator>) null));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  void fields() {
 | 
			
		||||
    when(propagator1.fields()).thenReturn(Arrays.asList("foo", "bar"));
 | 
			
		||||
    when(propagator2.fields()).thenReturn(Arrays.asList("hello", "world"));
 | 
			
		||||
    TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2);
 | 
			
		||||
 | 
			
		||||
    Collection<String> fields = prop.fields();
 | 
			
		||||
    assertThat(fields).containsExactly("foo", "bar", "hello", "world");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  void fields_duplicates() {
 | 
			
		||||
    when(propagator1.fields()).thenReturn(Arrays.asList("foo", "bar", "foo"));
 | 
			
		||||
    when(propagator2.fields()).thenReturn(Arrays.asList("hello", "world", "world", "bar"));
 | 
			
		||||
    TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2);
 | 
			
		||||
 | 
			
		||||
    Collection<String> fields = prop.fields();
 | 
			
		||||
    assertThat(fields).containsExactly("foo", "bar", "hello", "world");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  void fields_readOnly() {
 | 
			
		||||
    when(propagator1.fields()).thenReturn(Arrays.asList("rubber", "baby"));
 | 
			
		||||
    when(propagator2.fields()).thenReturn(Arrays.asList("buggy", "bumpers"));
 | 
			
		||||
    TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2);
 | 
			
		||||
    Collection<String> fields = prop.fields();
 | 
			
		||||
    assertThrows(UnsupportedOperationException.class, () -> fields.add("hi"));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  void inject_allDelegated() {
 | 
			
		||||
    Map<String, String> carrier = new HashMap<>();
 | 
			
		||||
    Context context = mock(Context.class);
 | 
			
		||||
    TextMapPropagator.Setter<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
 | 
			
		||||
  void extract_noPropagators() {
 | 
			
		||||
    Map<String, String> carrier = new HashMap<>();
 | 
			
		||||
    Context context = mock(Context.class);
 | 
			
		||||
 | 
			
		||||
    TextMapPropagator prop = new MultiTextMapPropagator();
 | 
			
		||||
    Context resContext = prop.extract(context, carrier, getter);
 | 
			
		||||
    assertThat(context).isSameAs(resContext);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  void extract_found_all() {
 | 
			
		||||
    Map<String, String> carrier = new HashMap<>();
 | 
			
		||||
    TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2, propagator3);
 | 
			
		||||
    Context context1 = mock(Context.class);
 | 
			
		||||
    Context context2 = mock(Context.class);
 | 
			
		||||
    Context context3 = mock(Context.class);
 | 
			
		||||
    Context expectedContext = mock(Context.class);
 | 
			
		||||
 | 
			
		||||
    when(propagator1.extract(context1, carrier, getter)).thenReturn(context2);
 | 
			
		||||
    when(propagator2.extract(context2, carrier, getter)).thenReturn(context3);
 | 
			
		||||
    when(propagator3.extract(context3, carrier, getter)).thenReturn(expectedContext);
 | 
			
		||||
 | 
			
		||||
    assertThat(prop.extract(context1, carrier, getter)).isEqualTo(expectedContext);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  void extract_notFound() {
 | 
			
		||||
 | 
			
		||||
    Map<String, String> carrier = new HashMap<>();
 | 
			
		||||
    Context context = mock(Context.class);
 | 
			
		||||
    when(propagator1.extract(context, carrier, getter)).thenReturn(context);
 | 
			
		||||
    when(propagator2.extract(context, carrier, getter)).thenReturn(context);
 | 
			
		||||
 | 
			
		||||
    TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2);
 | 
			
		||||
    Context result = prop.extract(context, carrier, getter);
 | 
			
		||||
 | 
			
		||||
    assertThat(result).isSameAs(context);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -48,8 +48,14 @@ import javax.annotation.concurrent.Immutable;
 | 
			
		|||
 * Context context = OpenTelemetry.getPropagators().getTextMapPropagator()
 | 
			
		||||
 *   .extract(context, carrier, carrierGetter);
 | 
			
		||||
 * }</pre>
 | 
			
		||||
 *
 | 
			
		||||
 * <p>This class is @deprecated and will be removed in 0.14.0. Users should migrate to the
 | 
			
		||||
 * MultiTextMapPropagator in the api.
 | 
			
		||||
 *
 | 
			
		||||
 * @deprecated Use {@link TextMapPropagator#composite}
 | 
			
		||||
 */
 | 
			
		||||
@Immutable
 | 
			
		||||
@Deprecated
 | 
			
		||||
public final class TraceMultiPropagator implements TextMapPropagator {
 | 
			
		||||
 | 
			
		||||
  /** Returns a {@link TraceMultiPropagator} for the given {@code propagators}. */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -30,6 +30,7 @@ import org.mockito.Mockito;
 | 
			
		|||
import org.mockito.junit.jupiter.MockitoExtension;
 | 
			
		||||
 | 
			
		||||
@ExtendWith(MockitoExtension.class)
 | 
			
		||||
@SuppressWarnings("deprecation") // the class under test will go away soon
 | 
			
		||||
class TraceMultiPropagatorTest {
 | 
			
		||||
  private static final TextMapPropagator PROPAGATOR1 = B3Propagator.getInstance();
 | 
			
		||||
  private static final TextMapPropagator PROPAGATOR2 =
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue