Search in sources :

Example 11 with VertxMetricsFactory

use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.

the class MetricsContextTest method testDatagram.

private void testDatagram(Function<Vertx, Context> contextFactory, BiConsumer<Thread, Context> checker) {
    AtomicReference<Thread> expectedThread = new AtomicReference<>();
    AtomicReference<Context> expectedContext = new AtomicReference<>();
    AtomicBoolean listening = new AtomicBoolean();
    AtomicBoolean bytesReadCalled = new AtomicBoolean();
    AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
    AtomicBoolean closeCalled = new AtomicBoolean();
    VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {

        @Override
        public DatagramSocketMetrics createMetrics(DatagramSocket socket, DatagramSocketOptions options) {
            return new DummyDatagramMetrics() {

                @Override
                public void listening(String localName, SocketAddress localAddress) {
                    listening.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)));
    Context ctx = contextFactory.apply(vertx);
    ctx.runOnContext(v1 -> {
        expectedThread.set(Thread.currentThread());
        expectedContext.set(Vertx.currentContext());
        DatagramSocket socket = vertx.createDatagramSocket();
        socket.listen(1234, "localhost", ar1 -> {
            assertTrue(ar1.succeeded());
            checker.accept(expectedThread.get(), expectedContext.get());
            socket.handler(packet -> {
                assertTrue(listening.get());
                assertTrue(bytesReadCalled.get());
                assertTrue(bytesWrittenCalled.get());
                executeInVanillaThread(() -> {
                    socket.close(ar2 -> {
                        assertTrue(closeCalled.get());
                        assertTrue(ar2.succeeded());
                        testComplete();
                    });
                });
            });
            socket.send(Buffer.buffer("msg"), 1234, "localhost", ar2 -> {
                assertTrue(ar2.succeeded());
            });
        });
    });
    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) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DatagramSocket(io.vertx.core.datagram.DatagramSocket)

Example 12 with VertxMetricsFactory

use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.

the class MetricsContextTest method testFactoryInCluster.

@Test
public void testFactoryInCluster() throws Exception {
    AtomicReference<Thread> metricsThread = new AtomicReference<>();
    AtomicReference<Context> metricsContext = new AtomicReference<>();
    Thread testThread = Thread.currentThread();
    VertxMetricsFactory factory = (vertx, options) -> {
        metricsThread.set(Thread.currentThread());
        metricsContext.set(Vertx.currentContext());
        return DummyVertxMetrics.INSTANCE;
    };
    clusteredVertx(new VertxOptions().setClustered(true).setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)), onSuccess(vertx -> {
        assertSame(testThread, metricsThread.get());
        assertNull(metricsContext.get());
        testComplete();
    }));
    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) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) Test(org.junit.Test)

Example 13 with VertxMetricsFactory

use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.

the class MetricsContextTest method testFactory.

@Test
public void testFactory() throws Exception {
    AtomicReference<Thread> metricsThread = new AtomicReference<>();
    AtomicReference<Context> metricsContext = new AtomicReference<>();
    VertxMetricsFactory factory = (vertx, options) -> {
        metricsThread.set(Thread.currentThread());
        metricsContext.set(Vertx.currentContext());
        return DummyVertxMetrics.INSTANCE;
    };
    vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    assertSame(Thread.currentThread(), metricsThread.get());
    assertNull(metricsContext.get());
}
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) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) Test(org.junit.Test)

Example 14 with VertxMetricsFactory

use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.

the class MetricsContextTest method testEventBusLifecycle.

@Test
public void testEventBusLifecycle() {
    AtomicBoolean closeCalled = 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 close() {
                    closeCalled.set(true);
                }
            };
        }
    };
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    vertx.eventBus();
    executeInVanillaThread(() -> {
        vertx.close(onSuccess(v -> {
            assertTrue(closeCalled.get());
            testComplete();
        }));
    });
    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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MetricsOptions(io.vertx.core.metrics.MetricsOptions) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) EventBus(io.vertx.core.eventbus.EventBus) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) Test(org.junit.Test)

Example 15 with VertxMetricsFactory

use of io.vertx.core.spi.VertxMetricsFactory in project vert.x by eclipse.

the class VertxImpl method initialiseMetrics.

private VertxMetrics initialiseMetrics(VertxOptions options) {
    if (options.getMetricsOptions() != null && options.getMetricsOptions().isEnabled()) {
        VertxMetricsFactory factory = options.getMetricsOptions().getFactory();
        if (factory == null) {
            factory = ServiceHelper.loadFactoryOrNull(VertxMetricsFactory.class);
            if (factory == null) {
                log.warn("Metrics has been set to enabled but no VertxMetricsFactory found on classpath");
            }
        }
        if (factory != null) {
            VertxMetrics metrics = factory.metrics(this, options);
            Objects.requireNonNull(metrics, "The metric instance created from " + factory + " cannot be null");
            return metrics;
        }
    }
    return DummyVertxMetrics.INSTANCE;
}
Also used : DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) VertxMetrics(io.vertx.core.spi.metrics.VertxMetrics) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory)

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