Search in sources :

Example 11 with Metrics

use of io.micrometer.api.instrument.Metrics in project reactor-netty by reactor.

the class HttpMetricsHandlerTests method testServerConnectionsMicrometer.

@ParameterizedTest
@MethodSource("httpCompatibleProtocols")
void testServerConnectionsMicrometer(HttpProtocol[] serverProtocols, HttpProtocol[] clientProtocols, @Nullable ProtocolSslContextSpec serverCtx, @Nullable ProtocolSslContextSpec clientCtx) throws Exception {
    disposableServer = customizeServerOptions(httpServer, serverCtx, serverProtocols).metrics(true, Function.identity()).bindNow();
    AtomicReference<SocketAddress> clientAddress = new AtomicReference<>();
    httpClient = httpClient.doAfterRequest((req, conn) -> clientAddress.set(conn.channel().localAddress()));
    String uri = "/4";
    String address = formatSocketAddress(disposableServer.address());
    CountDownLatch latch = new CountDownLatch(1);
    httpClient = customizeClientOptions(httpClient, clientCtx, clientProtocols);
    httpClient.doOnResponse((res, conn) -> conn.channel().closeFuture().addListener(f -> latch.countDown())).metrics(true, Function.identity()).post().uri(uri).send(body).responseContent().aggregate().asString().as(StepVerifier::create).expectNext("Hello World!").expectComplete().verify(Duration.ofSeconds(30));
    assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    // ensure that the server counters have been updated. For the moment, wait 1 sec.
    Thread.sleep(1000);
    // now check the server counters
    checkGauge(SERVER_CONNECTIONS_TOTAL, true, 0, URI, HTTP, LOCAL_ADDRESS, address);
    checkGauge(SERVER_CONNECTIONS_ACTIVE, true, 0, URI, HTTP, LOCAL_ADDRESS, address);
    // These metrics are meant only for the servers,
    // connections metrics for the clients are available from the connection pool
    address = formatSocketAddress(clientAddress.get());
    checkGauge(CLIENT_CONNECTIONS_TOTAL, false, 0, URI, HTTP, LOCAL_ADDRESS, address);
    checkGauge(CLIENT_CONNECTIONS_ACTIVE, false, 0, URI, HTTP, LOCAL_ADDRESS, address);
    disposableServer.disposeNow();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) SocketAddress(java.net.SocketAddress) ContextView(reactor.util.context.ContextView) ContextAwareHttpServerMetricsRecorder(reactor.netty.http.server.ContextAwareHttpServerMetricsRecorder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BaseHttpTest(reactor.netty.BaseHttpTest) DATA_SENT_TIME(reactor.netty.Metrics.DATA_SENT_TIME) Counter(io.micrometer.api.instrument.Counter) Tags(io.micrometer.api.instrument.Tags) ProtocolSslContextSpec(reactor.netty.tcp.SslProvider.ProtocolSslContextSpec) BeforeAll(org.junit.jupiter.api.BeforeAll) ByteBufFlux(reactor.netty.ByteBufFlux) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) MethodSource(org.junit.jupiter.params.provider.MethodSource) Timer(io.micrometer.api.instrument.Timer) ERRORS(reactor.netty.Metrics.ERRORS) RESPONSE_TIME(reactor.netty.Metrics.RESPONSE_TIME) Context(reactor.util.context.Context) TLS_HANDSHAKE_TIME(reactor.netty.Metrics.TLS_HANDSHAKE_TIME) MeterRegistryAssert(io.micrometer.core.tck.MeterRegistryAssert) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) DistributionSummary(io.micrometer.api.instrument.DistributionSummary) HTTP_CLIENT_PREFIX(reactor.netty.Metrics.HTTP_CLIENT_PREFIX) Test(org.junit.jupiter.api.Test) STATUS(reactor.netty.Metrics.STATUS) ContextAwareHttpClientMetricsRecorder(reactor.netty.http.client.ContextAwareHttpClientMetricsRecorder) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServer(reactor.netty.http.server.HttpServer) Stream(java.util.stream.Stream) Named(org.junit.jupiter.api.Named) CONNECT_TIME(reactor.netty.Metrics.CONNECT_TIME) Gauge(io.micrometer.api.instrument.Gauge) HttpClient(reactor.netty.http.client.HttpClient) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) REMOTE_ADDRESS(reactor.netty.Metrics.REMOTE_ADDRESS) HTTP_SERVER_PREFIX(reactor.netty.Metrics.HTTP_SERVER_PREFIX) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Nullable(reactor.util.annotation.Nullable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteBuf(io.netty.buffer.ByteBuf) DATA_SENT(reactor.netty.Metrics.DATA_SENT) METHOD(reactor.netty.Metrics.METHOD) DATA_RECEIVED_TIME(reactor.netty.Metrics.DATA_RECEIVED_TIME) Metrics.formatSocketAddress(reactor.netty.Metrics.formatSocketAddress) CONNECTIONS_ACTIVE(reactor.netty.Metrics.CONNECTIONS_ACTIVE) CONNECTIONS_TOTAL(reactor.netty.Metrics.CONNECTIONS_TOTAL) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpServerMetricsRecorder(reactor.netty.http.server.HttpServerMetricsRecorder) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) LOCAL_ADDRESS(reactor.netty.Metrics.LOCAL_ADDRESS) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) SslProvider(io.netty.handler.ssl.SslProvider) MeterRegistry(io.micrometer.api.instrument.MeterRegistry) Metrics(io.micrometer.api.instrument.Metrics) DATA_RECEIVED(reactor.netty.Metrics.DATA_RECEIVED) ConnectionProvider(reactor.netty.resources.ConnectionProvider) URI(reactor.netty.Metrics.URI) AtomicReference(java.util.concurrent.atomic.AtomicReference) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Metrics.formatSocketAddress(reactor.netty.Metrics.formatSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with Metrics

use of io.micrometer.api.instrument.Metrics in project reactor-netty by reactor.

the class AddressResolverGroupMetricsTest method test.

@Test
void test() throws Exception {
    disposableServer = createServer().host("localhost").handle((req, res) -> res.header("Connection", "close").sendString(Mono.just("test"))).bindNow();
    CountDownLatch latch = new CountDownLatch(1);
    HttpClient.create().doOnResponse((res, conn) -> conn.channel().closeFuture().addListener(f -> latch.countDown())).metrics(true, Function.identity()).get().uri("http://localhost:" + disposableServer.port()).responseContent().aggregate().asString().block(Duration.ofSeconds(30));
    assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    assertThat(getTimerValue("localhost:" + disposableServer.port())).isGreaterThan(0);
}
Also used : Timer(io.micrometer.api.instrument.Timer) SUCCESS(reactor.netty.Metrics.SUCCESS) BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) REMOTE_ADDRESS(reactor.netty.Metrics.REMOTE_ADDRESS) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BaseHttpTest(reactor.netty.BaseHttpTest) Mono(reactor.core.publisher.Mono) MeterRegistryAssert(io.micrometer.core.tck.MeterRegistryAssert) Tags(io.micrometer.api.instrument.Tags) Function(java.util.function.Function) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) STATUS(reactor.netty.Metrics.STATUS) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) MeterRegistry(io.micrometer.api.instrument.MeterRegistry) Duration(java.time.Duration) Metrics(io.micrometer.api.instrument.Metrics) HttpClient(reactor.netty.http.client.HttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 13 with Metrics

use of io.micrometer.api.instrument.Metrics in project reactor-netty by reactor.

the class ByteBufAllocatorMetricsTest method test.

@Test
void test() throws Exception {
    disposableServer = createServer().handle((req, res) -> res.header("Connection", "close").sendString(Mono.just("test"))).bindNow();
    CountDownLatch latch = new CountDownLatch(1);
    PooledByteBufAllocator alloc = new PooledByteBufAllocator(true);
    createClient(disposableServer.port()).option(ChannelOption.ALLOCATOR, alloc).doOnResponse((res, conn) -> conn.channel().closeFuture().addListener(f -> latch.countDown())).metrics(true, Function.identity()).get().uri("/").responseContent().aggregate().asString().block(Duration.ofSeconds(30));
    assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    String id = alloc.metric().hashCode() + "";
    String[] tags = new String[] { ID, id, TYPE, "pooled" };
    checkExpectations(BYTE_BUF_ALLOCATOR_PREFIX, tags);
    // Verify ACTIVE_HEAP_MEMORY and ACTIVE_DIRECT_MEMORY meters
    List<ByteBuf> buffers = new ArrayList<>();
    boolean releaseBufList = true;
    try {
        double currentActiveHeap = getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_HEAP_MEMORY, tags);
        double currentActiveDirect = getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_DIRECT_MEMORY, tags);
        IntStream.range(0, 10).mapToObj(i -> alloc.heapBuffer(102400)).forEach(buffers::add);
        IntStream.range(0, 10).mapToObj(i -> alloc.directBuffer(102400)).forEach(buffers::add);
        assertThat(getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_HEAP_MEMORY, tags)).isGreaterThan(currentActiveHeap);
        assertThat(getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_DIRECT_MEMORY, tags)).isGreaterThan(currentActiveDirect);
        currentActiveHeap = getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_HEAP_MEMORY, tags);
        currentActiveDirect = getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_DIRECT_MEMORY, tags);
        buffers.forEach(ByteBuf::release);
        releaseBufList = false;
        assertThat(getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_HEAP_MEMORY, tags)).isLessThan(currentActiveHeap);
        assertThat(getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_DIRECT_MEMORY, tags)).isLessThan(currentActiveDirect);
    } finally {
        if (releaseBufList) {
            buffers.forEach(ByteBuf::release);
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) ChannelOption(io.netty.channel.ChannelOption) USED_DIRECT_MEMORY(reactor.netty.Metrics.USED_DIRECT_MEMORY) CHUNK_SIZE(reactor.netty.Metrics.CHUNK_SIZE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BaseHttpTest(reactor.netty.BaseHttpTest) THREAD_LOCAL_CACHES(reactor.netty.Metrics.THREAD_LOCAL_CACHES) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ACTIVE_HEAP_MEMORY(reactor.netty.Metrics.ACTIVE_HEAP_MEMORY) ByteBuf(io.netty.buffer.ByteBuf) Duration(java.time.Duration) DIRECT_ARENAS(reactor.netty.Metrics.DIRECT_ARENAS) BYTE_BUF_ALLOCATOR_PREFIX(reactor.netty.Metrics.BYTE_BUF_ALLOCATOR_PREFIX) HEAP_ARENAS(reactor.netty.Metrics.HEAP_ARENAS) Mono(reactor.core.publisher.Mono) USED_HEAP_MEMORY(reactor.netty.Metrics.USED_HEAP_MEMORY) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) ID(reactor.netty.Metrics.ID) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) NORMAL_CACHE_SIZE(reactor.netty.Metrics.NORMAL_CACHE_SIZE) SMALL_CACHE_SIZE(reactor.netty.Metrics.SMALL_CACHE_SIZE) TYPE(reactor.netty.Metrics.TYPE) MeterRegistry(io.micrometer.api.instrument.MeterRegistry) ACTIVE_DIRECT_MEMORY(reactor.netty.Metrics.ACTIVE_DIRECT_MEMORY) Metrics(io.micrometer.api.instrument.Metrics) Gauge(io.micrometer.api.instrument.Gauge) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Aggregations

Metrics (io.micrometer.api.instrument.Metrics)13 MeterRegistry (io.micrometer.api.instrument.MeterRegistry)12 SimpleMeterRegistry (io.micrometer.api.instrument.simple.SimpleMeterRegistry)12 Duration (java.time.Duration)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 TimeUnit (java.util.concurrent.TimeUnit)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 AfterEach (org.junit.jupiter.api.AfterEach)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12 Test (org.junit.jupiter.api.Test)12 Mono (reactor.core.publisher.Mono)12 Gauge (io.micrometer.api.instrument.Gauge)11 Function (java.util.function.Function)11 BaseHttpTest (reactor.netty.BaseHttpTest)11 HttpClient (reactor.netty.http.client.HttpClient)10 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)9 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)9 CertificateException (java.security.cert.CertificateException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 BeforeAll (org.junit.jupiter.api.BeforeAll)9