Search in sources :

Example 1 with Metrics

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

the class Application method main.

public static void main(String[] args) {
    // <1>
    Metrics.globalRegistry.config().meterFilter(MeterFilter.maximumAllowableTags("reactor.netty.http.server", "URI", 100, MeterFilter.deny()));
    DisposableServer server = HttpServer.create().metrics(true, s -> {
        if (s.startsWith("/stream/")) {
            // <2>
            return "/stream/{n}";
        } else if (s.startsWith("/bytes/")) {
            return "/bytes/{n}";
        }
        return s;
    }).route(r -> r.get("/stream/{n}", (req, res) -> res.sendString(Mono.just(req.param("n")))).get("/bytes/{n}", (req, res) -> res.sendString(Mono.just(req.param("n"))))).bindNow();
    server.onDispose().block();
}
Also used : HttpServer(reactor.netty.http.server.HttpServer) MeterFilter(io.micrometer.api.instrument.config.MeterFilter) Metrics(io.micrometer.api.instrument.Metrics) Mono(reactor.core.publisher.Mono) DisposableServer(reactor.netty.DisposableServer) DisposableServer(reactor.netty.DisposableServer)

Example 2 with Metrics

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

the class PooledConnectionProviderDefaultMetricsTest method doTest.

private void doTest(HttpServer server, HttpClient client, String poolName, boolean clientMetricsEnabled, int expectedMaxConnection, int expectedMaxPendingAcquire) throws Exception {
    disposableServer = server.handle((req, res) -> res.header("Connection", "close").sendString(Mono.just("test"))).bindNow();
    AtomicBoolean metrics = new AtomicBoolean(false);
    CountDownLatch latch = new CountDownLatch(1);
    boolean isSecured = client.configuration().sslProvider() != null;
    client.doOnResponse((res, conn) -> {
        conn.channel().closeFuture().addListener(f -> latch.countDown());
        double totalConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName);
        double activeConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, poolName);
        double maxConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_CONNECTIONS, poolName);
        double idleConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, poolName);
        double pendingConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_CONNECTIONS, poolName);
        double maxPendingConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_PENDING_CONNECTIONS, poolName);
        if (totalConnections == 1 && activeConnections == 1 && idleConnections == 0 && pendingConnections == 0 && maxConnections == expectedMaxConnection && maxPendingConnections == expectedMaxPendingAcquire) {
            metrics.set(true);
        }
        if (isSecured) {
            double activeStreams = getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_STREAMS, "http2." + poolName);
            double pendingStreams = getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_STREAMS, "http2." + poolName);
            if (activeStreams == 1 && pendingStreams == 0) {
                metrics.set(true);
            }
        }
    }).metrics(clientMetricsEnabled, Function.identity()).get().uri("/").responseContent().aggregate().asString().block(Duration.ofSeconds(30));
    assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    assertThat(metrics.get()).isTrue();
    if (isSecured) {
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName)).isEqualTo(1);
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, poolName)).isEqualTo(1);
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_STREAMS, "http2." + poolName)).isEqualTo(0);
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_STREAMS, "http2." + poolName)).isEqualTo(0);
    } else {
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName)).isEqualTo(0);
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, poolName)).isEqualTo(0);
    }
    assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, poolName)).isEqualTo(0);
    assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_CONNECTIONS, poolName)).isEqualTo(0);
    assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_CONNECTIONS, poolName)).isEqualTo(expectedMaxConnection);
    assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_PENDING_CONNECTIONS, poolName)).isEqualTo(expectedMaxPendingAcquire);
}
Also used : TOTAL_CONNECTIONS(reactor.netty.Metrics.TOTAL_CONNECTIONS) BeforeEach(org.junit.jupiter.api.BeforeEach) MAX_CONNECTIONS(reactor.netty.Metrics.MAX_CONNECTIONS) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) BaseHttpTest(reactor.netty.BaseHttpTest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NAME(reactor.netty.Metrics.NAME) Function(java.util.function.Function) PENDING_STREAMS(reactor.netty.Metrics.PENDING_STREAMS) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) CONNECTION_PROVIDER_PREFIX(reactor.netty.Metrics.CONNECTION_PROVIDER_PREFIX) BeforeAll(org.junit.jupiter.api.BeforeAll) ACTIVE_CONNECTIONS(reactor.netty.Metrics.ACTIVE_CONNECTIONS) Duration(java.time.Duration) PENDING_CONNECTIONS(reactor.netty.Metrics.PENDING_CONNECTIONS) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) IDLE_CONNECTIONS(reactor.netty.Metrics.IDLE_CONNECTIONS) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) HttpServer(reactor.netty.http.server.HttpServer) MAX_PENDING_CONNECTIONS(reactor.netty.Metrics.MAX_PENDING_CONNECTIONS) MeterRegistry(io.micrometer.api.instrument.MeterRegistry) Metrics(io.micrometer.api.instrument.Metrics) ACTIVE_STREAMS(reactor.netty.Metrics.ACTIVE_STREAMS) Gauge(io.micrometer.api.instrument.Gauge) HttpClient(reactor.netty.http.client.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with Metrics

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

the class PooledConnectionProviderDefaultMetricsTest method testConnectionProviderMetricsEnableAndHttpClientMetricsDisabledHttp2.

@Test
void testConnectionProviderMetricsEnableAndHttpClientMetricsDisabledHttp2() throws Exception {
    Http2SslContextSpec serverCtx = Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey());
    Http2SslContextSpec clientCtx = Http2SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
    ConnectionProvider provider = ConnectionProvider.builder("test4").maxConnections(1).pendingAcquireMaxCount(10).metrics(true).lifo().build();
    try {
        doTest(createServer().protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(serverCtx)), createClient(provider, () -> disposableServer.address()).protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(clientCtx)), "test4", false, 1, 10);
    } finally {
        provider.disposeLater().block(Duration.ofSeconds(5));
    }
}
Also used : Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) TOTAL_CONNECTIONS(reactor.netty.Metrics.TOTAL_CONNECTIONS) BeforeEach(org.junit.jupiter.api.BeforeEach) MAX_CONNECTIONS(reactor.netty.Metrics.MAX_CONNECTIONS) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) BaseHttpTest(reactor.netty.BaseHttpTest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NAME(reactor.netty.Metrics.NAME) Function(java.util.function.Function) PENDING_STREAMS(reactor.netty.Metrics.PENDING_STREAMS) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) CONNECTION_PROVIDER_PREFIX(reactor.netty.Metrics.CONNECTION_PROVIDER_PREFIX) BeforeAll(org.junit.jupiter.api.BeforeAll) ACTIVE_CONNECTIONS(reactor.netty.Metrics.ACTIVE_CONNECTIONS) Duration(java.time.Duration) PENDING_CONNECTIONS(reactor.netty.Metrics.PENDING_CONNECTIONS) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) IDLE_CONNECTIONS(reactor.netty.Metrics.IDLE_CONNECTIONS) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) HttpServer(reactor.netty.http.server.HttpServer) MAX_PENDING_CONNECTIONS(reactor.netty.Metrics.MAX_PENDING_CONNECTIONS) MeterRegistry(io.micrometer.api.instrument.MeterRegistry) Metrics(io.micrometer.api.instrument.Metrics) ACTIVE_STREAMS(reactor.netty.Metrics.ACTIVE_STREAMS) Gauge(io.micrometer.api.instrument.Gauge) HttpClient(reactor.netty.http.client.HttpClient) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 4 with Metrics

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

the class TransportEventLoopMetricsTest method testEventLoopMetrics.

@Test
void testEventLoopMetrics() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    DisposableServer server = null;
    Connection client = null;
    LoopResources loop = null;
    try {
        loop = LoopResources.create(TransportEventLoopMetricsTest.class.getName(), 3, true);
        server = TcpServer.create().port(0).metrics(true).runOn(loop).doOnConnection(c -> {
            EventLoop eventLoop = c.channel().eventLoop();
            IntStream.range(0, 10).forEach(i -> eventLoop.execute(() -> {
            }));
            if (eventLoop instanceof SingleThreadEventExecutor) {
                SingleThreadEventExecutor singleThreadEventExecutor = (SingleThreadEventExecutor) eventLoop;
                String[] tags = new String[] { NAME, singleThreadEventExecutor.threadProperties().name() };
                assertThat(getGaugeValue(EVENT_LOOP_PREFIX + PENDING_TASKS, tags)).isEqualTo(10);
                latch.countDown();
            }
        }).wiretap(true).bindNow();
        assertThat(server).isNotNull();
        client = TcpClient.create().port(server.port()).wiretap(true).connectNow();
        assertThat(client).isNotNull();
        assertThat(latch.await(5, TimeUnit.SECONDS)).as("Did not find 10 pending tasks from meter").isTrue();
    } finally {
        if (client != null) {
            client.disposeNow();
        }
        if (server != null) {
            server.disposeNow();
        }
        if (loop != null) {
            loop.disposeLater().block(Duration.ofSeconds(10));
        }
    }
}
Also used : LoopResources(reactor.netty.resources.LoopResources) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) EVENT_LOOP_PREFIX(reactor.netty.Metrics.EVENT_LOOP_PREFIX) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) SingleThreadEventExecutor(io.netty.util.concurrent.SingleThreadEventExecutor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) NAME(reactor.netty.Metrics.NAME) EventLoop(io.netty.channel.EventLoop) Test(org.junit.jupiter.api.Test) TcpClient(reactor.netty.tcp.TcpClient) TimeUnit(java.util.concurrent.TimeUnit) PENDING_TASKS(reactor.netty.Metrics.PENDING_TASKS) 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) TcpServer(reactor.netty.tcp.TcpServer) Connection(reactor.netty.Connection) DisposableServer(reactor.netty.DisposableServer) Gauge(io.micrometer.api.instrument.Gauge) EventLoop(io.netty.channel.EventLoop) DisposableServer(reactor.netty.DisposableServer) SingleThreadEventExecutor(io.netty.util.concurrent.SingleThreadEventExecutor) LoopResources(reactor.netty.resources.LoopResources) Connection(reactor.netty.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 5 with Metrics

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

the class HttpMetricsHandlerTests method setUp.

/**
 * Initialization done before running each test.
 * <ul>
 *  <li> /1 is used by testExistingEndpoint test</li>
 *  <li> /2 is used by testExistingEndpoint, and testUriTagValueFunctionNotSharedForClient tests</li>
 *  <li> /3 does not exists but is used by testNonExistingEndpoint, checkExpectationsNonExisting tests</li>
 *  <li> /4 is used by testServerConnectionsMicrometer test</li>
 *  <li> /5 is used by testServerConnectionsRecorder test</li>
 * </ul>
 */
@BeforeEach
void setUp() {
    httpServer = createServer().host("127.0.0.1").metrics(true, Function.identity()).route(r -> r.post("/1", (req, res) -> res.header("Connection", "close").send(req.receive().retain().delayElements(Duration.ofMillis(10)))).post("/2", (req, res) -> res.header("Connection", "close").send(req.receive().retain().delayElements(Duration.ofMillis(10)))).post("/4", (req, res) -> res.header("Connection", "close").send(req.receive().retain().doOnNext(b -> checkServerConnectionsMicrometer(req.hostAddress())))).post("/5", (req, res) -> res.header("Connection", "close").send(req.receive().retain().doOnNext(b -> checkServerConnectionsRecorder(req.hostAddress())))));
    provider = ConnectionProvider.create("HttpMetricsHandlerTests", 1);
    httpClient = createClient(provider, () -> disposableServer.address()).metrics(true, Function.identity());
    registry = new SimpleMeterRegistry();
    Metrics.addRegistry(registry);
}
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) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

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