Skip to content

Commit

Permalink
HTTP PATH metrics don't consistently report a path (#242)
Browse files Browse the repository at this point in the history
See #225

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont authored Oct 29, 2024
1 parent ff3a061 commit 02b926b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
44 changes: 44 additions & 0 deletions src/main/java/io/vertx/micrometer/impl/HttpUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.vertx.micrometer.impl;

/**
* Copied over from Vert.x core HttpUtils to avoid exposing internal utilities.
*/
class HttpUtils {

/**
* Extract the path out of the uri.
*/
static String parsePath(String uri) {
if (uri.isEmpty()) {
return "";
}
int i;
if (uri.charAt(0) == '/') {
i = 0;
} else {
i = uri.indexOf("://");
if (i == -1) {
i = 0;
} else {
i = uri.indexOf('/', i + 3);
if (i == -1) {
// contains no /
return "/";
}
}
}

int queryStart = uri.indexOf('?', i);
if (queryStart == -1) {
queryStart = uri.length();
if (i == 0) {
return uri;
}
}
return uri.substring(i, queryStart);
}

private HttpUtils() {
// Utility
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void dequeueRequest(Timers.EventTiming taskMetric) {

@Override
public Handler requestBegin(String uri, HttpRequest request) {
Handler handler = new Handler(remote, request.uri(), request.method().name());
Handler handler = new Handler(remote, HttpUtils.parsePath(request.uri()), request.method().name());
if (customTagsProvider != null) {
handler.customTags = customTagsProvider.apply(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Instance extends VertxNetServerMetrics.Instance implements HttpServerMetri

@Override
public Handler requestBegin(String remote, HttpRequest request) {
Handler handler = new Handler(remote, request.uri(), request.method().name());
Handler handler = new Handler(remote, HttpUtils.parsePath(request.uri()), request.method().name());
if (customTagsProvider != null) {
handler.customTags = customTagsProvider.apply(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpVersion;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetSocket;
import io.vertx.core.http.*;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
Expand Down Expand Up @@ -236,7 +228,7 @@ private void runClientRequests(TestContext ctx, boolean ws, String user) {
private void httpRequest(HttpClient httpClient, TestContext ctx, String user) {
Async async = ctx.async(HTTP_SENT_COUNT);
for (int i = 0; i < HTTP_SENT_COUNT; i++) {
httpClient.request(HttpMethod.POST, 9195, "127.0.0.1", "/resource")
httpClient.request(HttpMethod.POST, 9195, "127.0.0.1", "/resource?foo=bar")
.compose(req -> {
req = user != null ? req.putHeader("user", user) : req;
return req.send(CLIENT_REQUEST)
Expand Down
13 changes: 10 additions & 3 deletions src/test/java/io/vertx/micrometer/VertxHttpServerMetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class VertxHttpServerMetricsTest extends MicrometerMetricsTestBase {

private HttpServer httpServer;

@Override
protected MicrometerMetricsOptions metricOptions() {
return super.metricOptions()
.addLabels(Label.HTTP_PATH);
}

@Test
public void shouldDecrementActiveRequestsWhenRequestEndedAfterResponseEnded(TestContext ctx) {
vertx = vertx(ctx);
Expand All @@ -36,7 +42,7 @@ public void shouldDecrementActiveRequestsWhenRequestEndedAfterResponseEnded(Test
listenLatch.awaitSuccess(20_000);
HttpClient client = vertx.createHttpClient();
for (int i = 0;i < numRequests;i++) {
client.request(HttpMethod.POST, 9195, "127.0.0.1", "/")
client.request(HttpMethod.POST, 9195, "127.0.0.1", "/resource?foo=bar")
.onComplete(ctx.asyncAssertSuccess(req -> {
req
.response()
Expand All @@ -50,7 +56,8 @@ public void shouldDecrementActiveRequestsWhenRequestEndedAfterResponseEnded(Test
}));
}
doneLatch.awaitSuccess(20_000);
List<Datapoint> res = listDatapoints(startsWith("vertx.http.server.active.requests"));
assertThat(res).extracting(Datapoint::value).contains(0.0);
List<Datapoint> datapoints = listDatapoints(startsWith("vertx.http.server.active.requests"));
assertThat(datapoints).hasSize(1).contains(
dp("vertx.http.server.active.requests[method=POST,path=/resource]$VALUE", 0.0));
}
}

0 comments on commit 02b926b

Please sign in to comment.