|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.dynamic.sampler; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import io.opentelemetry.api.common.Attributes; |
| 11 | +import io.opentelemetry.api.trace.SpanKind; |
| 12 | +import io.opentelemetry.context.Context; |
| 13 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 14 | +import io.opentelemetry.sdk.trace.samplers.SamplingDecision; |
| 15 | +import io.opentelemetry.sdk.trace.samplers.SamplingResult; |
| 16 | +import java.util.Collections; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +class DelegatingSamplerTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + void defaultDelegateIsAlwaysOn() { |
| 23 | + DelegatingSampler sampler = new DelegatingSampler(); |
| 24 | + |
| 25 | + SamplingDecision decision = doSample(sampler); |
| 26 | + |
| 27 | + assertThat(decision).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void setDelegateNullFallsBackToAlwaysOn() { |
| 32 | + DelegatingSampler sampler = new DelegatingSampler(Sampler.alwaysOff()); |
| 33 | + |
| 34 | + sampler.setDelegate(null); |
| 35 | + |
| 36 | + SamplingDecision decision = doSample(sampler); |
| 37 | + assertThat(decision).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void shouldSampleDelegatesToCurrentSamplerAfterUpdate() { |
| 42 | + DelegatingSampler sampler = new DelegatingSampler(Sampler.alwaysOff()); |
| 43 | + |
| 44 | + assertThat(doSample(sampler)).isEqualTo(SamplingDecision.DROP); |
| 45 | + |
| 46 | + sampler.setDelegate(Sampler.alwaysOn()); |
| 47 | + |
| 48 | + assertThat(doSample(sampler)).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); |
| 49 | + } |
| 50 | + |
| 51 | + private static SamplingDecision doSample(DelegatingSampler sampler) { |
| 52 | + SamplingResult result = |
| 53 | + sampler.shouldSample( |
| 54 | + Context.root(), |
| 55 | + "00000000000000000000000000000001", |
| 56 | + "test-span", |
| 57 | + SpanKind.INTERNAL, |
| 58 | + Attributes.empty(), |
| 59 | + Collections.emptyList()); |
| 60 | + return result.getDecision(); |
| 61 | + } |
| 62 | +} |
0 commit comments