Skip to content

Commit 51fd14b

Browse files
authored
create metrics with actual server port (#6019)
This ensures that metrics are created with the actual server port (for test cases where port is dynamic).
1 parent 795da78 commit 51fd14b

4 files changed

Lines changed: 71 additions & 4 deletions

File tree

vertx-core/src/main/java/io/vertx/core/net/impl/NetServerImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,13 @@ private void bind(
555555
}
556556
});
557557
}
558-
// Update port to actual port when it is not a domain socket as wildcard port 0 might have been used
558+
// Update port and address to actual values when it is not a domain socket as wildcard port 0 might have been used
559+
var actualLocalAddress = localAddress;
559560
if (bindAddress.isInetSocket()) {
560561
actualPort = ((InetSocketAddress)ch.localAddress()).getPort();
562+
actualLocalAddress = SocketAddress.inetSocketAddress(actualPort, localAddress.host());
561563
}
562-
metrics = createMetrics(localAddress);
564+
metrics = createMetrics(actualLocalAddress);
563565
promise.complete(ch);
564566
} else {
565567
promise.fail(res.cause());

vertx-core/src/test/java/io/vertx/test/fakemetrics/FakeHttpServerMetrics.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import io.vertx.core.http.HttpMethod;
1515
import io.vertx.core.http.HttpServerRequest;
1616
import io.vertx.core.http.ServerWebSocket;
17-
import io.vertx.core.http.WebSocketBase;
1817
import io.vertx.core.net.SocketAddress;
1918
import io.vertx.core.spi.metrics.HttpServerMetrics;
2019
import io.vertx.core.spi.observability.HttpRequest;
@@ -31,6 +30,11 @@ public class FakeHttpServerMetrics extends FakeTCPMetrics implements HttpServerM
3130

3231
private final ConcurrentMap<String, WebSocketMetric> webSockets = new ConcurrentHashMap<>();
3332
private final Set<HttpServerMetric> requests = ConcurrentHashMap.newKeySet();
33+
private final SocketAddress socketAddress;
34+
35+
public FakeHttpServerMetrics(SocketAddress socketAddress) {
36+
this.socketAddress = socketAddress;
37+
}
3438

3539
public WebSocketMetric getWebSocketMetric(ServerWebSocket ws) {
3640
return webSockets.get(ws.path());
@@ -44,6 +48,10 @@ public HttpServerMetric getResponseMetric(String uri) {
4448
return requests.stream().filter(m -> m.uri.equals(uri)).findFirst().orElse(null);
4549
}
4650

51+
public SocketAddress socketAddress() {
52+
return socketAddress;
53+
}
54+
4755
@Override
4856
public HttpServerMetric requestBegin(SocketMetric socketMetric, HttpRequest request) {
4957
HttpServerMetric metric = new HttpServerMetric(request, socketMetric);

vertx-core/src/test/java/io/vertx/test/fakemetrics/FakeVertxMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public EventBusMetrics createEventBusMetrics() {
5555
}
5656

5757
public HttpServerMetrics<?, ?, ?> createHttpServerMetrics(HttpServerOptions options, SocketAddress localAddress) {
58-
return new FakeHttpServerMetrics();
58+
return new FakeHttpServerMetrics(localAddress);
5959
}
6060

6161
public HttpClientMetrics<?, ?, ?> createHttpClientMetrics(HttpClientOptions options) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.vertx.tests.metrics;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.core.VertxOptions;
5+
import io.vertx.core.http.HttpClientRequest;
6+
import io.vertx.core.http.HttpServerOptions;
7+
import io.vertx.core.http.RequestOptions;
8+
import io.vertx.core.metrics.MetricsOptions;
9+
import io.vertx.test.core.TestUtils;
10+
import io.vertx.test.fakemetrics.FakeHttpServerMetrics;
11+
import io.vertx.test.fakemetrics.FakeMetricsBase;
12+
import io.vertx.test.fakemetrics.FakeMetricsFactory;
13+
import io.vertx.test.http.HttpTestBase;
14+
import org.junit.Test;
15+
16+
public class MetricsPortTest extends HttpTestBase {
17+
18+
@Override
19+
protected HttpServerOptions createBaseServerOptions() {
20+
return new HttpServerOptions().setPort(0).setHost(DEFAULT_HTTP_HOST);
21+
}
22+
23+
@Override
24+
protected VertxOptions getOptions() {
25+
VertxOptions options = super.getOptions();
26+
options.setMetricsOptions(new MetricsOptions().setEnabled(true));
27+
return options;
28+
}
29+
30+
@Override
31+
protected Vertx createVertx(VertxOptions options) {
32+
return Vertx.builder().with(options)
33+
.withMetrics(new FakeMetricsFactory())
34+
.build();
35+
}
36+
37+
@Test
38+
public void actualPortInMetricsWhenDynamicPortIsUsed() throws Exception {
39+
server.requestHandler(req -> {
40+
FakeHttpServerMetrics metrics = FakeMetricsBase.getMetrics(server);
41+
42+
assertEquals(server.actualPort(), metrics.socketAddress().port());
43+
44+
req.response().end();
45+
testComplete();
46+
});
47+
48+
startServer();
49+
50+
var requestOptions = new RequestOptions()
51+
.setPort(server.actualPort());
52+
53+
client.request(new RequestOptions(requestOptions).setURI(TestUtils.randomAlphaString(16))).onComplete(onSuccess(HttpClientRequest::send));
54+
await();
55+
}
56+
57+
}

0 commit comments

Comments
 (0)