-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
…y-java into sampler-factory
- Loading branch information
Showing
12 changed files
with
234 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/PropagatorsFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||
|
||
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; | ||
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
import io.opentelemetry.context.propagation.ContextPropagators; | ||
import io.opentelemetry.context.propagation.TextMapPropagator; | ||
import io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager; | ||
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurablePropagatorProvider; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; | ||
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; | ||
import java.io.Closeable; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.annotation.Nullable; | ||
|
||
final class PropagatorsFactory implements Factory<List<String>, ContextPropagators> { | ||
|
||
private static final PropagatorsFactory INSTANCE = new PropagatorsFactory(); | ||
|
||
private PropagatorsFactory() {} | ||
|
||
static PropagatorsFactory getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public ContextPropagators create( | ||
@Nullable List<String> model, SpiHelper spiHelper, List<Closeable> closeables) { | ||
if (model == null || model.isEmpty()) { | ||
model = Arrays.asList("tracecontext", "baggage"); | ||
} | ||
|
||
if (model.contains("none")) { | ||
if (model.size() > 1) { | ||
throw new ConfigurationException( | ||
"propagators contains \"none\" along with other propagators"); | ||
} | ||
return ContextPropagators.noop(); | ||
} | ||
|
||
NamedSpiManager<TextMapPropagator> spiPropagatorsManager = | ||
spiHelper.loadConfigurable( | ||
ConfigurablePropagatorProvider.class, | ||
ConfigurablePropagatorProvider::getName, | ||
ConfigurablePropagatorProvider::getPropagator, | ||
DefaultConfigProperties.createForTest(Collections.emptyMap())); | ||
Set<TextMapPropagator> propagators = new LinkedHashSet<>(); | ||
for (String propagator : model) { | ||
propagators.add(getPropagator(propagator, spiPropagatorsManager)); | ||
} | ||
|
||
return ContextPropagators.create(TextMapPropagator.composite(propagators)); | ||
} | ||
|
||
private static TextMapPropagator getPropagator( | ||
String name, NamedSpiManager<TextMapPropagator> spiPropagatorsManager) { | ||
if (name.equals("tracecontext")) { | ||
return W3CTraceContextPropagator.getInstance(); | ||
} | ||
if (name.equals("baggage")) { | ||
return W3CBaggagePropagator.getInstance(); | ||
} | ||
|
||
TextMapPropagator spiPropagator = spiPropagatorsManager.getByName(name); | ||
if (spiPropagator != null) { | ||
return spiPropagator; | ||
} | ||
throw new ConfigurationException("Unrecognized value for otel.propagators: " + name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/PropagatorsFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; | ||
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
import io.opentelemetry.context.propagation.ContextPropagators; | ||
import io.opentelemetry.context.propagation.TextMapPropagator; | ||
import io.opentelemetry.extension.trace.propagation.B3Propagator; | ||
import io.opentelemetry.extension.trace.propagation.JaegerPropagator; | ||
import io.opentelemetry.extension.trace.propagation.OtTracePropagator; | ||
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class PropagatorsFactoryTest { | ||
|
||
private final SpiHelper spiHelper = | ||
SpiHelper.create(PropagatorsFactoryTest.class.getClassLoader()); | ||
|
||
@ParameterizedTest | ||
@MethodSource("createArguments") | ||
void create(List<String> model, ContextPropagators expectedPropagators) { | ||
ContextPropagators propagators = | ||
PropagatorsFactory.getInstance().create(model, spiHelper, Collections.emptyList()); | ||
|
||
assertThat(propagators.toString()).isEqualTo(expectedPropagators.toString()); | ||
} | ||
|
||
private static Stream<Arguments> createArguments() { | ||
return Stream.of( | ||
Arguments.of( | ||
null, | ||
ContextPropagators.create( | ||
TextMapPropagator.composite( | ||
W3CTraceContextPropagator.getInstance(), W3CBaggagePropagator.getInstance()))), | ||
Arguments.of( | ||
Collections.emptyList(), | ||
ContextPropagators.create( | ||
TextMapPropagator.composite( | ||
W3CTraceContextPropagator.getInstance(), W3CBaggagePropagator.getInstance()))), | ||
Arguments.of(Collections.singletonList("none"), ContextPropagators.noop()), | ||
Arguments.of( | ||
Arrays.asList("tracecontext", "baggage", "ottrace", "b3multi", "b3", "jaeger"), | ||
ContextPropagators.create( | ||
TextMapPropagator.composite( | ||
W3CTraceContextPropagator.getInstance(), | ||
W3CBaggagePropagator.getInstance(), | ||
OtTracePropagator.getInstance(), | ||
B3Propagator.injectingMultiHeaders(), | ||
B3Propagator.injectingSingleHeader(), | ||
JaegerPropagator.getInstance())))); | ||
} | ||
|
||
@Test | ||
void create_NoneAndOther() { | ||
assertThatThrownBy( | ||
() -> | ||
PropagatorsFactory.getInstance() | ||
.create(Arrays.asList("none", "foo"), spiHelper, Collections.emptyList())) | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessage("propagators contains \"none\" along with other propagators"); | ||
} | ||
|
||
@Test | ||
void create_UnknownSpiPropagator() { | ||
assertThatThrownBy( | ||
() -> | ||
PropagatorsFactory.getInstance() | ||
.create(Collections.singletonList("foo"), spiHelper, Collections.emptyList())) | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessage("Unrecognized value for otel.propagators: foo"); | ||
} | ||
} |