Skip to content

Commit

Permalink
Merge branch 'main' into 7311-add-GetReceiptsFromPeerTask
Browse files Browse the repository at this point in the history
  • Loading branch information
Matilda-Clerke authored Oct 30, 2024
2 parents 24d8f99 + f9f721c commit 8e72f70
Show file tree
Hide file tree
Showing 16 changed files with 537 additions and 183 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
- Create and publish Besu BOM (Bill of Materials) [#7615](https://github.com/hyperledger/besu/pull/7615)
- Update Java dependencies [#7786](https://github.com/hyperledger/besu/pull/7786)
- Add a method to get all the transaction in the pool, to the `TransactionPoolService`, to easily access the transaction pool content from plugins [#7813](https://github.com/hyperledger/besu/pull/7813)
- Add a method to check if a metric category is enabled to the plugin API [#7832](https://github.com/hyperledger/besu/pull/7832)

### Bug fixes
- Fix registering new metric categories from plugins [#7825](https://github.com/hyperledger/besu/pull/7825)

## 24.10.0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.tests.acceptance.plugins;

import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.MetricCategoryRegistry;

import java.util.Locale;
import java.util.Optional;

import com.google.auto.service.AutoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@AutoService(BesuPlugin.class)
public class TestMetricsPlugin implements BesuPlugin {
private static final Logger LOG = LoggerFactory.getLogger(TestMetricsPlugin.class);
private BesuContext besuContext;

@Override
public void register(final BesuContext context) {
LOG.info("Registering TestMetricsPlugin");
besuContext = context;
context
.getService(MetricCategoryRegistry.class)
.orElseThrow()
.addMetricCategory(TestMetricCategory.TEST_METRIC_CATEGORY);
}

@Override
public void start() {
LOG.info("Starting TestMetricsPlugin");
besuContext
.getService(MetricsSystem.class)
.orElseThrow()
.createGauge(
TestMetricCategory.TEST_METRIC_CATEGORY,
"test_metric",
"Returns 1 on succes",
() -> 1.0);
}

@Override
public void stop() {
LOG.info("Stopping TestMetricsPlugin");
}

public enum TestMetricCategory implements MetricCategory {
TEST_METRIC_CATEGORY;

@Override
public String getName() {
return name().toLowerCase(Locale.ROOT);
}

@Override
public Optional<String> getApplicationPrefix() {
return Optional.of("plugin_test_");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.tests.acceptance.plugins;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.tests.acceptance.plugins.TestMetricsPlugin.TestMetricCategory.TEST_METRIC_CATEGORY;

import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;
import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase;
import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode;
import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.BesuNodeConfigurationBuilder;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class MetricsPluginTest extends AcceptanceTestBase {
private BesuNode node;
private MetricsConfiguration metricsConfiguration;

@BeforeEach
public void setUp() throws Exception {
metricsConfiguration =
MetricsConfiguration.builder()
.enabled(true)
.port(0)
.metricCategories(Set.of(TEST_METRIC_CATEGORY))
.build();
node =
besu.create(
new BesuNodeConfigurationBuilder()
.name("node1")
.plugins(List.of("testPlugins"))
.metricsConfiguration(metricsConfiguration)
.build());

cluster.start(node);
}

@Test
public void metricCategoryAdded() throws IOException, InterruptedException {
final var httpClient = HttpClient.newHttpClient();
final var req = HttpRequest.newBuilder(URI.create(node.metricsHttpUrl().get())).build();
final var resp = httpClient.send(req, HttpResponse.BodyHandlers.ofLines());
assertThat(resp.statusCode()).isEqualTo(200);
final var foundMetric =
resp.body()
.filter(
line -> line.startsWith(TEST_METRIC_CATEGORY.getApplicationPrefix().orElseThrow()))
.findFirst()
.orElseThrow();
assertThat(foundMetric).endsWith("1.0");
}
}
22 changes: 10 additions & 12 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.hyperledger.besu.cli.config.EthNetworkConfig;
import org.hyperledger.besu.cli.config.NetworkName;
import org.hyperledger.besu.cli.config.ProfilesCompletionCandidates;
import org.hyperledger.besu.cli.converter.MetricCategoryConverter;
import org.hyperledger.besu.cli.custom.JsonRPCAllowlistHostsProperty;
import org.hyperledger.besu.cli.error.BesuExecutionExceptionHandler;
import org.hyperledger.besu.cli.error.BesuParameterExceptionHandler;
Expand Down Expand Up @@ -170,7 +169,6 @@
import org.hyperledger.besu.plugin.services.TransactionSelectionService;
import org.hyperledger.besu.plugin.services.TransactionSimulationService;
import org.hyperledger.besu.plugin.services.exception.StorageException;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.MetricCategoryRegistry;
import org.hyperledger.besu.plugin.services.p2p.P2PService;
import org.hyperledger.besu.plugin.services.rlp.RlpConverterService;
Expand Down Expand Up @@ -332,7 +330,6 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
private final Map<String, String> environment;
private final MetricCategoryRegistryImpl metricCategoryRegistry =
new MetricCategoryRegistryImpl();
private final MetricCategoryConverter metricCategoryConverter = new MetricCategoryConverter();

private final PreSynchronizationTaskRunner preSynchronizationTaskRunner =
new PreSynchronizationTaskRunner();
Expand Down Expand Up @@ -1136,10 +1133,6 @@ private void registerConverters() {
commandLine.registerConverter(Hash.class, Hash::fromHexString);
commandLine.registerConverter(Optional.class, Optional::of);
commandLine.registerConverter(Double.class, Double::parseDouble);

metricCategoryConverter.addCategories(BesuMetricCategory.class);
metricCategoryConverter.addCategories(StandardMetricCategory.class);
commandLine.registerConverter(MetricCategory.class, metricCategoryConverter);
}

private void handleStableOptions() {
Expand Down Expand Up @@ -1174,6 +1167,9 @@ private void preparePlugins() {
besuPluginContext.addService(PicoCLIOptions.class, new PicoCLIOptionsImpl(commandLine));
besuPluginContext.addService(SecurityModuleService.class, securityModuleService);
besuPluginContext.addService(StorageService.class, storageService);

metricCategoryRegistry.addCategories(BesuMetricCategory.class);
metricCategoryRegistry.addCategories(StandardMetricCategory.class);
besuPluginContext.addService(MetricCategoryRegistry.class, metricCategoryRegistry);
besuPluginContext.addService(PermissioningService.class, permissioningService);
besuPluginContext.addService(PrivacyPluginService.class, privacyPluginService);
Expand All @@ -1191,10 +1187,6 @@ private void preparePlugins() {
rocksDBPlugin.register(besuPluginContext);
new InMemoryStoragePlugin().register(besuPluginContext);

metricCategoryRegistry
.getMetricCategories()
.forEach(metricCategoryConverter::addRegistryCategory);

// register default security module
securityModuleService.register(
DEFAULT_SECURITY_MODULE, Suppliers.memoize(this::defaultSecurityModule));
Expand Down Expand Up @@ -1891,6 +1883,10 @@ public MetricsConfiguration metricsConfiguration() {
"--metrics-push-interval",
"--metrics-push-prometheus-job"));

metricsOptions.setMetricCategoryRegistry(metricCategoryRegistry);

metricsOptions.validate(commandLine);

final MetricsConfiguration.Builder metricsConfigurationBuilder =
metricsOptions.toDomainObject();
metricsConfigurationBuilder
Expand All @@ -1903,7 +1899,9 @@ public MetricsConfiguration metricsConfiguration() {
? p2PDiscoveryOptions.autoDiscoverDefaultIP().getHostAddress()
: metricsOptions.getMetricsPushHost())
.hostsAllowlist(hostsAllowlist);
return metricsConfigurationBuilder.build();
final var metricsConfiguration = metricsConfigurationBuilder.build();
metricCategoryRegistry.setMetricsConfiguration(metricsConfiguration);
return metricsConfiguration;
}

private PrivacyParameters privacyParameters() {
Expand Down

This file was deleted.

Loading

0 comments on commit 8e72f70

Please sign in to comment.