Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,13 @@ private void bind(
}
});
}
// Update port to actual port when it is not a domain socket as wildcard port 0 might have been used
// Update port and address to actual values when it is not a domain socket as wildcard port 0 might have been used
var actualLocalAddress = localAddress;
if (bindAddress.isInetSocket()) {
actualPort = ((InetSocketAddress)ch.localAddress()).getPort();
actualLocalAddress = SocketAddress.inetSocketAddress(actualPort, localAddress.host());
}
metrics = createMetrics(localAddress);
metrics = createMetrics(actualLocalAddress);
promise.complete(ch);
} else {
promise.fail(res.cause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.http.WebSocketBase;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.HttpServerMetrics;
import io.vertx.core.spi.observability.HttpRequest;
Expand All @@ -31,6 +30,11 @@ public class FakeHttpServerMetrics extends FakeTCPMetrics implements HttpServerM

private final ConcurrentMap<String, WebSocketMetric> webSockets = new ConcurrentHashMap<>();
private final Set<HttpServerMetric> requests = ConcurrentHashMap.newKeySet();
private final SocketAddress socketAddress;

public FakeHttpServerMetrics(SocketAddress socketAddress) {
this.socketAddress = socketAddress;
}

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

public SocketAddress socketAddress() {
return socketAddress;
}

@Override
public HttpServerMetric requestBegin(SocketMetric socketMetric, HttpRequest request) {
HttpServerMetric metric = new HttpServerMetric(request, socketMetric);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public EventBusMetrics createEventBusMetrics() {
}

public HttpServerMetrics<?, ?, ?> createHttpServerMetrics(HttpServerOptions options, SocketAddress localAddress) {
return new FakeHttpServerMetrics();
return new FakeHttpServerMetrics(localAddress);
}

public HttpClientMetrics<?, ?, ?> createHttpClientMetrics(HttpClientOptions options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.vertx.tests.metrics;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.test.core.TestUtils;
import io.vertx.test.fakemetrics.FakeHttpServerMetrics;
import io.vertx.test.fakemetrics.FakeMetricsBase;
import io.vertx.test.fakemetrics.FakeMetricsFactory;
import io.vertx.test.http.HttpTestBase;
import org.junit.Test;

public class MetricsPortTest extends HttpTestBase {

@Override
protected HttpServerOptions createBaseServerOptions() {
return new HttpServerOptions().setPort(0).setHost(DEFAULT_HTTP_HOST);
}

@Override
protected VertxOptions getOptions() {
VertxOptions options = super.getOptions();
options.setMetricsOptions(new MetricsOptions().setEnabled(true));
return options;
}

@Override
protected Vertx createVertx(VertxOptions options) {
return Vertx.builder().with(options)
.withMetrics(new FakeMetricsFactory())
.build();
}

@Test
public void actualPortInMetricsWhenDynamicPortIsUsed() throws Exception {
server.requestHandler(req -> {
FakeHttpServerMetrics metrics = FakeMetricsBase.getMetrics(server);

assertEquals(server.actualPort(), metrics.socketAddress().port());

req.response().end();
testComplete();
});

startServer();

var requestOptions = new RequestOptions()
.setPort(server.actualPort());

client.request(new RequestOptions(requestOptions).setURI(TestUtils.randomAlphaString(16))).onComplete(onSuccess(HttpClientRequest::send));
await();
}

}
Loading