|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package io.opentelemetry.contrib.samplers; |
| 6 | + |
| 7 | +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_TARGET; |
| 8 | +import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_URL; |
| 9 | +import static java.util.Collections.emptyList; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 12 | +import static org.mockito.ArgumentMatchers.any; |
| 13 | +import static org.mockito.Mockito.clearInvocations; |
| 14 | +import static org.mockito.Mockito.verify; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | + |
| 17 | +import io.opentelemetry.api.common.Attributes; |
| 18 | +import io.opentelemetry.api.trace.Span; |
| 19 | +import io.opentelemetry.api.trace.SpanContext; |
| 20 | +import io.opentelemetry.api.trace.SpanKind; |
| 21 | +import io.opentelemetry.api.trace.TraceFlags; |
| 22 | +import io.opentelemetry.api.trace.TraceState; |
| 23 | +import io.opentelemetry.context.Context; |
| 24 | +import io.opentelemetry.sdk.trace.IdGenerator; |
| 25 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 26 | +import io.opentelemetry.sdk.trace.samplers.SamplingDecision; |
| 27 | +import io.opentelemetry.sdk.trace.samplers.SamplingResult; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 33 | +import org.mockito.Mock; |
| 34 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 35 | + |
| 36 | +@ExtendWith(MockitoExtension.class) |
| 37 | +class RuleBasedRoutingSamplerTest { |
| 38 | + private static final String SPAN_NAME = "MySpanName"; |
| 39 | + private static final SpanKind SPAN_KIND = SpanKind.SERVER; |
| 40 | + private final IdGenerator idsGenerator = IdGenerator.random(); |
| 41 | + private final String traceId = idsGenerator.generateTraceId(); |
| 42 | + private final String parentSpanId = idsGenerator.generateSpanId(); |
| 43 | + private final SpanContext sampledSpanContext = |
| 44 | + SpanContext.create(traceId, parentSpanId, TraceFlags.getSampled(), TraceState.getDefault()); |
| 45 | + private final Context parentContext = Context.root().with(Span.wrap(sampledSpanContext)); |
| 46 | + |
| 47 | + private final List<SamplingRule> patterns = new ArrayList<>(); |
| 48 | + |
| 49 | + @Mock(lenient = true) |
| 50 | + private Sampler delegate; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setup() { |
| 54 | + when(delegate.shouldSample(any(), any(), any(), any(), any(), any())) |
| 55 | + .thenReturn(SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE)); |
| 56 | + |
| 57 | + patterns.add(new SamplingRule(HTTP_URL, ".*/healthcheck", Sampler.alwaysOff())); |
| 58 | + patterns.add(new SamplingRule(HTTP_TARGET, "/actuator", Sampler.alwaysOff())); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testThatThrowsOnNullParameter() { |
| 63 | + assertThatExceptionOfType(NullPointerException.class) |
| 64 | + .isThrownBy(() -> new RuleBasedRoutingSampler(patterns, SPAN_KIND, null)); |
| 65 | + |
| 66 | + assertThatExceptionOfType(NullPointerException.class) |
| 67 | + .isThrownBy(() -> new RuleBasedRoutingSampler(null, SPAN_KIND, delegate)); |
| 68 | + |
| 69 | + assertThatExceptionOfType(NullPointerException.class) |
| 70 | + .isThrownBy(() -> new RuleBasedRoutingSampler(patterns, null, delegate)); |
| 71 | + |
| 72 | + assertThatExceptionOfType(NullPointerException.class) |
| 73 | + .isThrownBy(() -> RuleBasedRoutingSampler.builder(SPAN_KIND, null)); |
| 74 | + |
| 75 | + assertThatExceptionOfType(NullPointerException.class) |
| 76 | + .isThrownBy(() -> RuleBasedRoutingSampler.builder(null, delegate)); |
| 77 | + |
| 78 | + assertThatExceptionOfType(NullPointerException.class) |
| 79 | + .isThrownBy(() -> RuleBasedRoutingSampler.builder(SPAN_KIND, delegate).drop(null, "")); |
| 80 | + |
| 81 | + assertThatExceptionOfType(NullPointerException.class) |
| 82 | + .isThrownBy( |
| 83 | + () -> RuleBasedRoutingSampler.builder(SPAN_KIND, delegate).drop(HTTP_URL, null)); |
| 84 | + |
| 85 | + assertThatExceptionOfType(NullPointerException.class) |
| 86 | + .isThrownBy( |
| 87 | + () -> RuleBasedRoutingSampler.builder(SPAN_KIND, delegate).recordAndSample(null, "")); |
| 88 | + |
| 89 | + assertThatExceptionOfType(NullPointerException.class) |
| 90 | + .isThrownBy( |
| 91 | + () -> |
| 92 | + RuleBasedRoutingSampler.builder(SPAN_KIND, delegate) |
| 93 | + .recordAndSample(HTTP_URL, null)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testThatDelegatesIfNoRulesGiven() { |
| 98 | + RuleBasedRoutingSampler sampler = RuleBasedRoutingSampler.builder(SPAN_KIND, delegate).build(); |
| 99 | + |
| 100 | + // no http.url attribute |
| 101 | + Attributes attributes = Attributes.empty(); |
| 102 | + sampler.shouldSample(parentContext, traceId, SPAN_NAME, SPAN_KIND, attributes, emptyList()); |
| 103 | + verify(delegate) |
| 104 | + .shouldSample(parentContext, traceId, SPAN_NAME, SPAN_KIND, attributes, emptyList()); |
| 105 | + |
| 106 | + clearInvocations(delegate); |
| 107 | + |
| 108 | + // with http.url attribute |
| 109 | + attributes = Attributes.of(HTTP_URL, "https://example.com"); |
| 110 | + sampler.shouldSample(parentContext, traceId, SPAN_NAME, SPAN_KIND, attributes, emptyList()); |
| 111 | + verify(delegate) |
| 112 | + .shouldSample(parentContext, traceId, SPAN_NAME, SPAN_KIND, attributes, emptyList()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testDropOnExactMatch() { |
| 117 | + RuleBasedRoutingSampler sampler = |
| 118 | + addRules(RuleBasedRoutingSampler.builder(SPAN_KIND, delegate)).build(); |
| 119 | + assertThat(shouldSample(sampler, "https://example.com/healthcheck").getDecision()) |
| 120 | + .isEqualTo(SamplingDecision.DROP); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testDelegateOnDifferentKind() { |
| 125 | + RuleBasedRoutingSampler sampler = |
| 126 | + addRules(RuleBasedRoutingSampler.builder(SpanKind.CLIENT, delegate)).build(); |
| 127 | + assertThat(shouldSample(sampler, "https://example.com/healthcheck").getDecision()) |
| 128 | + .isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); |
| 129 | + verify(delegate).shouldSample(any(), any(), any(), any(), any(), any()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testDelegateOnNoMatch() { |
| 134 | + RuleBasedRoutingSampler sampler = |
| 135 | + addRules(RuleBasedRoutingSampler.builder(SPAN_KIND, delegate)).build(); |
| 136 | + assertThat(shouldSample(sampler, "https://example.com/customers").getDecision()) |
| 137 | + .isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); |
| 138 | + verify(delegate).shouldSample(any(), any(), any(), any(), any(), any()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testDelegateOnMalformedUrl() { |
| 143 | + RuleBasedRoutingSampler sampler = |
| 144 | + addRules(RuleBasedRoutingSampler.builder(SPAN_KIND, delegate)).build(); |
| 145 | + assertThat(shouldSample(sampler, "abracadabra").getDecision()) |
| 146 | + .isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); |
| 147 | + verify(delegate).shouldSample(any(), any(), any(), any(), any(), any()); |
| 148 | + |
| 149 | + clearInvocations(delegate); |
| 150 | + |
| 151 | + assertThat(shouldSample(sampler, "healthcheck").getDecision()) |
| 152 | + .isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); |
| 153 | + verify(delegate).shouldSample(any(), any(), any(), any(), any(), any()); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testVerifiesAllGivenAttributes() { |
| 158 | + RuleBasedRoutingSampler sampler = |
| 159 | + addRules(RuleBasedRoutingSampler.builder(SPAN_KIND, delegate)).build(); |
| 160 | + Attributes attributes = Attributes.of(HTTP_TARGET, "/actuator/info"); |
| 161 | + assertThat( |
| 162 | + sampler |
| 163 | + .shouldSample(parentContext, traceId, SPAN_NAME, SPAN_KIND, attributes, emptyList()) |
| 164 | + .getDecision()) |
| 165 | + .isEqualTo(SamplingDecision.DROP); |
| 166 | + } |
| 167 | + |
| 168 | + private SamplingResult shouldSample(Sampler sampler, String url) { |
| 169 | + Attributes attributes = Attributes.of(HTTP_URL, url); |
| 170 | + return sampler.shouldSample( |
| 171 | + parentContext, traceId, SPAN_NAME, SPAN_KIND, attributes, emptyList()); |
| 172 | + } |
| 173 | + |
| 174 | + private RuleBasedRoutingSamplerBuilder addRules(RuleBasedRoutingSamplerBuilder builder) { |
| 175 | + return builder.drop(HTTP_URL, ".*/healthcheck").drop(HTTP_TARGET, "/actuator"); |
| 176 | + } |
| 177 | +} |
0 commit comments