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();
}
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);
}
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));
}
}
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));
}
}
}
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);
}
Aggregations