forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace the id label with a name label for Netty allocator
The Netty allocator metrics exposed an `id` label using the allocator hashcode. However, this does not make a lot of sense when you need to aggregate metrics from multiple metrics. Thus, this commit replaces the `id` with a `name` label with stable names for the different allocators. Unfortunately, the initial code was in micrometer. This commit forks the 2 classes required to replace `id` with `name` and adds stable names to the different allocators. Fix quarkusio#45847
- Loading branch information
1 parent
ddd9c85
commit 2ee2894
Showing
6 changed files
with
374 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...ntime/src/main/java/io/quarkus/micrometer/runtime/binder/netty/NettyAllocatorMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package io.quarkus.micrometer.runtime.binder.netty; | ||
|
||
import io.micrometer.core.instrument.Gauge; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Tags; | ||
import io.micrometer.core.instrument.binder.MeterBinder; | ||
import io.netty.buffer.ByteBufAllocatorMetric; | ||
import io.netty.buffer.ByteBufAllocatorMetricProvider; | ||
import io.netty.buffer.PooledByteBufAllocator; | ||
import io.netty.buffer.PooledByteBufAllocatorMetric; | ||
|
||
/** | ||
* {@link MeterBinder} for Netty memory allocators. | ||
* <p> | ||
* This class is based on the MicroMeter NettyAllocatorMetrics class, but remove the "id" from the tags are it's | ||
* computed from the `hashCode` which does not allow aggregation across processed. | ||
* Instead, it gets a {@code name} label indicating an unique name for the allocator. | ||
*/ | ||
public class NettyAllocatorMetrics implements MeterBinder { | ||
|
||
private final ByteBufAllocatorMetricProvider allocator; | ||
private final String name; | ||
|
||
/** | ||
* Create a binder instance for the given allocator. | ||
* | ||
* @param name the unique name for the allocator | ||
* @param allocator the {@code ByteBuf} allocator to instrument | ||
*/ | ||
public NettyAllocatorMetrics(String name, ByteBufAllocatorMetricProvider allocator) { | ||
this.name = name; | ||
this.allocator = allocator; | ||
} | ||
|
||
@Override | ||
public void bindTo(MeterRegistry registry) { | ||
ByteBufAllocatorMetric allocatorMetric = this.allocator.metric(); | ||
Tags tags = Tags.of( | ||
NettyMeters.AllocatorKeyNames.NAME.asString(), this.name, | ||
NettyMeters.AllocatorKeyNames.ALLOCATOR_TYPE.asString(), this.allocator.getClass().getSimpleName()); | ||
|
||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_MEMORY_USED.getName(), allocatorMetric, | ||
ByteBufAllocatorMetric::usedHeapMemory) | ||
.tags(tags.and(NettyMeters.AllocatorMemoryKeyNames.MEMORY_TYPE.asString(), "heap")) | ||
.register(registry); | ||
|
||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_MEMORY_USED.getName(), allocatorMetric, | ||
ByteBufAllocatorMetric::usedDirectMemory) | ||
.tags(tags.and(NettyMeters.AllocatorMemoryKeyNames.MEMORY_TYPE.asString(), "direct")) | ||
.register(registry); | ||
|
||
if (this.allocator instanceof PooledByteBufAllocator pooledByteBufAllocator) { | ||
PooledByteBufAllocatorMetric pooledAllocatorMetric = pooledByteBufAllocator.metric(); | ||
|
||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_MEMORY_PINNED.getName(), pooledByteBufAllocator, | ||
PooledByteBufAllocator::pinnedHeapMemory) | ||
.tags(tags.and(NettyMeters.AllocatorMemoryKeyNames.MEMORY_TYPE.asString(), "heap")) | ||
.register(registry); | ||
|
||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_MEMORY_PINNED.getName(), pooledByteBufAllocator, | ||
PooledByteBufAllocator::pinnedDirectMemory) | ||
.tags(tags.and(NettyMeters.AllocatorMemoryKeyNames.MEMORY_TYPE.asString(), "direct")) | ||
.register(registry); | ||
|
||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_POOLED_ARENAS.getName(), pooledAllocatorMetric, | ||
PooledByteBufAllocatorMetric::numHeapArenas) | ||
.tags(tags.and(NettyMeters.AllocatorMemoryKeyNames.MEMORY_TYPE.asString(), "heap")) | ||
.register(registry); | ||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_POOLED_ARENAS.getName(), pooledAllocatorMetric, | ||
PooledByteBufAllocatorMetric::numDirectArenas) | ||
.tags(tags.and(NettyMeters.AllocatorMemoryKeyNames.MEMORY_TYPE.asString(), "direct")) | ||
.register(registry); | ||
|
||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_POOLED_CACHE_SIZE.getName(), pooledAllocatorMetric, | ||
PooledByteBufAllocatorMetric::normalCacheSize) | ||
.tags(tags.and(NettyMeters.AllocatorPooledCacheKeyNames.CACHE_TYPE.asString(), "normal")) | ||
.register(registry); | ||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_POOLED_CACHE_SIZE.getName(), pooledAllocatorMetric, | ||
PooledByteBufAllocatorMetric::smallCacheSize) | ||
.tags(tags.and(NettyMeters.AllocatorPooledCacheKeyNames.CACHE_TYPE.asString(), "small")) | ||
.register(registry); | ||
|
||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_POOLED_THREADLOCAL_CACHES.getName(), pooledAllocatorMetric, | ||
PooledByteBufAllocatorMetric::numThreadLocalCaches) | ||
.tags(tags) | ||
.register(registry); | ||
|
||
Gauge | ||
.builder(NettyMeters.ALLOCATOR_POOLED_CHUNK_SIZE.getName(), pooledAllocatorMetric, | ||
PooledByteBufAllocatorMetric::chunkSize) | ||
.tags(tags) | ||
.register(registry); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.