Skip to content

Commit

Permalink
cache parsed routes
Browse files Browse the repository at this point in the history
  • Loading branch information
amarziali committed Dec 19, 2022
1 parent 03ba21f commit e6b8756
Showing 1 changed file with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;

import datadog.trace.api.cache.DDCache;
import datadog.trace.api.cache.DDCaches;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import org.springframework.web.reactive.function.server.HandlerFunction;
Expand All @@ -14,14 +17,24 @@ public class RouteOnSuccessOrError implements BiConsumer<HandlerFunction<?>, Thr

private static final Pattern SPECIAL_CHARACTERS_REGEX = Pattern.compile("[\\(\\)&|]");
private static final Pattern SPACES_REGEX = Pattern.compile("[ \\t]+");
private static final Pattern ROUTER_FUNCION_REGEX = Pattern.compile("\\s*->.*$");

private static final Pattern ROUTER_FUNCTION_REGEX = Pattern.compile("\\s*->.*$");
private static final Pattern METHOD_REGEX =
Pattern.compile("^(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH) ");
private static final Function<String, String> PATH_EXTRACTOR =
arg ->
METHOD_REGEX
.matcher(
SPACES_REGEX
.matcher(SPECIAL_CHARACTERS_REGEX.matcher(arg).replaceAll(""))
.replaceAll(" ")
.trim())
.replaceAll("");

private final RouterFunction routerFunction;
private final ServerRequest serverRequest;

private final DDCache<String, String> parsedRouteCache = DDCaches.newFixedSizeCache(16);

public RouteOnSuccessOrError(
final RouterFunction routerFunction, final ServerRequest serverRequest) {
this.routerFunction = routerFunction;
Expand Down Expand Up @@ -56,18 +69,12 @@ private String parsePredicateString() {
"org.springframework.web.reactive.function.server.RequestPredicates$$Lambda$")) {
return null;
} else {
return ROUTER_FUNCION_REGEX.matcher(routerFunctionString).replaceFirst("");
return ROUTER_FUNCTION_REGEX.matcher(routerFunctionString).replaceFirst("");
}
}

@Nonnull
private String parseRoute(@Nonnull String routerString) {
return METHOD_REGEX
.matcher(
SPACES_REGEX
.matcher(SPECIAL_CHARACTERS_REGEX.matcher(routerString).replaceAll(""))
.replaceAll(" ")
.trim())
.replaceAll("");
return parsedRouteCache.computeIfAbsent(routerString, PATH_EXTRACTOR);
}
}

0 comments on commit e6b8756

Please sign in to comment.