Netty Instrumentation

Micrometer supports binding metrics to Netty.

You can collect metrics from ByteBuf allocators and from EventLoopGroup instances. If you already know the resources, you can create instrumentation only once at startup:

// Create or get an existing resources
DefaultEventLoopGroup eventExecutors = new DefaultEventLoopGroup();
UnpooledByteBufAllocator unpooledByteBufAllocator = new UnpooledByteBufAllocator(false);
// Use binders to instrument them
new NettyEventExecutorMetrics(eventExecutors).bindTo(this.registry);
new NettyAllocatorMetrics(unpooledByteBufAllocator).bindTo(this.registry);

Netty infrastructure can be configured in many ways, so you can also instrument lazily at runtime, as resources are used. The fllowing example shows how to lazily create instrumentation:

@Override
protected void initChannel(SocketChannel channel) throws Exception {
    EventLoop eventLoop = channel.eventLoop();
    if (!isEventLoopInstrumented(eventLoop)) {
        new NettyEventExecutorMetrics(eventLoop).bindTo(this.meterRegistry);
    }
    ByteBufAllocator allocator = channel.alloc();
    if (!isAllocatorInstrumented(allocator) && allocator instanceof ByteBufAllocatorMetricProvider) {
        ByteBufAllocatorMetricProvider allocatorMetric = (ByteBufAllocatorMetricProvider) allocator;
        new NettyAllocatorMetrics(allocatorMetric).bindTo(this.meterRegistry);
    }
}
If you use lazy instrumentation, you must ensure that you do not bind metrics for the same resource multiple times, as this can have runtime drawbacks.