From fbf4d7aea2867277465755d779ed93c564d99eea Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 4 Jul 2023 10:46:37 +0300 Subject: [PATCH] Enhance OpenTelemetry's DropTargetsSampler This allows us to move effectively drop traces belonging to SwaggerUI and other framework endpoints Fixes: #34376 --- .../runtime/tracing/DropTargetsSampler.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/DropTargetsSampler.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/DropTargetsSampler.java index 53966c5303204..b94709813e57c 100644 --- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/DropTargetsSampler.java +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/DropTargetsSampler.java @@ -25,8 +25,7 @@ public SamplingResult shouldSample(Context parentContext, String traceId, String if (spanKind.equals(SpanKind.SERVER)) { String target = attributes.get(SemanticAttributes.HTTP_TARGET); - // TODO - radcortez - Match /* endpoints - if (target != null && dropTargets.contains(target)) { + if (shouldDrop(target)) { return SamplingResult.drop(); } } @@ -34,6 +33,36 @@ public SamplingResult shouldSample(Context parentContext, String traceId, String return sampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); } + /** + * Determines whether a path should be dropped + * TODO: this can certainly be optimized if we find that it's a hot-path + */ + private boolean shouldDrop(String target) { + if ((target == null) || target.isEmpty()) { + return false; + } + if (safeContains(target)) { // check exact match + return true; + } + if (target.charAt(target.length() - 1) == '/') { // check if the path without the ending slash matched + if (safeContains(target.substring(0, target.length() - 1))) { + return true; + } + } + int lastSlashIndex = target.lastIndexOf('/'); + if (lastSlashIndex != -1) { + if (safeContains(target.substring(0, lastSlashIndex) + "*") + || safeContains(target.substring(0, lastSlashIndex) + "/*")) { // check if a wildcard matches + return true; + } + } + return false; + } + + private boolean safeContains(String target) { + return dropTargets.contains(target); + } + @Override public String getDescription() { return sampler.getDescription();