Netty 仪表化
Micrometer 支持将指标绑定到 Netty。
你可以从 ByteBuf
分配器和 EventLoopGroup
实例中收集指标。如果你已经知道这些资源,可以在启动时只创建一次监控:
// 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 的基础设施可以通过多种方式进行配置,因此你也可以在运行时按需进行插装,即当资源被使用时再进行插装。以下示例展示了如何按需创建插装:
@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);
}
}
警告
如果你使用懒加载(lazy instrumentation),必须确保不会为同一资源多次绑定指标,因为这可能会导致运行时的问题。