From 25ab21128eefa31eb6338a9ec98b042ab0c0cfb4 Mon Sep 17 00:00:00 2001 From: Justin Florentine Date: Thu, 20 Apr 2023 10:37:39 -0400 Subject: [PATCH] Metrics via dagger (#5244) * dagger component * pushed up MetricsSystemModule * passes around the BesuComponent as an interim application context till more things are managed by Dagger --------- Signed-off-by: Justin Florentine --- acceptance-tests/dsl/build.gradle | 2 + besu/build.gradle | 2 + .../main/java/org/hyperledger/besu/Besu.java | 37 +++++----- .../org/hyperledger/besu/cli/BesuCommand.java | 37 +++++++--- .../besu/components/BesuCommandModule.java | 71 +++++++++++++++++++ .../besu/components/BesuComponent.java | 69 ++++++++++++++++++ .../controller/BesuControllerBuilder.java | 19 ++++- .../besu/cli/CommandTestAbstract.java | 22 +++--- ethereum/core/build.gradle | 2 + .../cache/CachedMerkleTrieLoaderModule.java | 31 ++++++++ .../besu/evmtool/EvmToolCommand.java | 1 + .../besu/evmtool/EvmToolComponent.java | 5 +- metrics/core/build.gradle | 4 ++ .../metrics/MetricsConfigurationModule.java | 16 ++--- .../besu/metrics/MetricsSystemModule.java | 45 ++++++++++++ .../prometheus/MetricsConfiguration.java | 2 +- 16 files changed, 312 insertions(+), 53 deletions(-) create mode 100644 besu/src/main/java/org/hyperledger/besu/components/BesuCommandModule.java create mode 100644 besu/src/main/java/org/hyperledger/besu/components/BesuComponent.java create mode 100644 ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedMerkleTrieLoaderModule.java rename ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/MetricsSystemModule.java => metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsConfigurationModule.java (67%) create mode 100644 metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsSystemModule.java diff --git a/acceptance-tests/dsl/build.gradle b/acceptance-tests/dsl/build.gradle index c619616caa..d65de08b95 100644 --- a/acceptance-tests/dsl/build.gradle +++ b/acceptance-tests/dsl/build.gradle @@ -28,6 +28,8 @@ dependencies { implementation 'com.github.tomakehurst:wiremock-jre8' implementation 'com.google.guava:guava' + implementation 'com.google.dagger:dagger' + annotationProcessor 'com.google.dagger:dagger-compiler' implementation 'com.squareup.okhttp3:okhttp' implementation 'info.picocli:picocli' implementation 'io.reactivex.rxjava2:rxjava' diff --git a/besu/build.gradle b/besu/build.gradle index eed30336db..969571b565 100644 --- a/besu/build.gradle +++ b/besu/build.gradle @@ -63,6 +63,7 @@ dependencies { implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8' implementation 'com.github.oshi:oshi-core' implementation 'com.google.guava:guava' + implementation 'com.google.dagger:dagger' implementation 'com.graphql-java:graphql-java' implementation 'info.picocli:picocli' implementation 'io.vertx:vertx-core' @@ -102,4 +103,5 @@ dependencies { testImplementation 'tech.pegasys.discovery:discovery' testRuntimeOnly 'org.junit.vintage:junit-vintage-engine' + annotationProcessor 'com.google.dagger:dagger-compiler' } diff --git a/besu/src/main/java/org/hyperledger/besu/Besu.java b/besu/src/main/java/org/hyperledger/besu/Besu.java index 35682cd8ae..6bdd87d0cf 100644 --- a/besu/src/main/java/org/hyperledger/besu/Besu.java +++ b/besu/src/main/java/org/hyperledger/besu/Besu.java @@ -1,5 +1,5 @@ /* - * Copyright ConsenSys AG. + * Copyright Hyperledger Besu Contributors. * * 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 @@ -14,13 +14,9 @@ */ package org.hyperledger.besu; -import org.hyperledger.besu.chainexport.RlpBlockExporter; -import org.hyperledger.besu.chainimport.JsonBlockImporter; -import org.hyperledger.besu.chainimport.RlpBlockImporter; import org.hyperledger.besu.cli.BesuCommand; import org.hyperledger.besu.cli.logging.BesuLoggingConfigurationFactory; -import org.hyperledger.besu.controller.BesuController; -import org.hyperledger.besu.services.BesuPluginContextImpl; +import org.hyperledger.besu.components.DaggerBesuComponent; import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.Log4J2LoggerFactory; @@ -37,19 +33,8 @@ public final class Besu { * @param args command line arguments. */ public static void main(final String... args) { - final Logger logger = setupLogging(); - - final BesuCommand besuCommand = - new BesuCommand( - logger, - RlpBlockImporter::new, - JsonBlockImporter::new, - RlpBlockExporter::new, - new RunnerBuilder(), - new BesuController.Builder(), - new BesuPluginContextImpl(), - System.getenv()); - + setupLogging(); + final BesuCommand besuCommand = DaggerBesuComponent.create().getBesuCommand(); int exitCode = besuCommand.parse( new RunLast(), @@ -61,7 +46,11 @@ public final class Besu { System.exit(exitCode); } - private static Logger setupLogging() { + /** + * a Logger setup for handling any exceptions during the bootstrap process, to indicate to users + * their CLI configuration had problems. + */ + public static void setupLogging() { try { InternalLoggerFactory.setDefaultFactory(Log4J2LoggerFactory.INSTANCE); } catch (Throwable t) { @@ -81,6 +70,14 @@ public final class Besu { "Could not set logging system property: %s - %s%n", t.getClass().getSimpleName(), t.getMessage()); } + } + + /** + * Returns the first logger to be created. This is used to set the default uncaught exception + * + * @return Logger + */ + public static Logger getFirstLogger() { final Logger logger = LoggerFactory.getLogger(Besu.class); Thread.setDefaultUncaughtExceptionHandler(slf4jExceptionHandler(logger)); Thread.currentThread().setUncaughtExceptionHandler(slf4jExceptionHandler(logger)); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 1fb5b73a06..0e8fbb09bc 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -86,6 +86,7 @@ import org.hyperledger.besu.cli.util.BesuCommandCustomFactory; import org.hyperledger.besu.cli.util.CommandLineUtils; import org.hyperledger.besu.cli.util.ConfigOptionSearchAndRunHandler; import org.hyperledger.besu.cli.util.VersionProvider; +import org.hyperledger.besu.components.BesuComponent; import org.hyperledger.besu.config.CheckpointConfigOptions; import org.hyperledger.besu.config.GenesisConfigFile; import org.hyperledger.besu.config.GenesisConfigOptions; @@ -1323,8 +1324,15 @@ public class BesuCommand implements DefaultCommandValues, Runnable { private Collection staticNodes; private BesuController besuController; private BesuConfiguration pluginCommonConfiguration; + + private BesuComponent besuComponent; private final Supplier metricsSystem = - Suppliers.memoize(() -> MetricsSystemFactory.create(metricsConfiguration())); + Suppliers.memoize( + () -> { + return besuComponent == null + ? MetricsSystemFactory.create(metricsConfiguration()) + : besuComponent.getObservableMetricsSystem(); + }); private Vertx vertx; private EnodeDnsConfiguration enodeDnsConfiguration; private KeyValueStorageProvider keyValueStorageProvider; @@ -1334,7 +1342,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable { /** * Besu command constructor. * - * @param logger Logger instance + * @param besuComponent BesuComponent which acts as our application context * @param rlpBlockImporter RlpBlockImporter supplier * @param jsonBlockImporterFactory instance of {@code Function} * @param rlpBlockExporterFactory instance of {@code Function} @@ -1344,7 +1352,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable { * @param environment Environment variables map */ public BesuCommand( - final Logger logger, + final BesuComponent besuComponent, final Supplier rlpBlockImporter, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -1353,7 +1361,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable { final BesuPluginContextImpl besuPluginContext, final Map environment) { this( - logger, + besuComponent, rlpBlockImporter, jsonBlockImporterFactory, rlpBlockExporterFactory, @@ -1372,7 +1380,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable { /** * Overloaded Besu command constructor visible for testing. * - * @param logger Logger instance + * @param besuComponent BesuComponent which acts as our application context * @param rlpBlockImporter RlpBlockImporter supplier * @param jsonBlockImporterFactory instance of {@code Function} * @param rlpBlockExporterFactory instance of {@code Function} @@ -1389,7 +1397,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable { */ @VisibleForTesting protected BesuCommand( - final Logger logger, + final BesuComponent besuComponent, final Supplier rlpBlockImporter, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -1403,7 +1411,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable { final PrivacyPluginServiceImpl privacyPluginService, final PkiBlockCreationConfigurationProvider pkiBlockCreationConfigProvider, final RpcEndpointServiceImpl rpcEndpointServiceImpl) { - this.logger = logger; + this.logger = besuComponent.getBesuCommandLogger(); this.rlpBlockImporter = rlpBlockImporter; this.rlpBlockExporterFactory = rlpBlockExporterFactory; this.jsonBlockImporterFactory = jsonBlockImporterFactory; @@ -1438,9 +1446,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable { final InputStream in, final String... args) { - commandLine = - new CommandLine(this, new BesuCommandCustomFactory(besuPluginContext)) - .setCaseInsensitiveEnumValuesAllowed(true); + toCommandLine(); handleStableOptions(); addSubCommands(in); @@ -1454,6 +1460,13 @@ public class BesuCommand implements DefaultCommandValues, Runnable { return exitCode; } + /** Used by Dagger to parse all options into a commandline instance. */ + public void toCommandLine() { + commandLine = + new CommandLine(this, new BesuCommandCustomFactory(besuPluginContext)) + .setCaseInsensitiveEnumValuesAllowed(true); + } + @Override public void run() { if (network != null && network.isDeprecated()) { @@ -2206,7 +2219,9 @@ public class BesuCommand implements DefaultCommandValues, Runnable { */ public BesuController buildController() { try { - return getControllerBuilder().build(); + return this.besuComponent == null + ? getControllerBuilder().build() + : getControllerBuilder().besuComponent(this.besuComponent).build(); } catch (final Exception e) { throw new ExecutionException(this.commandLine, e.getMessage(), e); } diff --git a/besu/src/main/java/org/hyperledger/besu/components/BesuCommandModule.java b/besu/src/main/java/org/hyperledger/besu/components/BesuCommandModule.java new file mode 100644 index 0000000000..cb9882238a --- /dev/null +++ b/besu/src/main/java/org/hyperledger/besu/components/BesuCommandModule.java @@ -0,0 +1,71 @@ +/* + * Copyright Hyperledger Besu Contributors. + * + * 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.components; + +import org.hyperledger.besu.Besu; +import org.hyperledger.besu.RunnerBuilder; +import org.hyperledger.besu.chainexport.RlpBlockExporter; +import org.hyperledger.besu.chainimport.JsonBlockImporter; +import org.hyperledger.besu.chainimport.RlpBlockImporter; +import org.hyperledger.besu.cli.BesuCommand; +import org.hyperledger.besu.controller.BesuController; +import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; +import org.hyperledger.besu.services.BesuPluginContextImpl; + +import javax.inject.Named; +import javax.inject.Singleton; + +import dagger.Module; +import dagger.Provides; +import org.slf4j.Logger; + +/** + * A dagger module that know how to create the BesuCommand, which collects all configuration + * settings. + */ +@Module +public class BesuCommandModule { + + @Provides + @Singleton + BesuCommand provideBesuCommand(final BesuComponent besuComponent) { + final BesuCommand besuCommand = + new BesuCommand( + besuComponent, + RlpBlockImporter::new, + JsonBlockImporter::new, + RlpBlockExporter::new, + new RunnerBuilder(), + new BesuController.Builder(), + new BesuPluginContextImpl(), + System.getenv()); + besuCommand.toCommandLine(); + return besuCommand; + } + + @Provides + @Singleton + MetricsConfiguration provideMetricsConfiguration(final BesuCommand provideFrom) { + return provideFrom.metricsConfiguration(); + } + + @Provides + @Named("besuCommandLogger") + @Singleton + Logger provideBesuCommandLogger() { + return Besu.getFirstLogger(); + } +} diff --git a/besu/src/main/java/org/hyperledger/besu/components/BesuComponent.java b/besu/src/main/java/org/hyperledger/besu/components/BesuComponent.java new file mode 100644 index 0000000000..ab188399db --- /dev/null +++ b/besu/src/main/java/org/hyperledger/besu/components/BesuComponent.java @@ -0,0 +1,69 @@ +/* + * Copyright Hyperledger Besu Contributors. + * + * 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.components; + +import org.hyperledger.besu.cli.BesuCommand; +import org.hyperledger.besu.ethereum.bonsai.cache.CachedMerkleTrieLoader; +import org.hyperledger.besu.ethereum.bonsai.cache.CachedMerkleTrieLoaderModule; +import org.hyperledger.besu.metrics.MetricsSystemModule; +import org.hyperledger.besu.metrics.ObservableMetricsSystem; + +import javax.inject.Named; +import javax.inject.Singleton; + +import dagger.Component; +import org.slf4j.Logger; + +/** An application context that knows how to provide dependencies based on Dagger setup. */ +@Singleton +@Component( + modules = { + BesuCommandModule.class, + MetricsSystemModule.class, + CachedMerkleTrieLoaderModule.class + }) +public interface BesuComponent { + + /** + * the configured and parsed representation of the user issued command to run Besu + * + * @return BesuCommand + */ + BesuCommand getBesuCommand(); + + /** + * a cached trie node loader + * + * @return CachedMerkleTrieLoader + */ + CachedMerkleTrieLoader getCachedMerkleTrieLoader(); + + /** + * a metrics system that is observable by a Prometheus or OTEL metrics collection subsystem + * + * @return ObservableMetricsSystem + */ + ObservableMetricsSystem getObservableMetricsSystem(); + + /** + * a Logger specifically configured to provide configuration feedback to users. + * + * @return Logger + */ + @Named("besuCommandLogger") + Logger getBesuCommandLogger(); +} diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index 876d5a912b..bde4b63b5b 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -16,6 +16,7 @@ package org.hyperledger.besu.controller; import static com.google.common.base.Preconditions.checkNotNull; +import org.hyperledger.besu.components.BesuComponent; import org.hyperledger.besu.config.CheckpointConfigOptions; import org.hyperledger.besu.config.GenesisConfigFile; import org.hyperledger.besu.config.GenesisConfigOptions; @@ -178,6 +179,19 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides private NetworkingConfiguration networkingConfiguration; private Boolean randomPeerPriority; + /** the Dagger configured context that can provide dependencies */ + protected BesuComponent besuComponent = null; + + /** + * Provide a BesuComponent which can be used to get other dependencies + * + * @param besuComponent application context that can be used to get other dependencies + * @return the besu controller builder + */ + public BesuControllerBuilder besuComponent(final BesuComponent besuComponent) { + this.besuComponent = besuComponent; + return this; + } /** * Storage provider besu controller builder. @@ -547,7 +561,10 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides reorgLoggingThreshold, dataDirectory.toString()); - final CachedMerkleTrieLoader cachedMerkleTrieLoader = new CachedMerkleTrieLoader(metricsSystem); + final CachedMerkleTrieLoader cachedMerkleTrieLoader = + besuComponent == null + ? new CachedMerkleTrieLoader(metricsSystem) + : besuComponent.getCachedMerkleTrieLoader(); final WorldStateArchive worldStateArchive = createWorldStateArchive(worldStateStorage, blockchain, cachedMerkleTrieLoader); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java index ca5b1f2488..be536f8091 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java @@ -37,6 +37,7 @@ import org.hyperledger.besu.cli.options.unstable.MetricsCLIOptions; import org.hyperledger.besu.cli.options.unstable.NetworkingOptions; import org.hyperledger.besu.cli.options.unstable.SynchronizerOptions; import org.hyperledger.besu.cli.options.unstable.TransactionPoolOptions; +import org.hyperledger.besu.components.BesuComponent; import org.hyperledger.besu.consensus.qbft.pki.PkiBlockCreationConfiguration; import org.hyperledger.besu.consensus.qbft.pki.PkiBlockCreationConfigurationProvider; import org.hyperledger.besu.controller.BesuController; @@ -166,6 +167,8 @@ public abstract class CommandTestAbstract { @Mock protected Logger mockLogger; + @Mock protected BesuComponent mockBesuComponent; + @Mock protected PkiBlockCreationConfigurationProvider mockPkiBlockCreationConfigProvider; @Mock protected PkiBlockCreationConfiguration mockPkiBlockCreationConfiguration; @@ -318,6 +321,7 @@ public abstract class CommandTestAbstract { .doReturn(mockPkiBlockCreationConfiguration) .when(mockPkiBlockCreationConfigProvider) .load(pkiKeyStoreConfigurationArgumentCaptor.capture()); + when(mockBesuComponent.getBesuCommandLogger()).thenReturn(mockLogger); } @Before @@ -402,7 +406,7 @@ public abstract class CommandTestAbstract { switch (testType) { case REQUIRED_OPTION: return new TestBesuCommandWithRequiredOption( - mockLogger, + mockBesuComponent, () -> rlpBlockImporter, this::jsonBlockImporterFactory, (blockchain) -> rlpBlockExporter, @@ -416,7 +420,7 @@ public abstract class CommandTestAbstract { privacyPluginService); case PORT_CHECK: return new TestBesuCommand( - mockLogger, + mockBesuComponent, () -> rlpBlockImporter, this::jsonBlockImporterFactory, (blockchain) -> rlpBlockExporter, @@ -430,7 +434,7 @@ public abstract class CommandTestAbstract { privacyPluginService); default: return new TestBesuCommandWithoutPortCheck( - mockLogger, + mockBesuComponent, () -> rlpBlockImporter, this::jsonBlockImporterFactory, (blockchain) -> rlpBlockExporter, @@ -452,7 +456,7 @@ public abstract class CommandTestAbstract { private Vertx vertx; TestBesuCommand( - final Logger mockLogger, + final BesuComponent besuComponent, final Supplier mockBlockImporter, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -465,7 +469,7 @@ public abstract class CommandTestAbstract { final PkiBlockCreationConfigurationProvider pkiBlockCreationConfigProvider, final PrivacyPluginServiceImpl privacyPluginService) { super( - mockLogger, + besuComponent, mockBlockImporter, jsonBlockImporterFactory, rlpBlockExporterFactory, @@ -536,7 +540,7 @@ public abstract class CommandTestAbstract { private final Boolean acceptTermsAndConditions = false; TestBesuCommandWithRequiredOption( - final Logger mockLogger, + final BesuComponent besuComponent, final Supplier mockBlockImporter, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -549,7 +553,7 @@ public abstract class CommandTestAbstract { final PkiBlockCreationConfigurationProvider pkiBlockCreationConfigProvider, final PrivacyPluginServiceImpl privacyPluginService) { super( - mockLogger, + besuComponent, mockBlockImporter, jsonBlockImporterFactory, rlpBlockExporterFactory, @@ -572,7 +576,7 @@ public abstract class CommandTestAbstract { public static class TestBesuCommandWithoutPortCheck extends TestBesuCommand { TestBesuCommandWithoutPortCheck( - final Logger mockLogger, + final BesuComponent context, final Supplier mockBlockImporter, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -585,7 +589,7 @@ public abstract class CommandTestAbstract { final PkiBlockCreationConfigurationProvider pkiBlockCreationConfigProvider, final PrivacyPluginServiceImpl privacyPluginService) { super( - mockLogger, + context, mockBlockImporter, jsonBlockImporterFactory, rlpBlockExporterFactory, diff --git a/ethereum/core/build.gradle b/ethereum/core/build.gradle index 99a0f7d178..53cb8ca6a7 100644 --- a/ethereum/core/build.gradle +++ b/ethereum/core/build.gradle @@ -46,6 +46,8 @@ dependencies { implementation 'com.fasterxml.jackson.core:jackson-databind' implementation 'com.google.guava:guava' + implementation 'com.google.dagger:dagger' + annotationProcessor 'com.google.dagger:dagger-compiler' implementation 'io.opentelemetry:opentelemetry-api' implementation 'io.vertx:vertx-core' implementation 'net.java.dev.jna:jna' diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedMerkleTrieLoaderModule.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedMerkleTrieLoaderModule.java new file mode 100644 index 0000000000..51cfdd8707 --- /dev/null +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedMerkleTrieLoaderModule.java @@ -0,0 +1,31 @@ +/* + * Copyright Hyperledger Besu Contributors. + * + * 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.ethereum.bonsai.cache; + +import org.hyperledger.besu.metrics.ObservableMetricsSystem; + +import dagger.Module; +import dagger.Provides; + +@Module +public class CachedMerkleTrieLoaderModule { + + @Provides + CachedMerkleTrieLoader provideCachedMerkleTrieLoaderModule( + final ObservableMetricsSystem metricsSystem) { + return new CachedMerkleTrieLoader(metricsSystem); + } +} diff --git a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java index f752ae15c1..33c3d1472f 100644 --- a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java +++ b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java @@ -41,6 +41,7 @@ import org.hyperledger.besu.evm.tracing.OperationTracer; import org.hyperledger.besu.evm.tracing.StandardJsonTracer; import org.hyperledger.besu.evm.worldstate.WorldState; import org.hyperledger.besu.evm.worldstate.WorldUpdater; +import org.hyperledger.besu.metrics.MetricsSystemModule; import org.hyperledger.besu.util.LogConfigurator; import java.io.BufferedWriter; diff --git a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolComponent.java b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolComponent.java index b5c4fc4d6f..5f66c5ddf5 100644 --- a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolComponent.java +++ b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolComponent.java @@ -1,5 +1,5 @@ /* - * Copyright ConsenSys AG. + * Copyright Hyperledger Besu Contributors. * * 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 @@ -20,6 +20,8 @@ import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.MutableWorldState; import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec; import org.hyperledger.besu.evm.worldstate.WorldUpdater; +import org.hyperledger.besu.metrics.MetricsConfigurationModule; +import org.hyperledger.besu.metrics.MetricsSystemModule; import java.util.function.Function; import javax.inject.Singleton; @@ -34,6 +36,7 @@ import dagger.Component; DataStoreModule.class, BlockchainModule.class, EvmToolCommandOptionsModule.class, + MetricsConfigurationModule.class, MetricsSystemModule.class, }) public interface EvmToolComponent { diff --git a/metrics/core/build.gradle b/metrics/core/build.gradle index 23faf8195e..5dc48ef514 100644 --- a/metrics/core/build.gradle +++ b/metrics/core/build.gradle @@ -39,6 +39,8 @@ dependencies { api 'org.slf4j:slf4j-api' implementation 'com.google.guava:guava' + implementation 'com.google.dagger:dagger' + implementation 'info.picocli:picocli' implementation 'io.grpc:grpc-netty' implementation 'io.grpc:grpc-core' implementation 'io.netty:netty-tcnative-boringssl-static' @@ -71,6 +73,8 @@ dependencies { testRuntimeOnly 'org.junit.vintage:junit-vintage-engine' testSupportImplementation 'org.mockito:mockito-core' + + annotationProcessor 'com.google.dagger:dagger-compiler' } diff --git a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/MetricsSystemModule.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsConfigurationModule.java similarity index 67% rename from ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/MetricsSystemModule.java rename to metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsConfigurationModule.java index 788a3a02a3..8c44e86962 100644 --- a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/MetricsSystemModule.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsConfigurationModule.java @@ -1,5 +1,5 @@ /* - * Copyright ConsenSys AG. + * Copyright Hyperledger Besu Contributors. * * 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 @@ -11,27 +11,23 @@ * specific language governing permissions and limitations under the License. * * SPDX-License-Identifier: Apache-2.0 - * */ -package org.hyperledger.besu.evmtool; +package org.hyperledger.besu.metrics; -import org.hyperledger.besu.metrics.MetricsSystemFactory; import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; -import org.hyperledger.besu.plugin.services.MetricsSystem; import javax.inject.Singleton; import dagger.Module; import dagger.Provides; -@SuppressWarnings("WeakerAccess") +/** Dagger module for providing the {@link MetricsConfiguration} instance. */ @Module -public class MetricsSystemModule { - +public class MetricsConfigurationModule { @Provides @Singleton - MetricsSystem getMetricsSystem() { - return MetricsSystemFactory.create(MetricsConfiguration.builder().build()); + MetricsConfiguration provideMetricsConfiguration() { + return MetricsConfiguration.builder().build(); } } diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsSystemModule.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsSystemModule.java new file mode 100644 index 0000000000..05dec0fb1f --- /dev/null +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsSystemModule.java @@ -0,0 +1,45 @@ +/* + * Copyright Hyperledger Besu Contributors. + * + * 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; + +import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; +import org.hyperledger.besu.plugin.services.MetricsSystem; + +import javax.inject.Singleton; + +import dagger.Module; +import dagger.Provides; + +/** + * Dagger module for providing the {@link MetricsSystem} and {@link ObservableMetricsSystem} + * instances. + */ +@Module +public class MetricsSystemModule { + + @Provides + @Singleton + MetricsSystem provideMetricsSystem(final MetricsConfiguration metricsConfig) { + return MetricsSystemFactory.create(metricsConfig); + } + + @Provides + @Singleton + ObservableMetricsSystem provideObservableMetricsSystem(final MetricsConfiguration metricsConfig) { + return MetricsSystemFactory.create(metricsConfig); + } +} diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsConfiguration.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsConfiguration.java index 2c313af74b..6a2424df50 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsConfiguration.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright ConsenSys AG. + * Copyright Hyperledger Besu Contributors. * * 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