-
Notifications
You must be signed in to change notification settings - Fork 834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Sampler configuration factory #5763
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
139 changes: 139 additions & 0 deletions
139
...tor/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/SamplerFactory.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,139 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||
|
||
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; | ||
import javax.annotation.Nullable; | ||
|
||
final class SamplerFactory | ||
implements Factory< | ||
io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Sampler, Sampler> { | ||
|
||
private static final SamplerFactory INSTANCE = new SamplerFactory(); | ||
|
||
private SamplerFactory() {} | ||
|
||
static SamplerFactory getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public Sampler create( | ||
@Nullable io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Sampler model, | ||
SpiHelper spiHelper, | ||
List<Closeable> closeables) { | ||
if (model == null) { | ||
return Sampler.parentBased(Sampler.alwaysOn()); | ||
} | ||
|
||
if (model.getAlwaysOn() != null) { | ||
return Sampler.alwaysOn(); | ||
} | ||
if (model.getAlwaysOff() != null) { | ||
return Sampler.alwaysOff(); | ||
} | ||
TraceIdRatioBased traceIdRatioBasedModel = model.getTraceIdRatioBased(); | ||
if (traceIdRatioBasedModel != null) { | ||
Double ratio = traceIdRatioBasedModel.getRatio(); | ||
if (ratio == null) { | ||
ratio = 1.0d; | ||
} | ||
return Sampler.traceIdRatioBased(ratio); | ||
} | ||
ParentBased parentBasedModel = model.getParentBased(); | ||
if (parentBasedModel != null) { | ||
Sampler root = | ||
parentBasedModel.getRoot() == null | ||
? Sampler.alwaysOn() | ||
: create(parentBasedModel.getRoot(), spiHelper, closeables); | ||
ParentBasedSamplerBuilder builder = Sampler.parentBasedBuilder(root); | ||
if (parentBasedModel.getRemoteParentSampled() != null) { | ||
builder.setRemoteParentSampled( | ||
create(parentBasedModel.getRemoteParentSampled(), spiHelper, closeables)); | ||
} | ||
if (parentBasedModel.getRemoteParentNotSampled() != null) { | ||
builder.setRemoteParentNotSampled( | ||
create(parentBasedModel.getRemoteParentNotSampled(), spiHelper, closeables)); | ||
} | ||
if (parentBasedModel.getLocalParentSampled() != null) { | ||
builder.setLocalParentSampled( | ||
create(parentBasedModel.getLocalParentSampled(), spiHelper, closeables)); | ||
} | ||
if (parentBasedModel.getLocalParentNotSampled() != null) { | ||
builder.setLocalParentNotSampled( | ||
create(parentBasedModel.getLocalParentNotSampled(), spiHelper, closeables)); | ||
} | ||
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(",")); | ||
|
||
// TODO(jack-berg): add method for creating from map | ||
ConfigProperties configProperties = | ||
DefaultConfigProperties.createForTest( | ||
Collections.singletonMap("otel.traces.sampler.arg", otelTraceSamplerArg)); | ||
|
||
return FileConfigUtil.addAndReturn( | ||
closeables, | ||
FileConfigUtil.assertNotNull( | ||
samplerSpiManager(configProperties, spiHelper).getByName("jaeger_remote"), | ||
"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(",", "[", "]"))); | ||
} | ||
|
||
return Sampler.parentBased(Sampler.alwaysOn()); | ||
} | ||
|
||
private static NamedSpiManager<Sampler> samplerSpiManager( | ||
ConfigProperties config, SpiHelper spiHelper) { | ||
return spiHelper.loadConfigurable( | ||
ConfigurableSamplerProvider.class, | ||
ConfigurableSamplerProvider::getName, | ||
ConfigurableSamplerProvider::createSampler, | ||
config); | ||
} | ||
} |
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
142 changes: 142 additions & 0 deletions
142
...src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/SamplerFactoryTest.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,142 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.opentelemetry.internal.testing.CleanupExtension; | ||
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.internal.model.AlwaysOff; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AlwaysOn; | ||
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.Sampler; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.TraceIdRatioBased; | ||
import io.opentelemetry.sdk.extension.trace.jaeger.sampler.JaegerRemoteSampler; | ||
import java.io.Closeable; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import javax.annotation.Nullable; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
// Suppress logs from JaegerRemoteSampler | ||
@SuppressLogger( | ||
loggerName = "io.opentelemetry.sdk.extension.trace.jaeger.sampler.OkHttpGrpcService") | ||
class SamplerFactoryTest { | ||
|
||
@RegisterExtension CleanupExtension cleanup = new CleanupExtension(); | ||
|
||
private final SpiHelper spiHelper = SpiHelper.create(SamplerFactoryTest.class.getClassLoader()); | ||
|
||
@ParameterizedTest | ||
@MethodSource("createArguments") | ||
void create( | ||
@Nullable Sampler model, io.opentelemetry.sdk.trace.samplers.Sampler expectedSampler) { | ||
// Some samplers like JaegerRemoteSampler are Closeable - ensure these get cleaned up | ||
if (expectedSampler instanceof Closeable) { | ||
cleanup.addCloseable((Closeable) expectedSampler); | ||
} | ||
|
||
List<Closeable> closeables = new ArrayList<>(); | ||
io.opentelemetry.sdk.trace.samplers.Sampler sampler = | ||
SamplerFactory.getInstance().create(model, spiHelper, closeables); | ||
cleanup.addCloseables(closeables); | ||
|
||
assertThat(sampler.toString()).isEqualTo(expectedSampler.toString()); | ||
} | ||
|
||
private static Stream<Arguments> createArguments() { | ||
return Stream.of( | ||
Arguments.of( | ||
null, | ||
io.opentelemetry.sdk.trace.samplers.Sampler.parentBased( | ||
io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn())), | ||
Arguments.of( | ||
new Sampler().withAlwaysOn(new AlwaysOn()), | ||
io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn()), | ||
Arguments.of( | ||
new Sampler().withAlwaysOff(new AlwaysOff()), | ||
io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOff()), | ||
Arguments.of( | ||
new Sampler().withTraceIdRatioBased(new TraceIdRatioBased()), | ||
io.opentelemetry.sdk.trace.samplers.Sampler.traceIdRatioBased(1.0d)), | ||
Arguments.of( | ||
new Sampler().withTraceIdRatioBased(new TraceIdRatioBased().withRatio(0.5d)), | ||
io.opentelemetry.sdk.trace.samplers.Sampler.traceIdRatioBased(0.5)), | ||
Arguments.of( | ||
new Sampler().withParentBased(new ParentBased()), | ||
io.opentelemetry.sdk.trace.samplers.Sampler.parentBased( | ||
io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn())), | ||
Arguments.of( | ||
new Sampler() | ||
.withParentBased( | ||
new ParentBased() | ||
.withRoot( | ||
new Sampler() | ||
.withTraceIdRatioBased(new TraceIdRatioBased().withRatio(0.1d))) | ||
.withRemoteParentSampled( | ||
new Sampler() | ||
.withTraceIdRatioBased(new TraceIdRatioBased().withRatio(0.2d))) | ||
.withRemoteParentNotSampled( | ||
new Sampler() | ||
.withTraceIdRatioBased(new TraceIdRatioBased().withRatio(0.3d))) | ||
.withLocalParentSampled( | ||
new Sampler() | ||
.withTraceIdRatioBased(new TraceIdRatioBased().withRatio(0.4d))) | ||
.withLocalParentNotSampled( | ||
new Sampler() | ||
.withTraceIdRatioBased(new TraceIdRatioBased().withRatio(0.5d)))), | ||
io.opentelemetry.sdk.trace.samplers.Sampler.parentBasedBuilder( | ||
io.opentelemetry.sdk.trace.samplers.Sampler.traceIdRatioBased(0.1d)) | ||
.setRemoteParentSampled( | ||
io.opentelemetry.sdk.trace.samplers.Sampler.traceIdRatioBased(0.2d)) | ||
.setRemoteParentNotSampled( | ||
io.opentelemetry.sdk.trace.samplers.Sampler.traceIdRatioBased(0.3d)) | ||
.setLocalParentSampled( | ||
io.opentelemetry.sdk.trace.samplers.Sampler.traceIdRatioBased(0.4d)) | ||
.setLocalParentNotSampled( | ||
io.opentelemetry.sdk.trace.samplers.Sampler.traceIdRatioBased(0.5d)) | ||
.build()), | ||
Arguments.of( | ||
new Sampler() | ||
.withJaegerRemote( | ||
new JaegerRemote() | ||
.withEndpoint("http://jaeger-remote-endpoint") | ||
.withInterval(10_000) | ||
.withInitialSampler(new Sampler().withAlwaysOff(new AlwaysOff()))), | ||
JaegerRemoteSampler.builder() | ||
.setEndpoint("http://jaeger-remote-endpoint") | ||
.setPollingInterval(Duration.ofSeconds(10)) | ||
.build())); | ||
} | ||
|
||
@Test | ||
void create_SpiExporter() { | ||
List<Closeable> closeables = new ArrayList<>(); | ||
|
||
assertThatThrownBy( | ||
() -> | ||
SamplerFactory.getInstance() | ||
.create( | ||
new Sampler() | ||
.withAdditionalProperty("test", ImmutableMap.of("key1", "value1")), | ||
spiHelper, | ||
new ArrayList<>())) | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessage("Unrecognized sampler(s): [test]"); | ||
cleanup.addCloseables(closeables); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that because the Jaeger sampler is
Closeable
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes