Skip to content

Commit

Permalink
Add SPI support to SamplerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg committed Oct 3, 2023
1 parent 094cd5c commit c8f611e
Showing 1 changed file with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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;
Expand Down Expand Up @@ -116,16 +115,38 @@ public Sampler create(
"jaeger remote sampler"));
}

// 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.isEmpty()) {
for (Map.Entry<String, Object> entry : additionalProperties.entrySet()) {
String samplerName = entry.getKey();
ConfigProperties configProperties =
new ExtendedConfigProperties(toConfigMap(entry.getValue()));
return FileConfigUtil.addAndReturn(
closeables,
FileConfigUtil.assertNotNull(
samplerSpiManager(configProperties, spiHelper).getByName(samplerName),
samplerName));
}
}

return Sampler.parentBased(Sampler.alwaysOn());
}

private static Map<String, Object> toConfigMap(Object object) {
if (!(object instanceof Map)) {
return Collections.emptyMap();
}
Map<String, Object> map = new HashMap<>();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
Object key = entry.getKey();
if (!(key instanceof String)) {
continue;
}
map.put((String) key, entry.getValue());
}
return Collections.unmodifiableMap(map);
}

private static NamedSpiManager<Sampler> samplerSpiManager(
ConfigProperties config, SpiHelper spiHelper) {
return spiHelper.loadConfigurable(
Expand Down

0 comments on commit c8f611e

Please sign in to comment.