Skip to content

Commit

Permalink
Micrometer performance - use MeterProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat committed May 10, 2024
1 parent 48c7deb commit 096a23e
Showing 1 changed file with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import org.jboss.logging.Logger;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.LongTaskTimer;
import io.micrometer.core.instrument.Meter.MeterProvider;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
Expand Down Expand Up @@ -37,29 +39,39 @@ public class VertxHttpServerMetrics extends VertxTcpServerMetrics
implements HttpServerMetrics<HttpRequestMetric, LongTaskTimer.Sample, LongTaskTimer.Sample> {
static final Logger log = Logger.getLogger(VertxHttpServerMetrics.class);

HttpBinderConfiguration config;
final HttpBinderConfiguration config;

final String nameWebsocketConnections;
final String nameHttpServerPush;
final String nameHttpServerRequests;
final LongAdder activeRequests;

final MeterProvider<Timer> requestsTimer;
final MeterProvider<LongTaskTimer> websocketConnectionTimer;
final MeterProvider<Counter> pushCounter;

private final List<HttpServerMetricsTagsContributor> httpServerMetricsTagsContributors;

VertxHttpServerMetrics(MeterRegistry registry, HttpBinderConfiguration config) {
super(registry, "http.server", null);
this.config = config;

// not dev-mode changeable
nameWebsocketConnections = config.getHttpServerWebSocketConnectionsName();
nameHttpServerPush = config.getHttpServerPushName();
nameHttpServerRequests = config.getHttpServerRequestsName();

activeRequests = new LongAdder();
Gauge.builder(config.getHttpServerActiveRequestsName(), activeRequests, LongAdder::doubleValue)
.register(registry);

httpServerMetricsTagsContributors = resolveHttpServerMetricsTagsContributors();

// not dev-mode changeable -----
requestsTimer = Timer.builder(config.getHttpServerRequestsName())
.description("HTTP server request processing time")
.withRegistry(registry);

websocketConnectionTimer = LongTaskTimer.builder(config.getHttpServerWebSocketConnectionsName())
.description("Server web socket connection time")
.withRegistry(registry);

pushCounter = Counter.builder(config.getHttpServerPushName())
.description("HTTP server response push counter")
.withRegistry(registry);
// not dev-mode changeable -----ˆ
}

private List<HttpServerMetricsTagsContributor> resolveHttpServerMetricsTagsContributors() {
Expand Down Expand Up @@ -98,11 +110,12 @@ public HttpRequestMetric responsePushed(LongTaskTimer.Sample socketMetric, HttpM
config.getServerMatchPatterns(),
config.getServerIgnorePatterns());
if (path != null) {
registry.counter(nameHttpServerPush, Tags.of(
HttpCommonTags.uri(path, requestMetric.initialPath, response.statusCode()),
VertxMetricsTags.method(method),
VertxMetricsTags.outcome(response),
HttpCommonTags.status(response.statusCode())))
pushCounter
.withTags(Tags.of(
HttpCommonTags.uri(path, requestMetric.initialPath, response.statusCode()),
VertxMetricsTags.method(method),
VertxMetricsTags.outcome(response),
HttpCommonTags.status(response.statusCode())))
.increment();
}
log.debugf("responsePushed %s, %s", socketMetric, requestMetric);
Expand Down Expand Up @@ -150,14 +163,13 @@ public void requestReset(HttpRequestMetric requestMetric) {
config.getServerIgnorePatterns());
if (path != null) {
Timer.Sample sample = requestMetric.getSample();
Timer.Builder builder = Timer.builder(nameHttpServerRequests)
.tags(Tags.of(

sample.stop(requestsTimer
.withTags(Tags.of(
VertxMetricsTags.method(requestMetric.request().method()),
HttpCommonTags.uri(path, requestMetric.initialPath, 0),
Outcome.CLIENT_ERROR.asTag(),
HttpCommonTags.STATUS_RESET));

sample.stop(builder.register(registry));
HttpCommonTags.STATUS_RESET)));
}
requestMetric.requestEnded();
}
Expand Down Expand Up @@ -194,17 +206,15 @@ public void responseEnd(HttpRequestMetric requestMetric, HttpResponse response,
}
}
}
Timer.Builder builder = Timer.builder(nameHttpServerRequests).tags(allTags);

sample.stop(builder.register(registry));
sample.stop(requestsTimer.withTags(allTags));
}
requestMetric.requestEnded();
}

/**
* Called when a server web socket connects.
*
* @param socketMetric a Map for socket metric context or null
* @param requestMetric a RequestMetricContext or null
* @param serverWebSocket the server web socket
* @return a LongTaskTimer.Sample or null
Expand All @@ -216,9 +226,8 @@ public LongTaskTimer.Sample connected(LongTaskTimer.Sample sample, HttpRequestMe
config.getServerMatchPatterns(),
config.getServerIgnorePatterns());
if (path != null) {
return LongTaskTimer.builder(nameWebsocketConnections)
.tags(Tags.of(HttpCommonTags.uri(path, requestMetric.initialPath, 0)))
.register(registry)
return websocketConnectionTimer
.withTags(Tags.of(HttpCommonTags.uri(path, requestMetric.initialPath, 0)))
.start();
}
return null;
Expand Down

0 comments on commit 096a23e

Please sign in to comment.