Skip to content

Commit

Permalink
Enhance OpenTelemetry's DropTargetsSampler
Browse files Browse the repository at this point in the history
This allows us to move effectively drop traces
belonging to SwaggerUI and other framework endpoints

Fixes: quarkusio#34376
  • Loading branch information
geoand committed Jul 4, 2023
1 parent 1b6013f commit fbf4d7a
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,44 @@ 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();
}
}

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();
Expand Down

0 comments on commit fbf4d7a

Please sign in to comment.