Skip to content

Commit

Permalink
Add file configuration ComponentProvider support for samplers (#6494)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Aug 28, 2024
1 parent 8495996 commit 938b03d
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfiguration;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Sampler;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -84,30 +86,10 @@ public static OpenTelemetrySdk parseAndCreate(InputStream inputStream) {
* @throws ConfigurationException if unable to interpret
*/
public static OpenTelemetrySdk create(OpenTelemetryConfiguration configurationModel) {
List<Closeable> closeables = new ArrayList<>();
try {
return OpenTelemetryConfigurationFactory.getInstance()
.create(
configurationModel,
SpiHelper.create(FileConfiguration.class.getClassLoader()),
closeables);
} catch (RuntimeException e) {
logger.info(
"Error encountered interpreting configuration model. Closing partially configured components.");
for (Closeable closeable : closeables) {
try {
logger.fine("Closing " + closeable.getClass().getName());
closeable.close();
} catch (IOException ex) {
logger.warning(
"Error closing " + closeable.getClass().getName() + ": " + ex.getMessage());
}
}
if (e instanceof ConfigurationException) {
throw e;
}
throw new ConfigurationException("Unexpected configuration error", e);
}
return createAndMaybeCleanup(
OpenTelemetryConfigurationFactory.getInstance(),
SpiHelper.create(FileConfiguration.class.getClassLoader()),
configurationModel);
}

/**
Expand Down Expand Up @@ -157,6 +139,57 @@ static StructuredConfigProperties toConfigProperties(Object model) {
return YamlStructuredConfigProperties.create(configurationMap);
}

/**
* Create a {@link Sampler} from the {@code samplerModel} representing the sampler config.
*
* <p>This is used when samplers are composed, with one sampler accepting one or more additional
* samplers as config properties. The {@link ComponentProvider} implementation can call this to
* configure a delegate {@link Sampler} from the {@link StructuredConfigProperties} corresponding
* to a particular config property.
*/
// TODO(jack-berg): add create methods for all SDK extension components supported by
// ComponentProvider
public static io.opentelemetry.sdk.trace.samplers.Sampler createSampler(
StructuredConfigProperties genericSamplerModel) {
Sampler samplerModel = convertToModel(genericSamplerModel, Sampler.class);
return createAndMaybeCleanup(
SamplerFactory.getInstance(),
SpiHelper.create(FileConfiguration.class.getClassLoader()),
samplerModel);
}

static <T> T convertToModel(
StructuredConfigProperties structuredConfigProperties, Class<T> modelType) {
if (!(structuredConfigProperties instanceof YamlStructuredConfigProperties)) {
throw new ConfigurationException(
"Only YamlStructuredConfigProperties can be converted to model");
}
return MAPPER.convertValue(
((YamlStructuredConfigProperties) structuredConfigProperties).toMap(), modelType);
}

static <M, R> R createAndMaybeCleanup(Factory<M, R> factory, SpiHelper spiHelper, M model) {
List<Closeable> closeables = new ArrayList<>();
try {
return factory.create(model, spiHelper, closeables);
} catch (RuntimeException e) {
logger.info("Error encountered interpreting model. Closing partially configured components.");
for (Closeable closeable : closeables) {
try {
logger.fine("Closing " + closeable.getClass().getName());
closeable.close();
} catch (IOException ex) {
logger.warning(
"Error closing " + closeable.getClass().getName() + ": " + ex.getMessage());
}
}
if (e instanceof ConfigurationException) {
throw e;
}
throw new ConfigurationException("Unexpected configuration error", e);
}
}

/**
* {@link StandardConstructor} which substitutes environment variables.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@

import static java.util.stream.Collectors.joining;

import io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.JaegerRemote;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.ParentBased;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.TraceIdRatioBased;
import io.opentelemetry.sdk.trace.samplers.ParentBasedSamplerBuilder;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import java.io.Closeable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -63,70 +57,49 @@ public Sampler create(
: create(parentBasedModel.getRoot(), spiHelper, closeables);
ParentBasedSamplerBuilder builder = Sampler.parentBasedBuilder(root);
if (parentBasedModel.getRemoteParentSampled() != null) {
builder.setRemoteParentSampled(
create(parentBasedModel.getRemoteParentSampled(), spiHelper, closeables));
Sampler sampler = create(parentBasedModel.getRemoteParentSampled(), spiHelper, closeables);
builder.setRemoteParentSampled(sampler);
}
if (parentBasedModel.getRemoteParentNotSampled() != null) {
builder.setRemoteParentNotSampled(
create(parentBasedModel.getRemoteParentNotSampled(), spiHelper, closeables));
Sampler sampler =
create(parentBasedModel.getRemoteParentNotSampled(), spiHelper, closeables);
builder.setRemoteParentNotSampled(sampler);
}
if (parentBasedModel.getLocalParentSampled() != null) {
builder.setLocalParentSampled(
create(parentBasedModel.getLocalParentSampled(), spiHelper, closeables));
Sampler sampler = create(parentBasedModel.getLocalParentSampled(), spiHelper, closeables);
builder.setLocalParentSampled(sampler);
}
if (parentBasedModel.getLocalParentNotSampled() != null) {
builder.setLocalParentNotSampled(
create(parentBasedModel.getLocalParentNotSampled(), spiHelper, closeables));
Sampler sampler =
create(parentBasedModel.getLocalParentNotSampled(), spiHelper, closeables);
builder.setLocalParentNotSampled(sampler);
}
return builder.build();
}

JaegerRemote jaegerRemoteModel = model.getJaegerRemote();
if (jaegerRemoteModel != null) {
// Translate from file configuration scheme to environment variable scheme. This is ultimately
// interpreted by JaegerRemoteSamplerProvider, but we want to avoid the dependency on
// opentelemetry-sdk-extension-jaeger-remote-sampler
Map<String, String> properties = new HashMap<>();
if (jaegerRemoteModel.getEndpoint() != null) {
properties.put("endpoint", jaegerRemoteModel.getEndpoint());
}
if (jaegerRemoteModel.getInterval() != null) {
properties.put("pollingInterval", String.valueOf(jaegerRemoteModel.getInterval()));
}
// TODO(jack-berg): determine how to support initial sampler. This is first case where a
// component configured via SPI has property that isn't available in the environment variable
// scheme.
String otelTraceSamplerArg =
properties.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(joining(","));

ConfigProperties configProperties =
DefaultConfigProperties.createFromMap(
Collections.singletonMap("otel.traces.sampler.arg", otelTraceSamplerArg));
return FileConfigUtil.addAndReturn(
closeables,
FileConfigUtil.assertNotNull(
samplerSpiManager(configProperties, spiHelper).getByName("jaeger_remote"),
"jaeger remote sampler"));
model.getAdditionalProperties().put("jaeger_remote", jaegerRemoteModel);
}

// TODO(jack-berg): add support for generic SPI samplers
if (!model.getAdditionalProperties().isEmpty()) {
throw new ConfigurationException(
"Unrecognized sampler(s): "
+ model.getAdditionalProperties().keySet().stream().collect(joining(",", "[", "]")));
Map<String, Object> additionalProperties = model.getAdditionalProperties();
if (additionalProperties.size() > 1) {
throw new ConfigurationException(
"Invalid configuration - multiple samplers exporters set: "
+ additionalProperties.keySet().stream().collect(joining(",", "[", "]")));
}
Map.Entry<String, Object> exporterKeyValue =
additionalProperties.entrySet().stream()
.findFirst()
.orElseThrow(
() -> new IllegalStateException("Missing sampler. This is a programming error."));
Sampler sampler =
FileConfigUtil.loadComponent(
spiHelper, Sampler.class, exporterKeyValue.getKey(), exporterKeyValue.getValue());
return FileConfigUtil.addAndReturn(closeables, sampler);
} else {
throw new ConfigurationException("sampler must be set");
}
}

private static NamedSpiManager<Sampler> samplerSpiManager(
ConfigProperties config, SpiHelper spiHelper) {
return spiHelper.loadConfigurable(
ConfigurableSamplerProvider.class,
ConfigurableSamplerProvider::getName,
ConfigurableSamplerProvider::createSampler,
config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ final class YamlStructuredConfigProperties implements StructuredConfigProperties
/** Values are {@link #isPrimitive(Object)}, {@link List} of scalars. */
private final Map<String, Object> simpleEntries;

private final Map<String, List<StructuredConfigProperties>> listEntries;
private final Map<String, StructuredConfigProperties> mapEntries;
private final Map<String, List<YamlStructuredConfigProperties>> listEntries;
private final Map<String, YamlStructuredConfigProperties> mapEntries;

private YamlStructuredConfigProperties(
Map<String, Object> simpleEntries,
Map<String, List<StructuredConfigProperties>> listEntries,
Map<String, StructuredConfigProperties> mapEntries) {
Map<String, List<YamlStructuredConfigProperties>> listEntries,
Map<String, YamlStructuredConfigProperties> mapEntries) {
this.simpleEntries = simpleEntries;
this.listEntries = listEntries;
this.mapEntries = mapEntries;
Expand All @@ -59,8 +59,8 @@ private YamlStructuredConfigProperties(
@SuppressWarnings("unchecked")
static YamlStructuredConfigProperties create(Map<String, Object> properties) {
Map<String, Object> simpleEntries = new HashMap<>();
Map<String, List<StructuredConfigProperties>> listEntries = new HashMap<>();
Map<String, StructuredConfigProperties> mapEntries = new HashMap<>();
Map<String, List<YamlStructuredConfigProperties>> listEntries = new HashMap<>();
Map<String, YamlStructuredConfigProperties> mapEntries = new HashMap<>();
for (Map.Entry<String, Object> entry : properties.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
Expand All @@ -73,7 +73,7 @@ static YamlStructuredConfigProperties create(Map<String, Object> properties) {
continue;
}
if (isListOfMaps(value)) {
List<StructuredConfigProperties> list =
List<YamlStructuredConfigProperties> list =
((List<Map<String, Object>>) value)
.stream().map(YamlStructuredConfigProperties::create).collect(toList());
listEntries.put(key, list);
Expand Down Expand Up @@ -257,7 +257,11 @@ public StructuredConfigProperties getStructured(String name) {
@Nullable
@Override
public List<StructuredConfigProperties> getStructuredList(String name) {
return listEntries.get(name);
List<YamlStructuredConfigProperties> value = listEntries.get(name);
if (value != null) {
return Collections.unmodifiableList(value);
}
return null;
}

@Override
Expand All @@ -277,4 +281,15 @@ public String toString() {
mapEntries.forEach((key, value) -> joiner.add(key + "=" + value));
return joiner.toString();
}

/** Return a map representation of the data. */
Map<String, Object> toMap() {
Map<String, Object> result = new HashMap<>(simpleEntries);
listEntries.forEach(
(key, value) ->
result.put(
key, value.stream().map(YamlStructuredConfigProperties::toMap).collect(toList())));
mapEntries.forEach((key, value) -> result.put(key, value.toMap()));
return Collections.unmodifiableMap(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void parseAndCreate_Exception_CleansUpPartials() {
.hasMessage(
"No component provider detected for io.opentelemetry.sdk.logs.export.LogRecordExporter with name \"foo\".");
logCapturer.assertContains(
"Error encountered interpreting configuration model. Closing partially configured components.");
"Error encountered interpreting model. Closing partially configured components.");
logCapturer.assertContains(
"Closing io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter");
logCapturer.assertContains("Closing io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.internal.testing.slf4j.SuppressLogger;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.extension.incubator.fileconfig.component.SamplerComponentProvider;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AlwaysOff;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AlwaysOn;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.JaegerRemote;
Expand Down Expand Up @@ -116,23 +117,41 @@ private static Stream<Arguments> createArguments() {
JaegerRemoteSampler.builder()
.setEndpoint("http://jaeger-remote-endpoint")
.setPollingInterval(Duration.ofSeconds(10))
.setInitialSampler(io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOff())
.build()));
}

@Test
void create_SpiExporter() {
void create_SpiExporter_Unknown() {
List<Closeable> closeables = new ArrayList<>();

assertThatThrownBy(
() ->
SamplerFactory.getInstance()
.create(
new Sampler()
.withAdditionalProperty("test", ImmutableMap.of("key1", "value1")),
new io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model
.Sampler()
.withAdditionalProperty(
"unknown_key", ImmutableMap.of("key1", "value1")),
spiHelper,
new ArrayList<>()))
.isInstanceOf(ConfigurationException.class)
.hasMessage("Unrecognized sampler(s): [test]");
.hasMessage(
"No component provider detected for io.opentelemetry.sdk.trace.samplers.Sampler with name \"unknown_key\".");
cleanup.addCloseables(closeables);
}

@Test
void create_SpiExporter_Valid() {
io.opentelemetry.sdk.trace.samplers.Sampler sampler =
SamplerFactory.getInstance()
.create(
new io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Sampler()
.withAdditionalProperty("test", ImmutableMap.of("key1", "value1")),
spiHelper,
new ArrayList<>());
assertThat(sampler).isInstanceOf(SamplerComponentProvider.TestSampler.class);
assertThat(((SamplerComponentProvider.TestSampler) sampler).config.getString("key1"))
.isEqualTo("value1");
}
}
Loading

0 comments on commit 938b03d

Please sign in to comment.