Skip to content

Commit 60da8ba

Browse files
authored
Merge pull request quarkusio#34510 from geoand/quarkusio#34376
Enhance OpenTelemetry's DropTargetsSampler
2 parents 798f1ce + b8b2c37 commit 60da8ba

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/DropTargetsSampler.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,44 @@ public SamplingResult shouldSample(Context parentContext, String traceId, String
2525

2626
if (spanKind.equals(SpanKind.SERVER)) {
2727
String target = attributes.get(SemanticAttributes.HTTP_TARGET);
28-
// TODO - radcortez - Match /* endpoints
29-
if (target != null && dropTargets.contains(target)) {
28+
if (shouldDrop(target)) {
3029
return SamplingResult.drop();
3130
}
3231
}
3332

3433
return sampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks);
3534
}
3635

36+
/**
37+
* Determines whether a path should be dropped
38+
* TODO: this can certainly be optimized if we find that it's a hot-path
39+
*/
40+
private boolean shouldDrop(String target) {
41+
if ((target == null) || target.isEmpty()) {
42+
return false;
43+
}
44+
if (safeContains(target)) { // check exact match
45+
return true;
46+
}
47+
if (target.charAt(target.length() - 1) == '/') { // check if the path without the ending slash matched
48+
if (safeContains(target.substring(0, target.length() - 1))) {
49+
return true;
50+
}
51+
}
52+
int lastSlashIndex = target.lastIndexOf('/');
53+
if (lastSlashIndex != -1) {
54+
if (safeContains(target.substring(0, lastSlashIndex) + "*")
55+
|| safeContains(target.substring(0, lastSlashIndex) + "/*")) { // check if a wildcard matches
56+
return true;
57+
}
58+
}
59+
return false;
60+
}
61+
62+
private boolean safeContains(String target) {
63+
return dropTargets.contains(target);
64+
}
65+
3766
@Override
3867
public String getDescription() {
3968
return sampler.getDescription();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)