Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,12 @@
import java.util.List;

final class ComposableProbabilitySampler implements ComposableSampler {
private static void validateRatio(double ratio) {
if (Double.isNaN(ratio) || ratio < 0.0 || ratio > 1.0) {
throw new IllegalArgumentException("ratio must be in range [0.0, 1.0]");
}
}

private static long calculateThreshold(double ratio) {
return ImmutableSamplingIntent.MAX_THRESHOLD
- Math.round(ratio * (double) ImmutableSamplingIntent.MAX_THRESHOLD);
Expand All @@ -21,6 +27,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 @@ -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 Down Expand Up @@ -77,4 +81,20 @@ 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("invalidRatios")
void invalidRatioThrows(double ratio) {
assertThatThrownBy(() -> ComposableSampler.probability(ratio))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("ratio must be in range [0.0, 1.0]");
}

@SuppressWarnings("unused")
static Stream<Arguments> invalidRatios() {
return Stream.of(
Arguments.argumentSet("less than 0.0", -0.5),
Arguments.argumentSet("greater than 1.0", 2.0),
Arguments.argumentSet("NaN", Double.NaN));
}
}
Loading