|
| 1 | +package io.quarkus.opentelemetry.runtime.tracing; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.atomic.AtomicLong; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 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.data.LinkData; |
| 14 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 15 | +import io.opentelemetry.sdk.trace.samplers.SamplingResult; |
| 16 | +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; |
| 17 | + |
| 18 | +class DropTargetsSamplerTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + void testDropTargets() { |
| 22 | + CountingSampler countingSampler = new CountingSampler(); |
| 23 | + var sut = new DropTargetsSampler(countingSampler, List.of("/q/swagger-ui", "/q/swagger-ui*")); |
| 24 | + |
| 25 | + assertEquals(SamplingResult.recordAndSample(), getShouldSample(sut, "/other")); |
| 26 | + assertEquals(1, countingSampler.count.get()); |
| 27 | + |
| 28 | + assertEquals(SamplingResult.drop(), getShouldSample(sut, "/q/swagger-ui")); |
| 29 | + assertEquals(1, countingSampler.count.get()); |
| 30 | + |
| 31 | + assertEquals(SamplingResult.drop(), getShouldSample(sut, "/q/swagger-ui/")); |
| 32 | + assertEquals(1, countingSampler.count.get()); |
| 33 | + |
| 34 | + assertEquals(SamplingResult.drop(), getShouldSample(sut, "/q/swagger-ui/whatever")); |
| 35 | + assertEquals(1, countingSampler.count.get()); |
| 36 | + |
| 37 | + assertEquals(SamplingResult.recordAndSample(), getShouldSample(sut, "/q/test")); |
| 38 | + assertEquals(2, countingSampler.count.get()); |
| 39 | + } |
| 40 | + |
| 41 | + private static SamplingResult getShouldSample(DropTargetsSampler sut, String target) { |
| 42 | + return sut.shouldSample(null, null, null, SpanKind.SERVER, |
| 43 | + Attributes.of(SemanticAttributes.HTTP_TARGET, target), null); |
| 44 | + } |
| 45 | + |
| 46 | + private static final class CountingSampler implements Sampler { |
| 47 | + |
| 48 | + final AtomicLong count = new AtomicLong(0); |
| 49 | + |
| 50 | + @Override |
| 51 | + public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, |
| 52 | + Attributes attributes, List<LinkData> parentLinks) { |
| 53 | + count.incrementAndGet(); |
| 54 | + return SamplingResult.recordAndSample(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getDescription() { |
| 59 | + return "test"; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments