Skip to content

Commit

Permalink
Upgrade to Promethus java client 1.x and adapt the code to the new ve…
Browse files Browse the repository at this point in the history
…rsion

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Nov 13, 2024
1 parent 5a4e3e1 commit c4a31c8
Show file tree
Hide file tree
Showing 23 changed files with 1,096 additions and 639 deletions.
8 changes: 3 additions & 5 deletions besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1034,8 +1034,7 @@ public Runner build() {
subscriptionManager, privacyParameters, context.getBlockchain().getGenesisBlockHeader());
}

final Optional<MetricsService> metricsService =
createMetricsService(vertx, metricsConfiguration);
final Optional<MetricsService> metricsService = createMetricsService(metricsConfiguration);

final Optional<EthStatsService> ethStatsService;
if (isEthStatsEnabled()) {
Expand Down Expand Up @@ -1469,9 +1468,8 @@ private WebSocketService createWebsocketService(
vertx, configuration, websocketMessageHandler, authenticationService, metricsSystem);
}

private Optional<MetricsService> createMetricsService(
final Vertx vertx, final MetricsConfiguration configuration) {
return MetricsService.create(vertx, configuration, metricsSystem);
private Optional<MetricsService> createMetricsService(final MetricsConfiguration configuration) {
return MetricsService.create(configuration, metricsSystem);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import java.util.function.Function;
import java.util.function.Supplier;

import io.vertx.core.Vertx;
import jakarta.validation.constraints.NotBlank;
import org.apache.tuweni.bytes.Bytes;
import org.slf4j.Logger;
Expand Down Expand Up @@ -458,8 +457,7 @@ private static Optional<MetricsService> initMetrics(final BlocksSubCommand paren
parentCommand.parentCommand.metricsConfiguration();

Optional<MetricsService> metricsService =
MetricsService.create(
Vertx.vertx(), metricsConfiguration, parentCommand.parentCommand.getMetricsSystem());
MetricsService.create(metricsConfiguration, parentCommand.parentCommand.getMetricsSystem());
metricsService.ifPresent(MetricsService::start);
return metricsService;
}
Expand Down
277 changes: 277 additions & 0 deletions gradle/verification-metadata.xml

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions metrics/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ dependencies {
implementation 'io.opentelemetry:opentelemetry-sdk-extension-autoconfigure'
implementation 'io.opentelemetry.semconv:opentelemetry-semconv'

implementation 'io.prometheus:simpleclient'
implementation 'io.prometheus:simpleclient_common'
implementation 'io.prometheus:simpleclient_guava'
implementation 'io.prometheus:simpleclient_hotspot'
implementation 'io.prometheus:simpleclient_pushgateway'
implementation 'io.prometheus:prometheus-metrics-core'
implementation 'io.prometheus:prometheus-metrics-instrumentation-guava'
implementation 'io.prometheus:prometheus-metrics-instrumentation-jvm'
implementation 'io.prometheus:prometheus-metrics-exporter-httpserver'
implementation 'io.prometheus:prometheus-metrics-exporter-pushgateway'

implementation 'io.vertx:vertx-core'
implementation 'io.vertx:vertx-web'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;
import org.hyperledger.besu.metrics.prometheus.MetricsHttpService;
import org.hyperledger.besu.metrics.prometheus.MetricsPushGatewayService;
import org.hyperledger.besu.metrics.prometheus.PrometheusMetricsSystem;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import io.vertx.core.Vertx;
import org.slf4j.LoggerFactory;

/**
Expand All @@ -35,20 +35,18 @@ public interface MetricsService {
/**
* Create Metrics Service.
*
* @param vertx the vertx
* @param configuration the configuration
* @param metricsSystem the metrics system
* @return the optional Metrics Service
*/
static Optional<MetricsService> create(
final Vertx vertx,
final MetricsConfiguration configuration,
final MetricsSystem metricsSystem) {
final MetricsConfiguration configuration, final MetricsSystem metricsSystem) {
LoggerFactory.getLogger(MetricsService.class)
.trace("Creating metrics service {}", configuration.getProtocol());
if (configuration.getProtocol() == MetricsProtocol.PROMETHEUS) {
if (configuration.isEnabled()) {
return Optional.of(new MetricsHttpService(vertx, configuration, metricsSystem));
return Optional.of(
new MetricsHttpService(configuration, (PrometheusMetricsSystem) metricsSystem));
} else if (configuration.isPushEnabled()) {
return Optional.of(new MetricsPushGatewayService(configuration, metricsSystem));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Besu Contributors
* Copyright contributors to Hyperledger Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.metrics.prometheus;

import org.hyperledger.besu.metrics.Observation;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;

import java.util.ArrayList;
import java.util.stream.Stream;

import io.prometheus.metrics.model.snapshots.SummarySnapshot;

abstract class AbstractPrometheusSummary extends CategorizedPrometheusCollector {

/**
* Create a new collector assigned to the given category and with the given name, and computed the
* prefixed name.
*
* @param category The {@link MetricCategory} this collector is assigned to
* @param name The name of this collector
*/
protected AbstractPrometheusSummary(final MetricCategory category, final String name) {
super(category, name);
}

protected abstract SummarySnapshot collect();

@Override
public Stream<Observation> streamObservations() {
return collect().getDataPoints().stream()
.flatMap(
dataPoint -> {
final var labelValues = PrometheusCollector.getLabelValues(dataPoint.getLabels());
final var quantiles = dataPoint.getQuantiles();
final var observations = new ArrayList<Observation>(quantiles.size() + 2);

if (dataPoint.hasSum()) {
observations.add(
new Observation(
category,
name,
dataPoint.getSum(),
PrometheusCollector.addLabelValues(labelValues, "sum")));
}

if (dataPoint.hasCount()) {
observations.add(
new Observation(
category,
name,
dataPoint.getCount(),
PrometheusCollector.addLabelValues(labelValues, "count")));
}

quantiles.forEach(
quantile ->
observations.add(
new Observation(
category,
name,
quantile.getValue(),
PrometheusCollector.addLabelValues(
labelValues,
"quantile",
Double.toString(quantile.getQuantile())))));

return observations.stream();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.metrics.prometheus;

import org.hyperledger.besu.plugin.services.metrics.MetricCategory;

/** A Prometheus collector that is assigned to a category */
public abstract class CategorizedPrometheusCollector implements PrometheusCollector {
/** The {@link MetricCategory} this collector is assigned to */
protected final MetricCategory category;

/** The name of this collector */
protected final String name;

/** The prefixed name of this collector */
protected final String prefixedName;

/**
* Create a new collector assigned to the given category and with the given name, and computed the
* prefixed name.
*
* @param category The {@link MetricCategory} this collector is assigned to
* @param name The name of this collector
*/
protected CategorizedPrometheusCollector(final MetricCategory category, final String name) {
this.category = category;
this.name = name;
this.prefixedName = prefixedName(category, name);
}

private static String categoryPrefix(final MetricCategory category) {
return category.getApplicationPrefix().orElse("") + category.getName() + "_";
}

private static String prefixedName(final MetricCategory category, final String name) {
return categoryPrefix(category) + name;
}
}

This file was deleted.

Loading

0 comments on commit c4a31c8

Please sign in to comment.