use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.
the class BareCommand method getMetricsOptions.
/**
* @return the metric options.
*/
protected MetricsOptions getMetricsOptions() {
MetricsOptions metricsOptions;
VertxMetricsFactory factory = ServiceHelper.loadFactoryOrNull(VertxMetricsFactory.class);
if (factory != null) {
metricsOptions = factory.newOptions();
} else {
metricsOptions = new MetricsOptions();
}
configureFromSystemProperties(metricsOptions, METRICS_OPTIONS_PROP_PREFIX);
return metricsOptions;
}
use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.
the class Starter method startVertx.
private Vertx startVertx(boolean clustered, boolean ha, Args args) {
MetricsOptions metricsOptions;
ServiceLoader<VertxMetricsFactory> factories = ServiceLoader.load(VertxMetricsFactory.class);
if (factories.iterator().hasNext()) {
VertxMetricsFactory factory = factories.iterator().next();
metricsOptions = factory.newOptions();
} else {
metricsOptions = new MetricsOptions();
}
configureFromSystemProperties(metricsOptions, METRICS_OPTIONS_PROP_PREFIX);
options = new VertxOptions().setMetricsOptions(metricsOptions);
configureFromSystemProperties(options, VERTX_OPTIONS_PROP_PREFIX);
if (clustered) {
log.info("Starting clustering...");
int clusterPort = args.getInt("-cluster-port");
if (clusterPort == -1) {
// Default to zero - this means choose an ephemeral port
clusterPort = 0;
}
String clusterHost = args.map.get("-cluster-host");
if (clusterHost == null) {
clusterHost = getDefaultAddress();
if (clusterHost == null) {
log.error("Unable to find a default network interface for clustering. Please specify one using -cluster-host");
return null;
} else {
log.info("No cluster-host specified so using address " + clusterHost);
}
}
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AsyncResult<Vertx>> result = new AtomicReference<>();
options.setClusterHost(clusterHost).setClusterPort(clusterPort).setClustered(true);
if (ha) {
String haGroup = args.map.get("-hagroup");
int quorumSize = args.getInt("-quorum");
options.setHAEnabled(true);
if (haGroup != null) {
options.setHAGroup(haGroup);
}
if (quorumSize != -1) {
options.setQuorumSize(quorumSize);
}
}
beforeStartingVertx(options);
Vertx.clusteredVertx(options, ar -> {
result.set(ar);
latch.countDown();
});
try {
if (!latch.await(2, TimeUnit.MINUTES)) {
log.error("Timed out in starting clustered Vert.x");
return null;
}
} catch (InterruptedException e) {
log.error("Thread interrupted in startup");
return null;
}
if (result.get().failed()) {
log.error("Failed to form cluster");
result.get().cause().printStackTrace();
return null;
}
vertx = result.get().result();
} else {
beforeStartingVertx(options);
vertx = Vertx.vertx(options);
}
addShutdownHook();
afterStartingVertx();
return vertx;
}
use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.
the class MetricsContextTest method testHttpClientWebsocket.
private void testHttpClientWebsocket(Function<Vertx, Context> contextFactory, BiConsumer<Thread, Context> checker) throws Exception {
AtomicReference<Thread> expectedThread = new AtomicReference<>();
AtomicReference<Context> expectedContext = new AtomicReference<>();
AtomicBoolean websocketConnected = new AtomicBoolean();
AtomicBoolean websocketDisconnected = new AtomicBoolean();
AtomicBoolean socketConnectedCalled = new AtomicBoolean();
AtomicBoolean socketDisconnectedCalled = new AtomicBoolean();
AtomicBoolean bytesReadCalled = new AtomicBoolean();
AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
AtomicBoolean closeCalled = new AtomicBoolean();
VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {
@Override
public HttpClientMetrics createMetrics(HttpClient client, HttpClientOptions options) {
return new DummyHttpClientMetrics() {
@Override
public Void connected(Void endpointMetric, Void socketMetric, WebSocket webSocket) {
websocketConnected.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
return null;
}
@Override
public void disconnected(Void webSocketMetric) {
websocketDisconnected.set(true);
}
@Override
public Void connected(SocketAddress remoteAddress, String remoteName) {
socketConnectedCalled.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
return null;
}
@Override
public void disconnected(Void socketMetric, SocketAddress remoteAddress) {
socketDisconnectedCalled.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
}
@Override
public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
bytesReadCalled.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
}
@Override
public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
bytesWrittenCalled.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
}
@Override
public void close() {
closeCalled.set(true);
}
@Override
public boolean isEnabled() {
return true;
}
};
}
};
Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
HttpServer server = vertx.createHttpServer();
server.websocketHandler(ws -> {
ws.handler(buf -> {
ws.write(Buffer.buffer("bye"));
});
});
CountDownLatch latch = new CountDownLatch(1);
server.listen(8080, "localhost", onSuccess(s -> {
latch.countDown();
}));
awaitLatch(latch);
Context ctx = contextFactory.apply(vertx);
ctx.runOnContext(v1 -> {
expectedThread.set(Thread.currentThread());
expectedContext.set(Vertx.currentContext());
HttpClient client = vertx.createHttpClient();
checker.accept(expectedThread.get(), expectedContext.get());
client.websocket(8080, "localhost", "/", ws -> {
ws.handler(buf -> {
ws.closeHandler(v2 -> {
executeInVanillaThread(() -> {
client.close();
vertx.close(v3 -> {
assertTrue(websocketConnected.get());
assertTrue(websocketDisconnected.get());
assertTrue(socketConnectedCalled.get());
assertTrue(socketDisconnectedCalled.get());
assertTrue(bytesReadCalled.get());
assertTrue(bytesWrittenCalled.get());
assertTrue(closeCalled.get());
testComplete();
});
});
});
ws.close();
});
ws.write(Buffer.buffer("hello"));
});
});
await();
}
use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.
the class MetricsContextTest method testMessageHandler.
private void testMessageHandler(BiConsumer<Vertx, Handler<Void>> runOnContext, BiConsumer<Thread, Context> checker) {
AtomicReference<Thread> consumerThread = new AtomicReference<>();
AtomicReference<Context> consumerContext = new AtomicReference<>();
AtomicBoolean registeredCalled = new AtomicBoolean();
AtomicBoolean unregisteredCalled = new AtomicBoolean();
AtomicBoolean beginHandleCalled = new AtomicBoolean();
AtomicBoolean endHandleCalled = new AtomicBoolean();
VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {
@Override
public EventBusMetrics createMetrics(EventBus eventBus) {
return new DummyEventBusMetrics() {
@Override
public boolean isEnabled() {
return true;
}
@Override
public Void handlerRegistered(String address, String repliedAddress) {
registeredCalled.set(true);
return null;
}
@Override
public void handlerUnregistered(Void handler) {
unregisteredCalled.set(true);
}
@Override
public void beginHandleMessage(Void handler, boolean local) {
consumerThread.set(Thread.currentThread());
consumerContext.set(Vertx.currentContext());
beginHandleCalled.set(true);
}
@Override
public void endHandleMessage(Void handler, Throwable failure) {
endHandleCalled.set(true);
checker.accept(consumerThread.get(), consumerContext.get());
}
};
}
};
Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
EventBus eb = vertx.eventBus();
runOnContext.accept(vertx, v -> {
MessageConsumer<Object> consumer = eb.consumer("the_address");
consumer.handler(msg -> {
checker.accept(consumerThread.get(), consumerContext.get());
executeInVanillaThread(() -> {
vertx.getOrCreateContext().runOnContext(v2 -> {
consumer.unregister(onSuccess(v3 -> {
assertTrue(registeredCalled.get());
assertTrue(beginHandleCalled.get());
assertTrue(endHandleCalled.get());
waitUntil(() -> unregisteredCalled.get());
testComplete();
}));
});
});
}).completionHandler(onSuccess(v2 -> {
eb.send("the_address", "the_msg");
}));
});
await();
}
use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.
the class MetricsContextTest method testHttpServerWebsocket.
private void testHttpServerWebsocket(Function<Vertx, Context> contextFactory, BiConsumer<Thread, Context> checker) throws Exception {
AtomicReference<Thread> expectedThread = new AtomicReference<>();
AtomicReference<Context> expectedContext = new AtomicReference<>();
AtomicBoolean websocketConnected = new AtomicBoolean();
AtomicBoolean websocketDisconnected = new AtomicBoolean();
AtomicBoolean socketConnectedCalled = new AtomicBoolean();
AtomicBoolean socketDisconnectedCalled = new AtomicBoolean();
AtomicBoolean bytesReadCalled = new AtomicBoolean();
AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
AtomicBoolean closeCalled = new AtomicBoolean();
VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {
@Override
public HttpServerMetrics createMetrics(HttpServer server, SocketAddress localAddress, HttpServerOptions options) {
return new DummyHttpServerMetrics() {
@Override
public Void connected(Void socketMetric, ServerWebSocket serverWebSocket) {
websocketConnected.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
return null;
}
@Override
public void disconnected(Void serverWebSocketMetric) {
websocketDisconnected.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
}
@Override
public Void connected(SocketAddress remoteAddress, String remoteName) {
socketConnectedCalled.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
return null;
}
@Override
public void disconnected(Void socketMetric, SocketAddress remoteAddress) {
socketDisconnectedCalled.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
}
@Override
public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
bytesReadCalled.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
}
@Override
public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
bytesWrittenCalled.set(true);
checker.accept(expectedThread.get(), expectedContext.get());
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public void close() {
closeCalled.set(true);
}
};
}
};
CountDownLatch latch = new CountDownLatch(1);
Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
Context ctx = contextFactory.apply(vertx);
ctx.runOnContext(v1 -> {
HttpServer server = vertx.createHttpServer().websocketHandler(ws -> {
ws.handler(buf -> {
ws.write(Buffer.buffer("bye"));
});
});
server.listen(8080, "localhost", onSuccess(s -> {
expectedThread.set(Thread.currentThread());
expectedContext.set(Vertx.currentContext());
latch.countDown();
}));
});
awaitLatch(latch);
HttpClient client = vertx.createHttpClient();
client.websocket(8080, "localhost", "/", ws -> {
ws.handler(buf -> {
ws.closeHandler(v -> {
vertx.close(v4 -> {
assertTrue(websocketConnected.get());
assertTrue(websocketDisconnected.get());
assertTrue(bytesReadCalled.get());
assertTrue(bytesWrittenCalled.get());
assertTrue(socketConnectedCalled.get());
assertTrue(socketDisconnectedCalled.get());
assertTrue(closeCalled.get());
testComplete();
});
});
ws.close();
});
ws.write(Buffer.buffer("hello"));
});
await();
}
Aggregations