Skip to content

Commit

Permalink
Properly append route path when requestRouted() is called multiple times
Browse files Browse the repository at this point in the history
This can happen when a route (/dev-ui/) is mounted on a router (/q/).
The method will be called twice, once with /q/ and the second time with
/dev-ui/.
  • Loading branch information
gsmet committed Jan 10, 2025
1 parent 6338ba6 commit a450c7d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public HttpRequestMetric responsePushed(LongTaskTimer.Sample socketMetric, HttpM
public void requestRouted(HttpRequestMetric requestMetric, String route) {
// note that in the case of a route (e.g. /dev-ui/) mounted on a router (e.g. /q/)
// this method will be called twice, once for the router (route=/q/), the second time for the route (route=/dev-ui/)

log.debugf("requestRouted %s %s", route, requestMetric);

if (route == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public final class MetricRequest {
private final HttpRequest request;
private String currentRoutePath;

MetricRequest(final HttpRequest request) {
this.request = request;
Expand All @@ -24,4 +25,37 @@ Optional<Context> getContext() {
static MetricRequest request(final HttpRequest httpRequest) {
return new MetricRequest(httpRequest);
}

void appendCurrentRoutePath(String path) {
// we append the path to the current route path
// if a router is mounted on /q/ for instance, and a route of this router on /dev-ui/
// a request to /q/dev-ui/ needs to be affected to the /q/dev-ui/ path
// appendCurrentRoutePath() will be called twice, once with /q/ and the second time with /dev-ui/
if (path == null || path.isEmpty()) {
return;
}

if (this.currentRoutePath == null) {
this.currentRoutePath = path;
return;
}

boolean currentRoutePathTrailingSlash = this.currentRoutePath.endsWith("/");
boolean pathLeadingSlash = path.startsWith("/");
if (currentRoutePathTrailingSlash) {
if (pathLeadingSlash) {
this.currentRoutePath = this.currentRoutePath + path.substring(1);
} else {
this.currentRoutePath = this.currentRoutePath + path;
}
} else if (pathLeadingSlash) {
this.currentRoutePath = this.currentRoutePath + path;
} else {
this.currentRoutePath = this.currentRoutePath + '/' + path;
}
}

String getCurrentRoutePath() {
return currentRoutePath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ public MetricRequest requestBegin(final Object socketMetric, final HttpRequest r

@Override
public void requestRouted(final MetricRequest requestMetric, final String route) {
if (route != null) {
requestMetric.getContext().ifPresent(context -> context.putLocal("VertxRoute", route));
// note that in the case of a route (e.g. /dev-ui/) mounted on a router (e.g. /q/)
// this method will be called twice, once for the router (route=/q/), the second time for the route (route=/dev-ui/)
if (route == null) {
return;
}

requestMetric.appendCurrentRoutePath(route);
requestMetric.getContext()
.ifPresent(context -> context.putLocal("VertxRoute", requestMetric.getCurrentRoutePath()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ public MetricRequest requestBegin(final Object socketMetric, final HttpRequest r

@Override
public void requestRouted(final MetricRequest requestMetric, final String route) {
if (route != null) {
requestMetric.getContext().ifPresent(context -> context.putLocal("VertxRoute", route));
// note that in the case of a route (e.g. /dev-ui/) mounted on a router (e.g. /q/)
// this method will be called twice, once for the router (route=/q/), the second time for the route (route=/dev-ui/)
if (route == null) {
return;
}

requestMetric.appendCurrentRoutePath(route);
requestMetric.getContext()
.ifPresent(context -> context.putLocal("VertxRoute", requestMetric.getCurrentRoutePath()));
}
}
}

0 comments on commit a450c7d

Please sign in to comment.