From c43fbbd9e88f4e81f9be99d2f37ea39a86b82a6c Mon Sep 17 00:00:00 2001 From: Jose Fernandez Date: Thu, 29 Jul 2021 12:10:38 -0700 Subject: [PATCH] Register the allocator memory metric polled gauge only once --- .../com/netflix/zuul/netty/server/Server.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/zuul-core/src/main/java/com/netflix/zuul/netty/server/Server.java b/zuul-core/src/main/java/com/netflix/zuul/netty/server/Server.java index f00cf6ceee..fb5c7ade71 100644 --- a/zuul-core/src/main/java/com/netflix/zuul/netty/server/Server.java +++ b/zuul-core/src/main/java/com/netflix/zuul/netty/server/Server.java @@ -203,6 +203,19 @@ public void start() addressesToChannels.put(requestedNamedAddr.withNewSocket(chan.localAddress()), chan); allBindFutures.add(nettyServerFuture); } + + // All channels should share a single ByteBufAllocator instance. + // Add metrics to monitor that allocator's memory usage. + if (!allBindFutures.isEmpty()) { + ByteBufAllocator alloc = allBindFutures.get(0).channel().alloc(); + if (alloc instanceof ByteBufAllocatorMetricProvider) { + ByteBufAllocatorMetric metrics = ((ByteBufAllocatorMetricProvider) alloc).metric(); + PolledMeter.using(registry).withId(registry.createId("zuul.nettybuffermem.live", "type", "heap")) + .monitorValue(metrics, ByteBufAllocatorMetric::usedHeapMemory); + PolledMeter.using(registry).withId(registry.createId("zuul.nettybuffermem.live", "type", "direct")) + .monitorValue(metrics, ByteBufAllocatorMetric::usedDirectMemory); + } + } } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -277,15 +290,6 @@ private ChannelFuture setupServerBootstrap( // Bind and start to accept incoming connections. ChannelFuture bindFuture = serverBootstrap.bind(listenAddress.unwrap()); - ByteBufAllocator alloc = bindFuture.channel().alloc(); - if (alloc instanceof ByteBufAllocatorMetricProvider) { - ByteBufAllocatorMetric metrics = ((ByteBufAllocatorMetricProvider) alloc).metric(); - PolledMeter.using(registry).withId(registry.createId("zuul.nettybuffermem.live", "type", "heap")) - .monitorValue(metrics, ByteBufAllocatorMetric::usedHeapMemory); - PolledMeter.using(registry).withId(registry.createId("zuul.nettybuffermem.live", "type", "direct")) - .monitorValue(metrics, ByteBufAllocatorMetric::usedDirectMemory); - } - try { return bindFuture.sync(); } catch (Exception e) {