diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentAlwaysOffSampler.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentAlwaysOffSampler.java index 37759d670..d6d2cd686 100644 --- a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentAlwaysOffSampler.java +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentAlwaysOffSampler.java @@ -10,12 +10,8 @@ @Immutable final class ConsistentAlwaysOffSampler extends ConsistentSampler { - private ConsistentAlwaysOffSampler() {} - - private static final ConsistentSampler INSTANCE = new ConsistentAlwaysOffSampler(); - - static ConsistentSampler getInstance() { - return INSTANCE; + ConsistentAlwaysOffSampler(RValueGenerator rValueGenerator) { + super(rValueGenerator); } @Override diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentAlwaysOnSampler.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentAlwaysOnSampler.java index 9ca49bd0d..ec1d112a2 100644 --- a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentAlwaysOnSampler.java +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentAlwaysOnSampler.java @@ -10,12 +10,8 @@ @Immutable final class ConsistentAlwaysOnSampler extends ConsistentSampler { - private ConsistentAlwaysOnSampler() {} - - private static final ConsistentSampler INSTANCE = new ConsistentAlwaysOnSampler(); - - static ConsistentSampler getInstance() { - return INSTANCE; + ConsistentAlwaysOnSampler(RValueGenerator rValueGenerator) { + super(rValueGenerator); } @Override diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentComposedAndSampler.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentComposedAndSampler.java index 17f758b46..7b17fdd52 100644 --- a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentComposedAndSampler.java +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentComposedAndSampler.java @@ -21,7 +21,9 @@ final class ConsistentComposedAndSampler extends ConsistentSampler { private final ConsistentSampler sampler2; private final String description; - ConsistentComposedAndSampler(ConsistentSampler sampler1, ConsistentSampler sampler2) { + ConsistentComposedAndSampler( + ConsistentSampler sampler1, ConsistentSampler sampler2, RValueGenerator rValueGenerator) { + super(rValueGenerator); this.sampler1 = requireNonNull(sampler1); this.sampler2 = requireNonNull(sampler2); this.description = diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentComposedOrSampler.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentComposedOrSampler.java index b090dd399..b0c1de91e 100644 --- a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentComposedOrSampler.java +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentComposedOrSampler.java @@ -21,7 +21,9 @@ final class ConsistentComposedOrSampler extends ConsistentSampler { private final ConsistentSampler sampler2; private final String description; - ConsistentComposedOrSampler(ConsistentSampler sampler1, ConsistentSampler sampler2) { + ConsistentComposedOrSampler( + ConsistentSampler sampler1, ConsistentSampler sampler2, RValueGenerator rValueGenerator) { + super(rValueGenerator); this.sampler1 = requireNonNull(sampler1); this.sampler2 = requireNonNull(sampler2); this.description = diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentParentBasedSampler.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentParentBasedSampler.java index 4db1ad196..54bf8d8d8 100644 --- a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentParentBasedSampler.java +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentParentBasedSampler.java @@ -21,25 +21,15 @@ final class ConsistentParentBasedSampler extends ConsistentSampler { private final String description; - /** - * Constructs a new consistent parent based sampler using the given root sampler. - * - * @param rootSampler the root sampler - */ - ConsistentParentBasedSampler(ConsistentSampler rootSampler) { - this(rootSampler, RandomGenerator.getDefault()); - } - /** * Constructs a new consistent parent based sampler using the given root sampler and the given * thread-safe random generator. * * @param rootSampler the root sampler - * @param threadSafeRandomGenerator a thread-safe random generator + * @param rValueGenerator the function to use for generating the r-value */ - ConsistentParentBasedSampler( - ConsistentSampler rootSampler, RandomGenerator threadSafeRandomGenerator) { - super(threadSafeRandomGenerator); + ConsistentParentBasedSampler(ConsistentSampler rootSampler, RValueGenerator rValueGenerator) { + super(rValueGenerator); this.rootSampler = requireNonNull(rootSampler); this.description = "ConsistentParentBasedSampler{rootSampler=" + rootSampler.getDescription() + '}'; diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentProbabilityBasedSampler.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentProbabilityBasedSampler.java index 2e55d7409..536bbea49 100644 --- a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentProbabilityBasedSampler.java +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentProbabilityBasedSampler.java @@ -15,29 +15,25 @@ final class ConsistentProbabilityBasedSampler extends ConsistentSampler { private final int upperPValue; private final double probabilityToUseLowerPValue; private final String description; + private final RandomGenerator randomGenerator; /** * Constructor. * * @param samplingProbability the sampling probability + * @param rValueGenerator the function to use for generating the r-value */ - ConsistentProbabilityBasedSampler(double samplingProbability) { - this(samplingProbability, RandomGenerator.getDefault()); - } - - /** - * Constructor. - * - * @param samplingProbability the sampling probability - * @param randomGenerator a random generator - */ - ConsistentProbabilityBasedSampler(double samplingProbability, RandomGenerator randomGenerator) { - super(randomGenerator); + ConsistentProbabilityBasedSampler( + double samplingProbability, + RValueGenerator rValueGenerator, + RandomGenerator randomGenerator) { + super(rValueGenerator); if (samplingProbability < 0.0 || samplingProbability > 1.0) { throw new IllegalArgumentException("Sampling probability must be in range [0.0, 1.0]!"); } this.description = String.format("ConsistentProbabilityBasedSampler{%.6f}", samplingProbability); + this.randomGenerator = randomGenerator; lowerPValue = getLowerBoundP(samplingProbability); upperPValue = getUpperBoundP(samplingProbability); diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentRateLimitingSampler.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentRateLimitingSampler.java index 9f1edbbd1..6771f7052 100644 --- a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentRateLimitingSampler.java +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentRateLimitingSampler.java @@ -85,6 +85,7 @@ public State(double effectiveWindowCount, double effectiveWindowNanos, long last private final double inverseAdaptationTimeNanos; private final double targetSpansPerNanosecondLimit; private final AtomicReference state; + private final RandomGenerator randomGenerator; /** * Constructor. @@ -92,30 +93,17 @@ public State(double effectiveWindowCount, double effectiveWindowNanos, long last * @param targetSpansPerSecondLimit the desired spans per second limit * @param adaptationTimeSeconds the typical time to adapt to a new load (time constant used for * exponential smoothing) - */ - ConsistentRateLimitingSampler(double targetSpansPerSecondLimit, double adaptationTimeSeconds) { - this( - targetSpansPerSecondLimit, - adaptationTimeSeconds, - RandomGenerator.getDefault(), - System::nanoTime); - } - - /** - * Constructor. - * - * @param targetSpansPerSecondLimit the desired spans per second limit - * @param adaptationTimeSeconds the typical time to adapt to a new load (time constant used for - * exponential smoothing) + * @param rValueGenerator the function to use for generating the r-value * @param randomGenerator a random generator * @param nanoTimeSupplier a supplier for the current nano time */ ConsistentRateLimitingSampler( double targetSpansPerSecondLimit, double adaptationTimeSeconds, + RValueGenerator rValueGenerator, RandomGenerator randomGenerator, LongSupplier nanoTimeSupplier) { - super(randomGenerator); + super(rValueGenerator); if (targetSpansPerSecondLimit < 0.0) { throw new IllegalArgumentException("Limit for sampled spans per second must be nonnegative!"); @@ -133,6 +121,8 @@ public State(double effectiveWindowCount, double effectiveWindowNanos, long last this.targetSpansPerNanosecondLimit = 1e-9 * targetSpansPerSecondLimit; this.state = new AtomicReference<>(new State(0, 0, nanoTimeSupplier.getAsLong())); + + this.randomGenerator = randomGenerator; } private State updateState(State oldState, long currentNanoTime) { diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentSampler.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentSampler.java index 6f2e59d5a..877f30fb5 100644 --- a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentSampler.java +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/ConsistentSampler.java @@ -28,17 +28,37 @@ public abstract class ConsistentSampler implements Sampler { * * @return a sampler */ - public static final ConsistentSampler alwaysOn() { - return ConsistentAlwaysOnSampler.getInstance(); + public static ConsistentSampler alwaysOn() { + return alwaysOn(RValueGenerators.getDefault()); + } + + /** + * Returns a {@link ConsistentSampler} that samples all spans. + * + * @param rValueGenerator the function to use for generating the r-value + * @return a sampler + */ + public static ConsistentSampler alwaysOn(RValueGenerator rValueGenerator) { + return new ConsistentAlwaysOnSampler(rValueGenerator); + } + + /** + * Returns a {@link ConsistentSampler} that does not sample any span. + * + * @return a sampler + */ + public static ConsistentSampler alwaysOff() { + return alwaysOff(RValueGenerators.getDefault()); } /** * Returns a {@link ConsistentSampler} that does not sample any span. * + * @param rValueGenerator the function to use for generating the r-value * @return a sampler */ - public static final ConsistentSampler alwaysOff() { - return ConsistentAlwaysOffSampler.getInstance(); + public static ConsistentSampler alwaysOff(RValueGenerator rValueGenerator) { + return new ConsistentAlwaysOffSampler(rValueGenerator); } /** @@ -47,20 +67,21 @@ public static final ConsistentSampler alwaysOff() { * @param samplingProbability the sampling probability * @return a sampler */ - public static final ConsistentSampler probabilityBased(double samplingProbability) { - return new ConsistentProbabilityBasedSampler(samplingProbability); + public static ConsistentSampler probabilityBased(double samplingProbability) { + return probabilityBased(samplingProbability, RValueGenerators.getDefault()); } /** * Returns a {@link ConsistentSampler} that samples each span with a fixed probability. * * @param samplingProbability the sampling probability - * @param randomGenerator a random generator + * @param rValueGenerator the function to use for generating the r-value * @return a sampler */ - static final ConsistentSampler probabilityBased( - double samplingProbability, RandomGenerator randomGenerator) { - return new ConsistentProbabilityBasedSampler(samplingProbability, randomGenerator); + public static ConsistentSampler probabilityBased( + double samplingProbability, RValueGenerator rValueGenerator) { + return new ConsistentProbabilityBasedSampler( + samplingProbability, rValueGenerator, RandomGenerator.getDefault()); } /** @@ -69,8 +90,8 @@ static final ConsistentSampler probabilityBased( * * @param rootSampler the root sampler */ - public static final ConsistentSampler parentBased(ConsistentSampler rootSampler) { - return new ConsistentParentBasedSampler(rootSampler); + public static ConsistentSampler parentBased(ConsistentSampler rootSampler) { + return parentBased(rootSampler, RValueGenerators.getDefault()); } /** @@ -78,11 +99,11 @@ public static final ConsistentSampler parentBased(ConsistentSampler rootSampler) * or falls-back to the given sampler if it is a root span. * * @param rootSampler the root sampler - * @param randomGenerator a random generator + * @param rValueGenerator the function to use for generating the r-value */ - static final ConsistentSampler parentBased( - ConsistentSampler rootSampler, RandomGenerator randomGenerator) { - return new ConsistentParentBasedSampler(rootSampler, randomGenerator); + public static ConsistentSampler parentBased( + ConsistentSampler rootSampler, RValueGenerator rValueGenerator) { + return new ConsistentParentBasedSampler(rootSampler, rValueGenerator); } /** @@ -93,9 +114,10 @@ static final ConsistentSampler parentBased( * @param adaptationTimeSeconds the typical time to adapt to a new load (time constant used for * exponential smoothing) */ - public static final ConsistentSampler rateLimited( + public static ConsistentSampler rateLimited( double targetSpansPerSecondLimit, double adaptationTimeSeconds) { - return new ConsistentRateLimitingSampler(targetSpansPerSecondLimit, adaptationTimeSeconds); + return rateLimited( + targetSpansPerSecondLimit, adaptationTimeSeconds, RValueGenerators.getDefault()); } /** @@ -105,16 +127,37 @@ public static final ConsistentSampler rateLimited( * @param targetSpansPerSecondLimit the desired spans per second limit * @param adaptationTimeSeconds the typical time to adapt to a new load (time constant used for * exponential smoothing) - * @param randomGenerator a random generator + * @param rValueGenerator the function to use for generating the r-value + */ + public static ConsistentSampler rateLimited( + double targetSpansPerSecondLimit, + double adaptationTimeSeconds, + RValueGenerator rValueGenerator) { + return rateLimited( + targetSpansPerSecondLimit, adaptationTimeSeconds, rValueGenerator, System::nanoTime); + } + + /** + * Returns a new {@link ConsistentSampler} that attempts to adjust the sampling probability + * dynamically to meet the target span rate. + * + * @param targetSpansPerSecondLimit the desired spans per second limit + * @param adaptationTimeSeconds the typical time to adapt to a new load (time constant used for + * exponential smoothing) + * @param rValueGenerator the function to use for generating the r-value * @param nanoTimeSupplier a supplier for the current nano time */ - static final ConsistentSampler rateLimited( + static ConsistentSampler rateLimited( double targetSpansPerSecondLimit, double adaptationTimeSeconds, - RandomGenerator randomGenerator, + RValueGenerator rValueGenerator, LongSupplier nanoTimeSupplier) { return new ConsistentRateLimitingSampler( - targetSpansPerSecondLimit, adaptationTimeSeconds, randomGenerator, nanoTimeSupplier); + targetSpansPerSecondLimit, + adaptationTimeSeconds, + rValueGenerator, + RandomGenerator.getDefault(), + nanoTimeSupplier); } /** @@ -136,7 +179,8 @@ public ConsistentSampler and(ConsistentSampler otherConsistentSampler) { if (otherConsistentSampler == this) { return this; } - return new ConsistentComposedAndSampler(this, otherConsistentSampler); + return new ConsistentComposedAndSampler( + this, otherConsistentSampler, RValueGenerators.getDefault()); } /** @@ -158,20 +202,17 @@ public ConsistentSampler or(ConsistentSampler otherConsistentSampler) { if (otherConsistentSampler == this) { return this; } - return new ConsistentComposedOrSampler(this, otherConsistentSampler); + return new ConsistentComposedOrSampler( + this, otherConsistentSampler, RValueGenerators.getDefault()); } - protected final RandomGenerator randomGenerator; - - protected ConsistentSampler(RandomGenerator randomGenerator) { - this.randomGenerator = requireNonNull(randomGenerator); - } + private final RValueGenerator rValueGenerator; - protected ConsistentSampler() { - this(RandomGenerator.getDefault()); + protected ConsistentSampler(RValueGenerator rValueGenerator) { + this.rValueGenerator = requireNonNull(rValueGenerator); } - private static final boolean isInvariantViolated( + private static boolean isInvariantViolated( OtelTraceState otelTraceState, boolean isParentSampled) { if (otelTraceState.hasValidR() && otelTraceState.hasValidP()) { // if valid p- and r-values are given, they must be consistent with the isParentSampled flag @@ -212,8 +253,7 @@ public final SamplingResult shouldSample( // generate new r-value if not available if (!otelTraceState.hasValidR()) { - otelTraceState.setR( - Math.min(randomGenerator.numberOfLeadingZerosOfRandomLong(), OtelTraceState.getMaxR())); + otelTraceState.setR(Math.min(rValueGenerator.generate(traceId), OtelTraceState.getMaxR())); } // determine and set new p-value that is used for the sampling decision diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/RValueGenerator.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/RValueGenerator.java new file mode 100644 index 000000000..511d80f30 --- /dev/null +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/RValueGenerator.java @@ -0,0 +1,41 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.contrib.samplers; + +/** + * A function for generating r-values. + * + *

The distribution of r-values generated by this function must satisfy the table below. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Required distribution of r-values
r-valueProbability of r-value
01/2
11/4
21/8
31/16
0 <= r <= 612**-(r+1)
592**-60
602**-61
612**-62
>=622**-62
+ * + * For more info see Methods + * for generating R-values. + */ +@FunctionalInterface +public interface RValueGenerator { + + int generate(String traceId); +} diff --git a/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/RValueGenerators.java b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/RValueGenerators.java new file mode 100644 index 000000000..03daafc02 --- /dev/null +++ b/consistent-sampling/src/main/java/io/opentelemetry/contrib/samplers/RValueGenerators.java @@ -0,0 +1,22 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.contrib.samplers; + +final class RValueGenerators { + + private static final RValueGenerator DEFAULT = createDefault(); + + static RValueGenerator getDefault() { + return DEFAULT; + } + + private static RValueGenerator createDefault() { + RandomGenerator randomGenerator = RandomGenerator.getDefault(); + return s -> randomGenerator.numberOfLeadingZerosOfRandomLong(); + } + + private RValueGenerators() {} +} diff --git a/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentProbabilityBasedSamplerTest.java b/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentProbabilityBasedSamplerTest.java index e37861d24..60391cf87 100644 --- a/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentProbabilityBasedSamplerTest.java +++ b/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentProbabilityBasedSamplerTest.java @@ -50,7 +50,8 @@ private void test(SplittableRandom rng, double samplingProbability) { Sampler sampler = ConsistentSampler.probabilityBased( - samplingProbability, RandomGenerator.create(rng::nextLong)); + samplingProbability, + s -> RandomGenerator.create(rng::nextLong).numberOfLeadingZerosOfRandomLong()); Map observedPvalues = new HashMap<>(); for (long i = 0; i < numSpans; ++i) { diff --git a/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentRateLimitingSamplerTest.java b/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentRateLimitingSamplerTest.java index c54d22d2f..f77c67294 100644 --- a/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentRateLimitingSamplerTest.java +++ b/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentRateLimitingSamplerTest.java @@ -59,14 +59,10 @@ void testConstantRate() { double targetSpansPerSecondLimit = 1000; double adaptationTimeSeconds = 5; - SplittableRandom random = new SplittableRandom(0L); ConsistentSampler sampler = ConsistentSampler.rateLimited( - targetSpansPerSecondLimit, - adaptationTimeSeconds, - RandomGenerator.create(random::nextLong), - nanoTimeSupplier); + targetSpansPerSecondLimit, adaptationTimeSeconds, rValueGenerator(), nanoTimeSupplier); long nanosBetweenSpans = TimeUnit.MICROSECONDS.toNanos(100); int numSpans = 1000000; @@ -96,14 +92,10 @@ void testRateIncrease() { double targetSpansPerSecondLimit = 1000; double adaptationTimeSeconds = 5; - SplittableRandom random = new SplittableRandom(0L); ConsistentSampler sampler = ConsistentSampler.rateLimited( - targetSpansPerSecondLimit, - adaptationTimeSeconds, - RandomGenerator.create(random::nextLong), - nanoTimeSupplier); + targetSpansPerSecondLimit, adaptationTimeSeconds, rValueGenerator(), nanoTimeSupplier); long nanosBetweenSpans1 = TimeUnit.MICROSECONDS.toNanos(100); long nanosBetweenSpans2 = TimeUnit.MICROSECONDS.toNanos(10); @@ -155,14 +147,10 @@ void testRateDecrease() { double targetSpansPerSecondLimit = 1000; double adaptationTimeSeconds = 5; - SplittableRandom random = new SplittableRandom(0L); ConsistentSampler sampler = ConsistentSampler.rateLimited( - targetSpansPerSecondLimit, - adaptationTimeSeconds, - RandomGenerator.create(random::nextLong), - nanoTimeSupplier); + targetSpansPerSecondLimit, adaptationTimeSeconds, rValueGenerator(), nanoTimeSupplier); long nanosBetweenSpans1 = TimeUnit.MICROSECONDS.toNanos(10); long nanosBetweenSpans2 = TimeUnit.MICROSECONDS.toNanos(100); @@ -208,4 +196,10 @@ void testRateDecrease() { assertThat(numSampledSpansInLast5Seconds / 5.) .isCloseTo(targetSpansPerSecondLimit, Percentage.withPercentage(5)); } + + private static RValueGenerator rValueGenerator() { + SplittableRandom random = new SplittableRandom(0L); + RandomGenerator randomGenerator = RandomGenerator.create(random::nextLong); + return s -> randomGenerator.numberOfLeadingZerosOfRandomLong(); + } } diff --git a/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentReservoirSamplingSpanProcessorTest.java b/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentReservoirSamplingSpanProcessorTest.java index 8c20a644b..5dd923279 100644 --- a/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentReservoirSamplingSpanProcessorTest.java +++ b/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentReservoirSamplingSpanProcessorTest.java @@ -526,11 +526,12 @@ private void testConsistentSampling( DEFAULT_EXPORT_TIMEOUT_NANOS, RandomGenerator.create(asThreadSafeLongSupplier(rng1))); + RandomGenerator randomGenerator = RandomGenerator.create(asThreadSafeLongSupplier(rng2)); SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder() .setSampler( ConsistentSampler.probabilityBased( - samplingProbability, RandomGenerator.create(asThreadSafeLongSupplier(rng2)))) + samplingProbability, s -> randomGenerator.numberOfLeadingZerosOfRandomLong())) .addSpanProcessor(processor) .build(); diff --git a/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentSamplerTest.java b/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentSamplerTest.java index 03e4b74d9..84bf4c316 100644 --- a/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentSamplerTest.java +++ b/consistent-sampling/src/test/java/io/opentelemetry/contrib/samplers/ConsistentSamplerTest.java @@ -90,8 +90,9 @@ void testRandomValues() { private static ConsistentSampler createConsistentSampler(int p, int r) { long randomLong = ~(0xFFFFFFFFFFFFFFFFL << r); + RandomGenerator randomGenerator = RandomGenerator.create(() -> randomLong); - return new ConsistentSampler(RandomGenerator.create(() -> randomLong)) { + return new ConsistentSampler(s -> randomGenerator.numberOfLeadingZerosOfRandomLong()) { @Override public String getDescription() { throw new UnsupportedOperationException();