Skip to content

Commit

Permalink
Backport of vert-x3#143 to 4.2 branch
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
  • Loading branch information
tsegismont committed Jun 10, 2022
1 parent 02be2b4 commit 0b1b350
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 98 deletions.
18 changes: 12 additions & 6 deletions src/main/java/io/vertx/micrometer/impl/AbstractMetrics.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 Red Hat, Inc.
* Copyright 2022 Red Hat, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -16,6 +16,7 @@

package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.MetricsDomain;
Expand All @@ -24,6 +25,7 @@
import io.vertx.micrometer.impl.meters.Summaries;
import io.vertx.micrometer.impl.meters.Timers;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.LongAdder;

Expand All @@ -35,20 +37,24 @@
public abstract class AbstractMetrics implements MicrometerMetrics {
protected final MeterRegistry registry;
protected final String category;
protected final ConcurrentMap<Meter.Id, Object> gaugesTable;

AbstractMetrics(MeterRegistry registry) {
AbstractMetrics(MeterRegistry registry, ConcurrentMap<Meter.Id, Object> gaugesTable) {
this.registry = registry;
this.gaugesTable = gaugesTable;
this.category = null;
}

AbstractMetrics(MeterRegistry registry, String category) {
AbstractMetrics(MeterRegistry registry, String category, ConcurrentMap<Meter.Id, Object> gaugesTable) {
this.registry = registry;
this.category = category;
this.gaugesTable = gaugesTable;
}

AbstractMetrics(MeterRegistry registry, MetricsDomain domain) {
AbstractMetrics(MeterRegistry registry, MetricsDomain domain, ConcurrentMap<Meter.Id, Object> gaugesTable) {
this.registry = registry;
this.category = (domain == null) ? null : domain.toCategory();
this.gaugesTable = gaugesTable;
}

/**
Expand All @@ -69,11 +75,11 @@ Counters counters(String name, String description, Label... keys) {
}

Gauges<LongAdder> longGauges(String name, String description, Label... keys) {
return new Gauges<>(baseName() + name, description, LongAdder::new, LongAdder::doubleValue, registry, keys);
return new Gauges<>(gaugesTable, baseName() + name, description, LongAdder::new, LongAdder::doubleValue, registry, keys);
}

Gauges<AtomicReference<Double>> doubleGauges(String name, String description, Label... keys) {
return new Gauges<>(baseName() + name, description, () -> new AtomicReference<>(0d), AtomicReference::get, registry, keys);
return new Gauges<>(gaugesTable, baseName() + name, description, () -> new AtomicReference<>(0d), AtomicReference::get, registry, keys);
}

Summaries summaries(String name, String description, Label... keys) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2017 The original author or authors
* Copyright (c) 2011-2022 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -16,6 +16,7 @@

package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.ClientMetrics;
Expand All @@ -25,6 +26,7 @@
import io.vertx.micrometer.impl.meters.Gauges;
import io.vertx.micrometer.impl.meters.Timers;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.LongAdder;

/**
Expand All @@ -37,8 +39,8 @@ class VertxClientMetrics extends AbstractMetrics {
private final Gauges<LongAdder> processingPending;
private final Counters resetCount;

VertxClientMetrics(MeterRegistry registry, String type, MetricsNaming names) {
super(registry, type);
VertxClientMetrics(MeterRegistry registry, String type, MetricsNaming names, ConcurrentMap<Meter.Id, Object> gaugesTable) {
super(registry, type, gaugesTable);
queueDelay = timers(names.getClientQueueTime(), "Time spent in queue before being processed", Label.REMOTE, Label.NAMESPACE);
queueSize = longGauges(names.getClientQueuePending(), "Number of pending elements in queue", Label.REMOTE, Label.NAMESPACE);
processingTime = timers(names.getClientProcessingTime(), "Processing time, from request start to response end", Label.REMOTE, Label.NAMESPACE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Red Hat, Inc. and/or its affiliates
* Copyright 2022 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,6 +16,7 @@
*/
package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.DatagramSocketMetrics;
Expand All @@ -25,6 +26,8 @@
import io.vertx.micrometer.impl.meters.Counters;
import io.vertx.micrometer.impl.meters.Summaries;

import java.util.concurrent.ConcurrentMap;

/**
* @author Joel Takvorian
*/
Expand All @@ -35,8 +38,8 @@ class VertxDatagramSocketMetrics extends AbstractMetrics implements DatagramSock

private volatile String localAddress;

VertxDatagramSocketMetrics(MeterRegistry registry, MetricsNaming names) {
super(registry, MetricsDomain.DATAGRAM_SOCKET);
VertxDatagramSocketMetrics(MeterRegistry registry, MetricsNaming names, ConcurrentMap<Meter.Id, Object> gaugesTable) {
super(registry, MetricsDomain.DATAGRAM_SOCKET, gaugesTable);
bytesReceived = summaries(names.getDatagramBytesRead(), "Total number of datagram bytes received", Label.LOCAL);
bytesSent = summaries(names.getDatagramBytesWritten(), "Total number of datagram bytes sent");
errorCount = counters(names.getDatagramErrorCount(), "Total number of datagram errors", Label.CLASS_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2017 The original author or authors
* Copyright (c) 2011-2022 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -15,6 +15,7 @@
*/
package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.core.eventbus.Message;
import io.vertx.core.eventbus.ReplyFailure;
Expand All @@ -26,6 +27,7 @@
import io.vertx.micrometer.impl.meters.Gauges;
import io.vertx.micrometer.impl.meters.Summaries;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.LongAdder;

/**
Expand All @@ -46,8 +48,8 @@ class VertxEventBusMetrics extends AbstractMetrics implements EventBusMetrics<Ve
private final Summaries bytesRead;
private final Summaries bytesWritten;

VertxEventBusMetrics(MeterRegistry registry, MetricsNaming names) {
super(registry, MetricsDomain.EVENT_BUS);
VertxEventBusMetrics(MeterRegistry registry, MetricsNaming names, ConcurrentMap<Meter.Id, Object> gaugesTable) {
super(registry, MetricsDomain.EVENT_BUS, gaugesTable);
handlers = longGauges(names.getEbHandlers(), "Number of event bus handlers in use", Label.EB_ADDRESS);
pending = longGauges(names.getEbPending(), "Number of messages not processed yet", Label.EB_ADDRESS, Label.EB_SIDE);
processed = counters(names.getEbProcessed(), "Number of processed messages", Label.EB_ADDRESS, Label.EB_SIDE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2017 The original author or authors
* Copyright (c) 2011-2022 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -16,6 +16,7 @@

package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.core.http.WebSocket;
import io.vertx.core.net.SocketAddress;
Expand All @@ -31,6 +32,7 @@
import io.vertx.micrometer.impl.meters.Summaries;
import io.vertx.micrometer.impl.meters.Timers;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.LongAdder;

/**
Expand All @@ -47,8 +49,8 @@ class VertxHttpClientMetrics extends VertxNetClientMetrics {
private final Summaries responseBytes;
private final Gauges<LongAdder> wsConnections;

VertxHttpClientMetrics(MeterRegistry registry, MetricsNaming names) {
super(registry, MetricsDomain.HTTP_CLIENT, names);
VertxHttpClientMetrics(MeterRegistry registry, MetricsNaming names, ConcurrentMap<Meter.Id, Object> gaugesTable) {
super(registry, MetricsDomain.HTTP_CLIENT, names, gaugesTable);
queueDelay = timers(names.getHttpQueueTime(), "Time spent in queue before being processed", Label.LOCAL, Label.REMOTE);
queueSize = longGauges(names.getHttpQueuePending(), "Number of pending elements in queue", Label.LOCAL, Label.REMOTE);
requests = longGauges(names.getHttpActiveRequests(), "Number of requests waiting for a response", Label.LOCAL, Label.REMOTE, Label.HTTP_PATH, Label.HTTP_METHOD);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2017 The original author or authors
* Copyright (c) 2011-2022 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -15,6 +15,7 @@
*/
package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.vertx.core.http.HttpMethod;
Expand All @@ -34,6 +35,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Function;

Expand All @@ -50,8 +52,8 @@ class VertxHttpServerMetrics extends VertxNetServerMetrics {
private final Gauges<LongAdder> wsConnections;
private final Function<HttpRequest, Iterable<Tag>> customTagsProvider;

VertxHttpServerMetrics(MeterRegistry registry, MetricsNaming names, Function<HttpRequest, Iterable<Tag>> customTagsProvider) {
super(registry, MetricsDomain.HTTP_SERVER, names);
VertxHttpServerMetrics(MeterRegistry registry, MetricsNaming names, Function<HttpRequest, Iterable<Tag>> customTagsProvider, ConcurrentMap<Meter.Id, Object> gaugesTable) {
super(registry, MetricsDomain.HTTP_SERVER, names, gaugesTable);
this.customTagsProvider = customTagsProvider;
requests = longGauges(names.getHttpActiveRequests(), "Number of requests being processed", Label.LOCAL, Label.REMOTE, Label.HTTP_PATH, Label.HTTP_METHOD);
requestCount = counters(names.getHttpRequestsCount(), "Number of processed requests", Label.LOCAL, Label.REMOTE, Label.HTTP_ROUTE, Label.HTTP_PATH, Label.HTTP_METHOD, Label.HTTP_CODE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2017 The original author or authors
* Copyright (c) 2011-2022 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -15,6 +15,8 @@
*/
package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
Expand All @@ -29,11 +31,17 @@
import io.vertx.micrometer.backends.BackendRegistries;
import io.vertx.micrometer.backends.BackendRegistry;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Joel Takvorian
*/
public class VertxMetricsFactoryImpl implements VertxMetricsFactory {

private static final Map<MeterRegistry, ConcurrentHashMap<Meter.Id, Object>> tables = new WeakHashMap<>(1);

@Override
public VertxMetrics metrics(VertxOptions vertxOptions) {
MetricsOptions metricsOptions = vertxOptions.getMetricsOptions();
Expand All @@ -44,7 +52,11 @@ public VertxMetrics metrics(VertxOptions vertxOptions) {
options = new MicrometerMetricsOptions(metricsOptions.toJson());
}
BackendRegistry backendRegistry = BackendRegistries.setupBackend(options);
VertxMetricsImpl metrics = new VertxMetricsImpl(options, backendRegistry);
ConcurrentHashMap<Meter.Id, Object> gaugesTable;
synchronized (tables) {
gaugesTable = tables.computeIfAbsent(backendRegistry.getMeterRegistry(), meterRegistry -> new ConcurrentHashMap<>());
}
VertxMetricsImpl metrics = new VertxMetricsImpl(options, backendRegistry, gaugesTable);
metrics.init();

if (options.isJvmMetricsEnabled()) {
Expand Down
44 changes: 15 additions & 29 deletions src/main/java/io/vertx/micrometer/impl/VertxMetricsImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2017 The original author or authors
* Copyright (c) 2011-2022 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -16,6 +16,7 @@

package io.vertx.micrometer.impl;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.core.datagram.DatagramSocketOptions;
import io.vertx.core.http.HttpClientOptions;
Expand All @@ -24,14 +25,7 @@
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetServerOptions;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.spi.metrics.DatagramSocketMetrics;
import io.vertx.core.spi.metrics.EventBusMetrics;
import io.vertx.core.spi.metrics.HttpClientMetrics;
import io.vertx.core.spi.metrics.HttpServerMetrics;
import io.vertx.core.spi.metrics.PoolMetrics;
import io.vertx.core.spi.metrics.TCPMetrics;
import io.vertx.core.spi.metrics.VertxMetrics;
import io.vertx.core.spi.metrics.*;
import io.vertx.micrometer.MetricsNaming;
import io.vertx.micrometer.MicrometerMetricsOptions;
import io.vertx.micrometer.backends.BackendRegistries;
Expand All @@ -41,14 +35,9 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static io.vertx.micrometer.MetricsDomain.DATAGRAM_SOCKET;
import static io.vertx.micrometer.MetricsDomain.EVENT_BUS;
import static io.vertx.micrometer.MetricsDomain.HTTP_CLIENT;
import static io.vertx.micrometer.MetricsDomain.HTTP_SERVER;
import static io.vertx.micrometer.MetricsDomain.NAMED_POOLS;
import static io.vertx.micrometer.MetricsDomain.NET_CLIENT;
import static io.vertx.micrometer.MetricsDomain.NET_SERVER;
import static io.vertx.micrometer.MetricsDomain.*;

/**
* Metrics SPI implementation for Micrometer.
Expand All @@ -69,11 +58,8 @@ public class VertxMetricsImpl extends AbstractMetrics implements VertxMetrics {
private final Map<String, VertxClientMetrics> mapClientMetrics = new ConcurrentHashMap<>();
private final Set<String> disabledCaterogies = new HashSet<>();

/**
* @param options Vertx Prometheus options
*/
public VertxMetricsImpl(MicrometerMetricsOptions options, BackendRegistry backendRegistry) {
super(backendRegistry.getMeterRegistry());
public VertxMetricsImpl(MicrometerMetricsOptions options, BackendRegistry backendRegistry, ConcurrentMap<Meter.Id, Object> gaugesTable) {
super(backendRegistry.getMeterRegistry(), gaugesTable);
this.backendRegistry = backendRegistry;
registryName = options.getRegistryName();
MeterRegistry registry = backendRegistry.getMeterRegistry();
Expand All @@ -83,19 +69,19 @@ public VertxMetricsImpl(MicrometerMetricsOptions options, BackendRegistry backen
names = options.getMetricsNaming();

eventBusMetrics = options.isMetricsCategoryDisabled(EVENT_BUS) ? null
: new VertxEventBusMetrics(registry, names);
: new VertxEventBusMetrics(registry, names, gaugesTable);
datagramSocketMetrics = options.isMetricsCategoryDisabled(DATAGRAM_SOCKET) ? null
: new VertxDatagramSocketMetrics(registry, names);
: new VertxDatagramSocketMetrics(registry, names, gaugesTable);
netClientMetrics = options.isMetricsCategoryDisabled(NET_CLIENT) ? null
: new VertxNetClientMetrics(registry, names);
: new VertxNetClientMetrics(registry, names, gaugesTable);
netServerMetrics = options.isMetricsCategoryDisabled(NET_SERVER) ? null
: new VertxNetServerMetrics(registry, names);
: new VertxNetServerMetrics(registry, names, gaugesTable);
httpClientMetrics = options.isMetricsCategoryDisabled(HTTP_CLIENT) ? null
: new VertxHttpClientMetrics(registry, names);
: new VertxHttpClientMetrics(registry, names, gaugesTable);
httpServerMetrics = options.isMetricsCategoryDisabled(HTTP_SERVER) ? null
: new VertxHttpServerMetrics(registry, names, options.getRequestsTagsProvider());
: new VertxHttpServerMetrics(registry, names, options.getRequestsTagsProvider(), gaugesTable);
poolMetrics = options.isMetricsCategoryDisabled(NAMED_POOLS) ? null
: new VertxPoolMetrics(registry, names);
: new VertxPoolMetrics(registry, names, gaugesTable);
}

void init() {
Expand Down Expand Up @@ -163,7 +149,7 @@ public PoolMetrics<?> createPoolMetrics(String poolType, String poolName, int ma
if (disabledCaterogies.contains(type)) {
return DummyVertxMetrics.DummyClientMetrics.INSTANCE;
}
VertxClientMetrics clientMetrics = mapClientMetrics.computeIfAbsent(type, t -> new VertxClientMetrics(registry, type, names));
VertxClientMetrics clientMetrics = mapClientMetrics.computeIfAbsent(type, t -> new VertxClientMetrics(registry, type, names, gaugesTable));
return clientMetrics.forInstance(remoteAddress, namespace);
}

Expand Down
Loading

0 comments on commit 0b1b350

Please sign in to comment.