Search in sources :

Example 1 with VertxMetricsFactory

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;
}
Also used : MetricsOptions(io.vertx.core.metrics.MetricsOptions) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory)

Example 2 with VertxMetricsFactory

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;
}
Also used : MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory)

Example 3 with VertxMetricsFactory

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();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) CountDownLatch(java.util.concurrent.CountDownLatch) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 4 with VertxMetricsFactory

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();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) EventBus(io.vertx.core.eventbus.EventBus) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 5 with VertxMetricsFactory

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();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) CountDownLatch(java.util.concurrent.CountDownLatch) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

VertxMetricsFactory (io.vertx.core.spi.VertxMetricsFactory)15 MetricsOptions (io.vertx.core.metrics.MetricsOptions)14 DummyVertxMetrics (io.vertx.core.metrics.impl.DummyVertxMetrics)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 io.vertx.core (io.vertx.core)12 Buffer (io.vertx.core.buffer.Buffer)12 DatagramSocket (io.vertx.core.datagram.DatagramSocket)12 DatagramSocketOptions (io.vertx.core.datagram.DatagramSocketOptions)12 EventBus (io.vertx.core.eventbus.EventBus)12 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)12 io.vertx.core.http (io.vertx.core.http)12 io.vertx.core.net (io.vertx.core.net)12 io.vertx.core.spi.metrics (io.vertx.core.spi.metrics)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 BiConsumer (java.util.function.BiConsumer)12 Function (java.util.function.Function)12 Ignore (org.junit.Ignore)12 Test (org.junit.Test)12 VertxMetrics (io.vertx.core.spi.metrics.VertxMetrics)1