Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
import java.util.List;

final class ComposableProbabilitySampler implements ComposableSampler {
private static final double MIN_RATIO = 1.0 / ImmutableSamplingIntent.MAX_THRESHOLD;

private static void validateRatio(double ratio) {
if (Double.isNaN(ratio) || (ratio != 0.0 && ratio < MIN_RATIO) || ratio > 1.0) {
throw new IllegalArgumentException("ratio must be 0.0 or in range [2^-56, 1.0]");
}
}

private static long calculateThreshold(double ratio) {
return ImmutableSamplingIntent.MAX_THRESHOLD
- Math.round(ratio * (double) ImmutableSamplingIntent.MAX_THRESHOLD);
Expand All @@ -21,6 +29,8 @@ private static long calculateThreshold(double ratio) {
private final String description;

ComposableProbabilitySampler(double ratio) {
validateRatio(ratio);

long threshold = calculateThreshold(ratio);
String thresholdStr;
if (threshold == ImmutableSamplingIntent.MAX_THRESHOLD) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ static ComposableSampler alwaysOn() {
return ComposableAlwaysOnSampler.INSTANCE;
}

/** Returns a {@link ComposableSampler} that samples each span with a fixed ratio. */
/**
* Returns a {@link ComposableSampler} that samples each span with a fixed ratio.
*
* @param ratio the sampling probability in the inclusive range [2^-56, 1.0], or zero to drop all
* spans
* @return the composable probability sampler
* @throws IllegalArgumentException if the ratio is NaN or is neither zero nor in the inclusive
* range [2^-56, 1.0]
*/
static ComposableSampler probability(double ratio) {
return new ComposableProbabilitySampler(ratio);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.sdk.extension.incubator.trace.samplers.TestUtil.traceIdGenerator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
Expand All @@ -17,9 +18,12 @@
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.util.Collections;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;

class ComposableProbabilitySamplerTest {
@Test
Expand All @@ -33,8 +37,6 @@ void testDescription() {
.isEqualTo("ComposableProbabilitySampler{threshold=8, ratio=0.5}");
assertThat(ComposableSampler.probability(0.25).getDescription())
.isEqualTo("ComposableProbabilitySampler{threshold=c, ratio=0.25}");
assertThat(ComposableSampler.probability(1e-300).getDescription())
.isEqualTo("ComposableProbabilitySampler{threshold=max, ratio=1.0E-300}");
assertThat(ComposableSampler.probability(0).getDescription())
.isEqualTo("ComposableProbabilitySampler{threshold=max, ratio=0.0}");
}
Expand Down Expand Up @@ -77,4 +79,50 @@ void sampling(double ratio, long threshold) {
// But our random seed in TestUtil conveniently allows the test to pass so don't bother.
assertThat(Math.abs(numSampled - expectedNumSampled)).isLessThan(50);
}

@ParameterizedTest
@MethodSource("validBoundaryRatios")
void validBoundaryRatio(double ratio, long threshold, boolean adjustedCountReliable) {
SamplingIntent intent =
ComposableSampler.probability(ratio)
.getSamplingIntent(
Context.root(),
"span",
SpanKind.SERVER,
Attributes.empty(),
Collections.emptyList());

assertThat(intent.getThreshold()).isEqualTo(threshold);
assertThat(intent.isAdjustedCountReliable()).isEqualTo(adjustedCountReliable);
}

static Stream<Arguments> validBoundaryRatios() {
return Stream.of(
Arguments.argumentSet("zero", 0.0, ImmutableSamplingIntent.INVALID_THRESHOLD, false),
Arguments.argumentSet(
"negative zero", -0.0, ImmutableSamplingIntent.INVALID_THRESHOLD, false),
Arguments.argumentSet("minimum", Math.scalb(1.0, -56), (1L << 56) - 1, true),
Arguments.argumentSet("maximum", 1.0, 0L, true));
}

@ParameterizedTest
@MethodSource("invalidRatios")
void invalidRatioThrows(double ratio) {
assertThatThrownBy(() -> ComposableSampler.probability(ratio))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("ratio must be 0.0 or in range [2^-56, 1.0]");
}

@SuppressWarnings("unused")
static Stream<Arguments> invalidRatios() {
return Stream.of(
Arguments.argumentSet("less than 0.0", -0.5),
Arguments.argumentSet("tiny positive", 1e-300),
Arguments.argumentSet("just below minimum", Math.nextDown(Math.scalb(1.0, -56))),
Arguments.argumentSet("greater than 1.0", 2.0),
Arguments.argumentSet("just above maximum", Math.nextUp(1.0)),
Arguments.argumentSet("negative infinity", Double.NEGATIVE_INFINITY),
Arguments.argumentSet("positive infinity", Double.POSITIVE_INFINITY),
Arguments.argumentSet("NaN", Double.NaN));
}
}
Loading