Skip to content

Commit

Permalink
Add file configuration ComponentProvider support for propagators (#6624)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Aug 28, 2024
1 parent 938b03d commit d37c1c7
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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()}.
*
* <p>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<TextMapPropagator> {

@Override
public Class<TextMapPropagator> getType() {
return TextMapPropagator.class;
}

@Override
public String getName() {
return "b3";
}

@Override
public TextMapPropagator create(StructuredConfigProperties config) {
return B3Propagator.injectingSingleHeader();
}
}
Original file line number Diff line number Diff line change
@@ -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()}.
*
* <p>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<TextMapPropagator> {

@Override
public Class<TextMapPropagator> getType() {
return TextMapPropagator.class;
}

@Override
public String getName() {
return "b3multi";
}

@Override
public TextMapPropagator create(StructuredConfigProperties config) {
return B3Propagator.injectingMultiHeaders();
}
}
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>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<TextMapPropagator> {

@Override
public Class<TextMapPropagator> getType() {
return TextMapPropagator.class;
}

@Override
public String getName() {
return "jaeger";
}

@Override
public TextMapPropagator create(StructuredConfigProperties config) {
return JaegerPropagator.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>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<TextMapPropagator> {

@Override
public Class<TextMapPropagator> getType() {
return TextMapPropagator.class;
}

@Override
public String getName() {
return "ottrace";
}

@Override
public TextMapPropagator create(StructuredConfigProperties config) {
return OtTracePropagator.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<String>, TextMapPropagator> {

Expand All @@ -45,33 +41,23 @@ public TextMapPropagator create(
return TextMapPropagator.noop();
}

NamedSpiManager<TextMapPropagator> spiPropagatorsManager =
spiHelper.loadConfigurable(
ConfigurablePropagatorProvider.class,
ConfigurablePropagatorProvider::getName,
ConfigurablePropagatorProvider::getPropagator,
DefaultConfigProperties.createFromMap(Collections.emptyMap()));
Set<TextMapPropagator> propagators = new LinkedHashSet<>();
List<TextMapPropagator> 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<TextMapPropagator> spiPropagatorsManager) {
private static TextMapPropagator getPropagator(SpiHelper spiHelper, String name) {
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 propagator: " + name);
return FileConfigUtil.loadComponent(
spiHelper, TextMapPropagator.class, name, Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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<TextMapPropagator> {
@Override
public Class<TextMapPropagator> 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<String> fields() {
return Collections.emptyList();
}

@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {}

@Override
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
return context;
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d37c1c7

Please sign in to comment.