Skip to content

Remove cache and use MeterProvider for dynamic tags #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2024
Merged
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
44 changes: 9 additions & 35 deletions src/main/java/io/vertx/micrometer/impl/AbstractMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.MetricsDomain;
import io.vertx.micrometer.MetricsNaming;
import io.vertx.micrometer.impl.meters.LongGaugeBuilder;
import io.vertx.micrometer.impl.meters.LongGauges;
import io.vertx.micrometer.impl.tags.IgnoredTag;

import java.util.EnumSet;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;

/**
Expand All @@ -39,15 +37,15 @@ public abstract class AbstractMetrics implements MicrometerMetrics {
protected final MeterRegistry registry;
protected final MetricsNaming names;
private final String category;
private final EnumSet<Label> enabledLabels;
private final MeterCache meterCache;
protected final EnumSet<Label> enabledLabels;
private final LongGauges longGauges;

AbstractMetrics(MeterRegistry registry, MetricsNaming names, LongGauges longGauges, EnumSet<Label> enabledLabels, boolean meterCacheEnabled) {
AbstractMetrics(MeterRegistry registry, MetricsNaming names, LongGauges longGauges, EnumSet<Label> enabledLabels) {
this.registry = registry;
this.category = null;
this.enabledLabels = enabledLabels;
this.names = names;
this.meterCache = new MeterCache(meterCacheEnabled, registry, longGauges);
this.longGauges = longGauges;
}

AbstractMetrics(AbstractMetrics parent, MetricsDomain domain) {
Expand All @@ -57,7 +55,7 @@ public abstract class AbstractMetrics implements MicrometerMetrics {
AbstractMetrics(AbstractMetrics parent, String category) {
this.registry = parent.registry;
this.enabledLabels = parent.enabledLabels;
this.meterCache = parent.meterCache;
this.longGauges = parent.longGauges;
this.category = category;
this.names = parent.names.withBaseName(baseName());
}
Expand All @@ -76,31 +74,7 @@ public final String baseName() {
return category == null ? null : "vertx." + category + ".";
}

Counter counter(String name, String description, Tags tags) {
return meterCache.getOrCreateCounter(name, description, tags);
}

LongAdder longGauge(String name, String description, Tags tags) {
return longGauge(name, description, tags, LongAdder::doubleValue);
}

LongAdder longGauge(String name, String description, Tags tags, ToDoubleFunction<LongAdder> func) {
return meterCache.getOrCreateLongGauge(name, description, tags, func);
}

DistributionSummary distributionSummary(String name, String description, Tags tags) {
return meterCache.getOrCreateDistributionSummary(name, description, tags);
}

Timer timer(String name, String description, Tags tags) {
return meterCache.getOrCreateTimer(name, description, tags);
}

<U> Tag toTag(Label label, Function<U, String> func, U u) {
return enabledLabels.contains(label) ? Tag.of(label.toString(), func.apply(u)) : IgnoredTag.INSTANCE;
}

<U, V> Tag toTag(Label label, BiFunction<U, V, String> func, U u, V v) {
return enabledLabels.contains(label) ? Tag.of(label.toString(), func.apply(u, v)) : IgnoredTag.INSTANCE;
LongGaugeBuilder longGaugeBuilder(String name, ToDoubleFunction<LongAdder> func) {
return longGauges.builder(name, func);
}
}
125 changes: 0 additions & 125 deletions src/main/java/io/vertx/micrometer/impl/MeterCache.java

This file was deleted.

36 changes: 28 additions & 8 deletions src/main/java/io/vertx/micrometer/impl/VertxClientMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.Timer.Sample;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.micrometer.impl.tags.Labels;
import io.vertx.micrometer.impl.tags.TagsWrapper;

import java.util.concurrent.atomic.LongAdder;

import static io.vertx.micrometer.Label.NAMESPACE;
import static io.vertx.micrometer.Label.REMOTE;
import static io.vertx.micrometer.impl.tags.TagsWrapper.of;

/**
* @author Joel Takvorian
Expand All @@ -43,12 +42,33 @@ class VertxClientMetrics extends AbstractMetrics implements ClientMetrics<Sample

VertxClientMetrics(AbstractMetrics parent, SocketAddress remoteAddress, String type, String namespace) {
super(parent, type);
TagsWrapper tags = of(toTag(REMOTE, Labels::address, remoteAddress), toTag(NAMESPACE, s -> s == null ? "" : s, namespace));
queueDelay = timer(names.getClientQueueTime(), "Time spent in queue before being processed", tags.unwrap());
queueSize = longGauge(names.getClientQueuePending(), "Number of pending elements in queue", tags.unwrap());
processingTime = timer(names.getClientProcessingTime(), "Processing time, from request start to response end", tags.unwrap());
processingPending = longGauge(names.getClientProcessingPending(), "Number of elements being processed", tags.unwrap());
resetCount = counter(names.getClientResetsCount(), "Total number of resets", tags.unwrap());
Tags tags = Tags.empty();
if (enabledLabels.contains(REMOTE)) {
tags = tags.and(REMOTE.toString(), Labels.address(remoteAddress));
}
if (enabledLabels.contains(NAMESPACE)) {
tags = tags.and(NAMESPACE.toString(), namespace == null ? "" : namespace);
}
queueDelay = Timer.builder(names.getClientQueueTime())
.description("Time spent in queue before being processed")
.tags(tags)
.register(registry);
queueSize = longGaugeBuilder(names.getClientQueuePending(), LongAdder::doubleValue)
.description("Number of pending elements in queue")
.tags(tags)
.register(registry);
processingTime = Timer.builder(names.getClientProcessingTime())
.description("Processing time, from request start to response end")
.tags(tags)
.register(registry);
processingPending = longGaugeBuilder(names.getClientProcessingPending(), LongAdder::doubleValue)
.description("Number of elements being processed")
.tags(tags)
.register(registry);
resetCount = Counter.builder(names.getClientResetsCount())
.description("Total number of resets")
.tags(tags)
.register(registry);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,49 @@
*/
package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Meter.MeterProvider;
import io.micrometer.core.instrument.Tags;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.DatagramSocketMetrics;
import io.vertx.micrometer.impl.tags.Labels;
import io.vertx.micrometer.impl.tags.TagsWrapper;

import static io.vertx.micrometer.Label.CLASS_NAME;
import static io.vertx.micrometer.Label.LOCAL;
import static io.vertx.micrometer.MetricsDomain.DATAGRAM_SOCKET;
import static io.vertx.micrometer.impl.tags.TagsWrapper.of;

/**
* @author Joel Takvorian
*/
class VertxDatagramSocketMetrics extends AbstractMetrics implements DatagramSocketMetrics {

private final DistributionSummary bytesWritten;
private final MeterProvider<Counter> errorCount;
private volatile DistributionSummary bytesRead;

VertxDatagramSocketMetrics(AbstractMetrics parent) {
super(parent, DATAGRAM_SOCKET);
bytesWritten = distributionSummary(names.getDatagramBytesWritten(), "Total number of datagram bytes sent", Tags.empty());
bytesWritten = DistributionSummary.builder(names.getDatagramBytesWritten())
.description("Total number of datagram bytes sent")
.register(registry);
errorCount = Counter.builder(names.getDatagramErrorCount())
.description("Total number of datagram errors")
.withRegistry(registry);
}

@Override
public void listening(String localName, SocketAddress localAddress) {
TagsWrapper tags = of(toTag(LOCAL, Labels::address, localAddress, localName));
bytesRead = distributionSummary(names.getDatagramBytesRead(), "Total number of datagram bytes received", tags.unwrap());
Tags tags;
if (enabledLabels.contains(LOCAL)) {
tags = Tags.of(LOCAL.toString(), Labels.address(localAddress, localName));
} else {
tags = Tags.empty();
}
bytesRead = DistributionSummary.builder(names.getDatagramBytesRead())
.description("Total number of datagram bytes received")
.tags(tags)
.register(registry);
}

@Override
Expand All @@ -61,8 +75,12 @@ public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long nu

@Override
public void exceptionOccurred(Void socketMetric, SocketAddress remoteAddress, Throwable t) {
TagsWrapper tags = of(toTag(CLASS_NAME, Class::getSimpleName, t.getClass()));
counter(names.getDatagramErrorCount(), "Total number of datagram errors", tags.unwrap())
.increment();
Tags tags;
if (enabledLabels.contains(CLASS_NAME)) {
tags = Tags.of(CLASS_NAME.toString(), t.getClass().getSimpleName());
} else {
tags = Tags.empty();
}
errorCount.withTags(tags).increment();
}
}
Loading
Loading