Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;

Expand All @@ -51,6 +53,8 @@
public class ExecutorServiceMetricsBinder implements BeanCreatedEventListener<ExecutorService> {

private static final String THREAD_PER_TASK_EXECUTOR = "java.util.concurrent.ThreadPerTaskExecutor";
private static final String NETTY_EVENT_LOOP_GROUP = "io.netty.channel.EventLoopGroup";
private static final ConcurrentMap<Class<?>, Boolean> NETTY_EVENT_LOOP_GROUP_TYPES = new ConcurrentHashMap<>();

private final BeanProvider<MeterRegistry> meterRegistryProvider;

Expand All @@ -70,7 +74,7 @@ public ExecutorService onCreated(BeanCreatedEvent<ExecutorService> event) {
unwrapped = ((InstrumentedExecutorService) unwrapped).getTarget();
}
// Netty EventLoopGroups require separate instrumentation.
if (unwrapped.getClass().getName().startsWith("io.netty")) {
if (isNettyEventLoopGroup(unwrapped.getClass()) || unwrapped.getClass().getName().startsWith("io.netty")) {
return unwrapped;
}
Comment thread
n0tl3ss marked this conversation as resolved.
// ExecutorServiceMetrics does not provide metrics for virtual threads
Expand Down Expand Up @@ -125,4 +129,24 @@ public Runnable instrument(Runnable command) {
};
}
}

private static boolean isNettyEventLoopGroup(Class<?> type) {
Boolean cached = NETTY_EVENT_LOOP_GROUP_TYPES.get(type);
if (cached != null) {
return cached;
}
boolean result = hasNettyEventLoopGroup(type);
NETTY_EVENT_LOOP_GROUP_TYPES.putIfAbsent(type, result);
return result;
}

private static boolean hasNettyEventLoopGroup(Class<?> type) {
for (Class<?> interfaceType : type.getInterfaces()) {
if (NETTY_EVENT_LOOP_GROUP.equals(interfaceType.getName()) || isNettyEventLoopGroup(interfaceType)) {
return true;
}
}
Class<?> superclass = type.getSuperclass();
return superclass != null && isNettyEventLoopGroup(superclass);
}
Comment thread
n0tl3ss marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.jspecify.annotations.Nullable;
import io.micronaut.http.netty.channel.EpollAvailabilityCondition;
import io.micronaut.http.netty.channel.EpollEventLoopGroupFactory;
import io.micronaut.http.netty.channel.EventLoopGroupFactory;
import io.netty.channel.DefaultSelectStrategyFactory;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
Expand All @@ -47,8 +46,8 @@
*/
@Singleton
@Internal
@Replaces(bean = EpollEventLoopGroupFactory.class, named = EventLoopGroupFactory.NATIVE)
@Named(EventLoopGroupFactory.NATIVE)
@Replaces(bean = EpollEventLoopGroupFactory.class, named = EpollEventLoopGroupFactory.NAME)
@Named(EpollEventLoopGroupFactory.NAME)
Comment thread
n0tl3ss marked this conversation as resolved.
@Requires(classes = Epoll.class, condition = EpollAvailabilityCondition.class)
@RequiresMetrics
@Requires(property = MICRONAUT_METRICS_BINDERS + ".netty.queues.enabled", defaultValue = FALSE, notEquals = FALSE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;
import io.micronaut.configuration.metrics.annotation.RequiresMetrics;
import io.micronaut.context.BeanContext;
import io.micronaut.context.BeanProvider;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Internal;
import io.micronaut.http.server.netty.NettyHttpServer;
import io.micronaut.http.netty.channel.EventLoopGroupConfiguration;
import io.micronaut.http.netty.channel.TaskQueueInterceptor;
import io.micronaut.inject.qualifiers.Qualifiers;
import io.netty.channel.EventLoopTaskQueueFactory;
import io.netty.util.internal.PlatformDependent;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

import static io.micronaut.configuration.metrics.binder.netty.NettyMetrics.COUNT;
Expand Down Expand Up @@ -58,65 +64,59 @@
@Requires(property = MICRONAUT_METRICS_BINDERS + ".netty.queues.enabled", defaultValue = FALSE, notEquals = FALSE)
@Requires(classes = EventLoopTaskQueueFactory.class)
@Internal
final class InstrumentedEventLoopTaskQueueFactory implements EventLoopTaskQueueFactory {

private static final AtomicInteger PARENT_COUNTER = new AtomicInteger(-1);
private static final AtomicInteger WORKER_COUNTER = new AtomicInteger(-1);
@SuppressWarnings("java:S1874")
final class InstrumentedEventLoopTaskQueueFactory implements EventLoopTaskQueueFactory, TaskQueueInterceptor {

private final BeanProvider<MeterRegistry> meterRegistryProvider;
private final Counter parentTaskCounter;
private final Counter workerTaskCounter;
private final Timer globalParentWaitTimeTimer;
private final Timer globalParentExecutionTimer;
private final Timer globalWorkerWaitTimeTimer;
private final Timer globalWorkerExecutionTimer;
private final BeanContext beanContext;
private final ConcurrentMap<String, EventLoopGroupMetrics> metrics = new ConcurrentHashMap<>();
Comment thread
n0tl3ss marked this conversation as resolved.

/**
* @param meterRegistryProvider the metric registry provider
* @param beanContext the bean context
*/
public InstrumentedEventLoopTaskQueueFactory(BeanProvider<MeterRegistry> meterRegistryProvider) {
public InstrumentedEventLoopTaskQueueFactory(BeanProvider<MeterRegistry> meterRegistryProvider,
BeanContext beanContext) {
this.meterRegistryProvider = meterRegistryProvider;
globalParentWaitTimeTimer = Timer.builder(dot(NETTY, QUEUE, GLOBAL, WAIT_TIME))
.description("Global wait time spent in the parent Queues.")
.tag(GROUP, PARENT)
.publishPercentileHistogram()
.register(meterRegistryProvider.get());
globalParentExecutionTimer = Timer.builder(dot(NETTY, QUEUE, GLOBAL, EXECUTION_TIME))
.description("Global parent runnable execution time.")
.tag(GROUP, PARENT)
.publishPercentileHistogram()
.register(meterRegistryProvider.get());
globalWorkerWaitTimeTimer = Timer.builder(dot(NETTY, QUEUE, GLOBAL, WAIT_TIME))
.description("Global wait time spent in the worker Queues.")
.tag(GROUP, WORKER)
.publishPercentileHistogram()
.register(meterRegistryProvider.get());
globalWorkerExecutionTimer = Timer.builder(dot(NETTY, QUEUE, GLOBAL, EXECUTION_TIME))
.description("Global worker runnable execution time.")
.tag(GROUP, WORKER)
.publishPercentileHistogram()
.register(meterRegistryProvider.get());
parentTaskCounter = Counter.builder(dot(NETTY, QUEUE, GLOBAL, ELEMENT, COUNT))
.tag(GROUP, PARENT)
.register(meterRegistryProvider.get());
workerTaskCounter = Counter.builder(dot(NETTY, QUEUE, GLOBAL, ELEMENT, COUNT))
.tag(GROUP, WORKER)
.register(meterRegistryProvider.get());
this.beanContext = beanContext;
metrics.put(PARENT, EventLoopGroupMetrics.create(meterRegistryProvider.get(), PARENT));
metrics.put(WORKER, EventLoopGroupMetrics.create(meterRegistryProvider.get(), WORKER));
}

@Override
public Queue<Runnable> newTaskQueue(int maxCapacity) {
final String kind = findOrigin();
final boolean parent = PARENT.equals(kind);
return new MonitoredQueue(parent ? PARENT_COUNTER.incrementAndGet() : WORKER_COUNTER.incrementAndGet(),
meterRegistryProvider.get(),
Tag.of(GROUP, kind),
parent ? parentTaskCounter : workerTaskCounter,
parent ? globalParentWaitTimeTimer : globalWorkerWaitTimeTimer,
parent ? globalParentExecutionTimer : globalWorkerExecutionTimer,
return newMonitoredQueue(kind,
maxCapacity == Integer.MAX_VALUE ? PlatformDependent.<Runnable>newMpscQueue() : PlatformDependent.<Runnable>newMpscQueue(maxCapacity));
}

@Override
public Queue<Runnable> wrapTaskQueue(String groupName, Queue<Runnable> original) {
return newMonitoredQueue(groupName, original);
}

private Queue<Runnable> newMonitoredQueue(String groupName, Queue<Runnable> queue) {
String name = normalizeGroupName(groupName);
EventLoopGroupMetrics groupMetrics = metrics.computeIfAbsent(name, group -> EventLoopGroupMetrics.create(meterRegistryProvider.get(), group));
return new MonitoredQueue(groupMetrics.queueCounter.incrementAndGet(),
meterRegistryProvider.get(),
Tag.of(GROUP, name),
groupMetrics.taskCounter,
groupMetrics.globalWaitTimeTimer,
groupMetrics.globalExecutionTimer,
queue);
}

private String normalizeGroupName(String groupName) {
if (groupName == null || groupName.isEmpty()) {
return WORKER;
}
if (PARENT.equals(groupName) || WORKER.equals(groupName) || EventLoopGroupConfiguration.DEFAULT.equals(groupName)) {
return groupName;
}
return beanContext.findBean(EventLoopGroupConfiguration.class, Qualifiers.byName(groupName)).isPresent() ? groupName : WORKER;
}

private String findOrigin() {
for (StackTraceElement elt: Thread.currentThread().getStackTrace()) {
if (NettyHttpServer.class.getName().equals(elt.getClassName()) && "createWorkerEventLoopGroup".equals(elt.getMethodName())) {
Expand All @@ -129,4 +129,26 @@ private String findOrigin() {
return WORKER;
}

private record EventLoopGroupMetrics(AtomicInteger queueCounter,
Counter taskCounter,
Timer globalWaitTimeTimer,
Timer globalExecutionTimer) {
static EventLoopGroupMetrics create(MeterRegistry meterRegistry, String group) {
Timer globalWaitTimeTimer = Timer.builder(dot(NETTY, QUEUE, GLOBAL, WAIT_TIME))
.description("Global wait time spent in the event loop group queues.")
.tag(GROUP, group)
.publishPercentileHistogram()
.register(meterRegistry);
Timer globalExecutionTimer = Timer.builder(dot(NETTY, QUEUE, GLOBAL, EXECUTION_TIME))
.description("Global runnable execution time for the event loop group.")
.tag(GROUP, group)
.publishPercentileHistogram()
.register(meterRegistry);
Counter taskCounter = Counter.builder(dot(NETTY, QUEUE, GLOBAL, ELEMENT, COUNT))
.tag(GROUP, group)
.register(meterRegistry);
return new EventLoopGroupMetrics(new AtomicInteger(-1), taskCounter, globalWaitTimeTimer, globalExecutionTimer);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Internal;
import org.jspecify.annotations.Nullable;
import io.micronaut.http.netty.channel.EventLoopGroupFactory;
import io.micronaut.http.netty.channel.KQueueAvailabilityCondition;
import io.micronaut.http.netty.channel.KQueueEventLoopGroupFactory;
import io.netty.channel.DefaultSelectStrategyFactory;
Expand All @@ -47,8 +46,8 @@
*/
@Singleton
@Internal
@Replaces(bean = KQueueEventLoopGroupFactory.class, named = EventLoopGroupFactory.NATIVE)
@Named(EventLoopGroupFactory.NATIVE)
@Replaces(bean = KQueueEventLoopGroupFactory.class, named = KQueueEventLoopGroupFactory.NAME)
@Named(KQueueEventLoopGroupFactory.NAME)
@Requires(classes = KQueue.class, condition = KQueueAvailabilityCondition.class)
@RequiresMetrics
@Requires(property = MICRONAUT_METRICS_BINDERS + ".netty.queues.enabled", defaultValue = FALSE, notEquals = FALSE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2017-2026 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.configuration.metrics.binder.netty;

import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Internal;
import io.micronaut.http.netty.channel.EventLoopGroupFactory;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

/**
* Backwards-compatible native event loop group factory aliases.
*
* @since 6.0.0
*/
@Factory
@Internal
final class InstrumentedNativeEventLoopGroupFactoryAliases {

@Singleton
@Named(EventLoopGroupFactory.NATIVE)
@Requires(beans = InstrumentedEpollEventLoopGroupFactory.class)
EventLoopGroupFactory epollNativeEventLoopGroupFactory(InstrumentedEpollEventLoopGroupFactory factory) {
return factory;
}

@Singleton
@Named(EventLoopGroupFactory.NATIVE)
@Requires(beans = InstrumentedKQueueEventLoopGroupFactory.class)
EventLoopGroupFactory kQueueNativeEventLoopGroupFactory(InstrumentedKQueueEventLoopGroupFactory factory) {
return factory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.netty.util.concurrent.DefaultEventExecutorChooserFactory;
import io.netty.util.concurrent.RejectedExecutionHandlers;
import io.netty.util.concurrent.ThreadPerTaskExecutor;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

import java.nio.channels.spi.SelectorProvider;
Expand All @@ -43,6 +44,7 @@
* @since 2.0
*/
@Singleton
@Named(NioEventLoopGroupFactory.NAME)
@Internal
@Replaces(bean = NioEventLoopGroupFactory.class)
@RequiresMetrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.micronaut.context.annotation.Factory
import io.micronaut.context.annotation.Requires
import io.micronaut.inject.qualifiers.Qualifiers
import io.micronaut.scheduling.TaskExecutors
import io.micronaut.scheduling.instrument.InstrumentedExecutorService
import io.netty.channel.DefaultEventLoop
import io.netty.channel.EventLoopGroup
import jakarta.inject.Named
Expand Down Expand Up @@ -79,6 +80,19 @@ class ExecutorServiceMetricsBinderSpec extends Specification {
context.close()
}

void "test event loop group outside netty package not instrumented"() {
when:
ApplicationContext context = ApplicationContext.run()
ExecutorService executorService = context.getBean(ExecutorService, Qualifiers.byName("delegating"))

then:
executorService instanceof TestEventLoopGroup
!(executorService instanceof InstrumentedExecutorService)

cleanup:
context.close()
}

@Issue("https://github.com/micronaut-projects/micronaut-micrometer/issues/679")
void "test the virtual task executor is unbound with no warnings logged"() {
when:
Expand Down Expand Up @@ -128,6 +142,15 @@ class ExecutorServiceMetricsBinderSpec extends Specification {
EventLoopGroup eventLoopGroup() {
return new DefaultEventLoop()
}

@Singleton
@Named("delegating")
TestEventLoopGroup delegatingEventLoopGroup() {
return new TestEventLoopGroup()
}
}

static class TestEventLoopGroup extends DefaultEventLoop {
}

class SimpleStreamsListener implements IStandardStreamsListener {
Expand Down
Loading
Loading