diff --git a/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/B3ComponentProvider.java b/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/B3ComponentProvider.java new file mode 100644 index 00000000000..b97134eda7a --- /dev/null +++ b/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/B3ComponentProvider.java @@ -0,0 +1,36 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.trace.propagation.internal; + +import io.opentelemetry.context.propagation.TextMapPropagator; +import io.opentelemetry.extension.trace.propagation.B3Propagator; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; +import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties; + +/** + * File configuration SPI implementation for {@link B3Propagator} which allows enables the {@link + * B3Propagator#injectingSingleHeader()}. + * + *

This class is internal and is hence not for public use. Its APIs are unstable and can change + * at any time. + */ +public final class B3ComponentProvider implements ComponentProvider { + + @Override + public Class getType() { + return TextMapPropagator.class; + } + + @Override + public String getName() { + return "b3"; + } + + @Override + public TextMapPropagator create(StructuredConfigProperties config) { + return B3Propagator.injectingSingleHeader(); + } +} diff --git a/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/B3MultiComponentProvider.java b/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/B3MultiComponentProvider.java new file mode 100644 index 00000000000..77ac501a2ea --- /dev/null +++ b/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/B3MultiComponentProvider.java @@ -0,0 +1,36 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.trace.propagation.internal; + +import io.opentelemetry.context.propagation.TextMapPropagator; +import io.opentelemetry.extension.trace.propagation.B3Propagator; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; +import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties; + +/** + * File configuration SPI implementation for {@link B3Propagator} which allows enables the {@link + * B3Propagator#injectingMultiHeaders()}. + * + *

This class is internal and is hence not for public use. Its APIs are unstable and can change + * at any time. + */ +public final class B3MultiComponentProvider implements ComponentProvider { + + @Override + public Class getType() { + return TextMapPropagator.class; + } + + @Override + public String getName() { + return "b3multi"; + } + + @Override + public TextMapPropagator create(StructuredConfigProperties config) { + return B3Propagator.injectingMultiHeaders(); + } +} diff --git a/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/JaegerComponentProvider.java b/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/JaegerComponentProvider.java new file mode 100644 index 00000000000..0fb844a40da --- /dev/null +++ b/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/JaegerComponentProvider.java @@ -0,0 +1,35 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.trace.propagation.internal; + +import io.opentelemetry.context.propagation.TextMapPropagator; +import io.opentelemetry.extension.trace.propagation.JaegerPropagator; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; +import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties; + +/** + * File configuration SPI implementation for {@link JaegerPropagator}. + * + *

This class is internal and is hence not for public use. Its APIs are unstable and can change + * at any time. + */ +public final class JaegerComponentProvider implements ComponentProvider { + + @Override + public Class getType() { + return TextMapPropagator.class; + } + + @Override + public String getName() { + return "jaeger"; + } + + @Override + public TextMapPropagator create(StructuredConfigProperties config) { + return JaegerPropagator.getInstance(); + } +} diff --git a/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/OtTraceComponentProvider.java b/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/OtTraceComponentProvider.java new file mode 100644 index 00000000000..97b41aad3af --- /dev/null +++ b/extensions/trace-propagators/src/main/java/io/opentelemetry/extension/trace/propagation/internal/OtTraceComponentProvider.java @@ -0,0 +1,36 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.trace.propagation.internal; + +import io.opentelemetry.context.propagation.TextMapPropagator; +import io.opentelemetry.extension.trace.propagation.B3Propagator; +import io.opentelemetry.extension.trace.propagation.OtTracePropagator; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; +import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties; + +/** + * File configuration SPI implementation for {@link B3Propagator}. + * + *

This class is internal and is hence not for public use. Its APIs are unstable and can change + * at any time. + */ +public final class OtTraceComponentProvider implements ComponentProvider { + + @Override + public Class getType() { + return TextMapPropagator.class; + } + + @Override + public String getName() { + return "ottrace"; + } + + @Override + public TextMapPropagator create(StructuredConfigProperties config) { + return OtTracePropagator.getInstance(); + } +} diff --git a/extensions/trace-propagators/src/main/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider b/extensions/trace-propagators/src/main/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider new file mode 100644 index 00000000000..d4194f2b014 --- /dev/null +++ b/extensions/trace-propagators/src/main/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider @@ -0,0 +1,4 @@ +io.opentelemetry.extension.trace.propagation.internal.B3ComponentProvider +io.opentelemetry.extension.trace.propagation.internal.B3MultiComponentProvider +io.opentelemetry.extension.trace.propagation.internal.JaegerComponentProvider +io.opentelemetry.extension.trace.propagation.internal.OtTraceComponentProvider diff --git a/sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/TextMapPropagatorFactory.java b/sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/TextMapPropagatorFactory.java index 965093d1c6b..b2a212aaa57 100644 --- a/sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/TextMapPropagatorFactory.java +++ b/sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/TextMapPropagatorFactory.java @@ -8,17 +8,13 @@ import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; 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.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; final class TextMapPropagatorFactory implements Factory, TextMapPropagator> { @@ -45,22 +41,15 @@ public TextMapPropagator create( return TextMapPropagator.noop(); } - NamedSpiManager spiPropagatorsManager = - spiHelper.loadConfigurable( - ConfigurablePropagatorProvider.class, - ConfigurablePropagatorProvider::getName, - ConfigurablePropagatorProvider::getPropagator, - DefaultConfigProperties.createFromMap(Collections.emptyMap())); - Set propagators = new LinkedHashSet<>(); + List propagators = new ArrayList<>(); for (String propagator : model) { - propagators.add(getPropagator(propagator, spiPropagatorsManager)); + propagators.add(getPropagator(spiHelper, propagator)); } return TextMapPropagator.composite(propagators); } - private static TextMapPropagator getPropagator( - String name, NamedSpiManager spiPropagatorsManager) { + private static TextMapPropagator getPropagator(SpiHelper spiHelper, String name) { if (name.equals("tracecontext")) { return W3CTraceContextPropagator.getInstance(); } @@ -68,10 +57,7 @@ private static TextMapPropagator getPropagator( return W3CBaggagePropagator.getInstance(); } - TextMapPropagator spiPropagator = spiPropagatorsManager.getByName(name); - if (spiPropagator != null) { - return spiPropagator; - } - throw new ConfigurationException("Unrecognized propagator: " + name); + return FileConfigUtil.loadComponent( + spiHelper, TextMapPropagator.class, name, Collections.emptyMap()); } } diff --git a/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/FileConfigurationCreateTest.java b/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/FileConfigurationCreateTest.java index d47ae14eb9a..51f70d930ec 100644 --- a/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/FileConfigurationCreateTest.java +++ b/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/FileConfigurationCreateTest.java @@ -86,7 +86,10 @@ void parseAndCreate_Examples(@TempDir Path tempDir) "client_key: .*\n", "client_key: " + clientKeyPath + System.lineSeparator()) .replaceAll( "client_certificate: .*\n", - "client_certificate: " + clientCertificatePath + System.lineSeparator()); + "client_certificate: " + clientCertificatePath + System.lineSeparator()) + // TODO: remove once ComponentProvider SPI implemented in + // https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/aws-xray-propagator + .replaceAll("xray,", ""); InputStream is = new ByteArrayInputStream(rewrittenExampleContent.getBytes(StandardCharsets.UTF_8)); diff --git a/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/TextMapPropagatorFactoryTest.java b/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/TextMapPropagatorFactoryTest.java index d2355fda46d..d2a1d5d242d 100644 --- a/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/TextMapPropagatorFactoryTest.java +++ b/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/TextMapPropagatorFactoryTest.java @@ -16,6 +16,8 @@ import io.opentelemetry.extension.trace.propagation.OtTracePropagator; import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; +import io.opentelemetry.sdk.extension.incubator.fileconfig.component.TextMapPropagatorComponentProvider; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -68,12 +70,28 @@ void create_NoneAndOther() { } @Test - void create_UnknownSpiPropagator() { + void create_SpiPropagator_Unknown() { assertThatThrownBy( () -> TextMapPropagatorFactory.getInstance() .create(Collections.singletonList("foo"), spiHelper, Collections.emptyList())) .isInstanceOf(ConfigurationException.class) - .hasMessage("Unrecognized propagator: foo"); + .hasMessage( + "No component provider detected for io.opentelemetry.context.propagation.TextMapPropagator with name \"foo\"."); + } + + @Test + void create_SpiPropagator_Valid() { + TextMapPropagator textMapPropagator = + TextMapPropagatorFactory.getInstance() + .create(Collections.singletonList("test"), spiHelper, new ArrayList<>()); + assertThat(textMapPropagator) + .isInstanceOfSatisfying( + TextMapPropagatorComponentProvider.TestTextMapPropagator.class, + testTextMapPropagator -> + assertThat(testTextMapPropagator.config) + .isInstanceOfSatisfying( + YamlStructuredConfigProperties.class, + config -> assertThat(config.getPropertyKeys()).isEmpty())); } } diff --git a/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/component/TextMapPropagatorComponentProvider.java b/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/component/TextMapPropagatorComponentProvider.java new file mode 100644 index 00000000000..a3005beba4b --- /dev/null +++ b/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/component/TextMapPropagatorComponentProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.extension.incubator.fileconfig.component; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapGetter; +import io.opentelemetry.context.propagation.TextMapPropagator; +import io.opentelemetry.context.propagation.TextMapSetter; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; +import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties; +import java.util.Collection; +import java.util.Collections; +import javax.annotation.Nullable; + +public class TextMapPropagatorComponentProvider implements ComponentProvider { + @Override + public Class getType() { + return TextMapPropagator.class; + } + + @Override + public String getName() { + return "test"; + } + + @Override + public TextMapPropagator create(StructuredConfigProperties config) { + return new TestTextMapPropagator(config); + } + + public static class TestTextMapPropagator implements TextMapPropagator { + + public final StructuredConfigProperties config; + + private TestTextMapPropagator(StructuredConfigProperties config) { + this.config = config; + } + + @Override + public Collection fields() { + return Collections.emptyList(); + } + + @Override + public void inject(Context context, @Nullable C carrier, TextMapSetter setter) {} + + @Override + public Context extract(Context context, @Nullable C carrier, TextMapGetter getter) { + return context; + } + } +} diff --git a/sdk-extensions/incubator/src/test/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider b/sdk-extensions/incubator/src/test/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider index 2bc161f4d9c..bf04a784ecd 100644 --- a/sdk-extensions/incubator/src/test/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider +++ b/sdk-extensions/incubator/src/test/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider @@ -1,6 +1,7 @@ io.opentelemetry.sdk.extension.incubator.fileconfig.component.MetricExporterComponentProvider io.opentelemetry.sdk.extension.incubator.fileconfig.component.SpanExporterComponentProvider io.opentelemetry.sdk.extension.incubator.fileconfig.component.LogRecordExporterComponentProvider +io.opentelemetry.sdk.extension.incubator.fileconfig.component.TextMapPropagatorComponentProvider io.opentelemetry.sdk.extension.incubator.fileconfig.component.SamplerComponentProvider io.opentelemetry.sdk.extension.incubator.fileconfig.component.SpanProcessorComponentProvider io.opentelemetry.sdk.extension.incubator.fileconfig.component.LogRecordProcessorComponentProvider