diff --git a/server/src/main/java/com/hedera/block/server/metrics/MetricsInjectionModule.java b/server/src/main/java/com/hedera/block/server/metrics/MetricsInjectionModule.java index f65d3e8e1..424856837 100644 --- a/server/src/main/java/com/hedera/block/server/metrics/MetricsInjectionModule.java +++ b/server/src/main/java/com/hedera/block/server/metrics/MetricsInjectionModule.java @@ -19,6 +19,7 @@ import com.swirlds.common.metrics.platform.DefaultMetricsProvider; import com.swirlds.config.api.Configuration; import com.swirlds.metrics.api.Metrics; +import dagger.Binds; import dagger.Module; import dagger.Provides; import javax.inject.Singleton; @@ -30,14 +31,11 @@ public interface MetricsInjectionModule { /** * Provides the metrics service. * - * @param metrics the metrics to be used by the service * @return the metrics service */ @Singleton - @Provides - static MetricsService provideMetricsService(Metrics metrics) { - return new MetricsService(metrics); - } + @Binds + MetricsService bindMetricsService(MetricsServiceImpl metricsService); /** * Provides the metrics. diff --git a/server/src/main/java/com/hedera/block/server/metrics/MetricsService.java b/server/src/main/java/com/hedera/block/server/metrics/MetricsService.java index 367a590bd..75d1fc971 100644 --- a/server/src/main/java/com/hedera/block/server/metrics/MetricsService.java +++ b/server/src/main/java/com/hedera/block/server/metrics/MetricsService.java @@ -18,73 +18,17 @@ import com.swirlds.metrics.api.Counter; import com.swirlds.metrics.api.LongGauge; -import com.swirlds.metrics.api.Metrics; -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Use member variables of this class to update metric data for the Hedera Block Node. - * - *

Metrics are updated by calling the appropriate method on the metric object instance. For - * example, to increment a counter, call {@link Counter#increment()}. - */ -public class MetricsService { - - private static final String CATEGORY = "hedera_block_node"; - - // Live BlockItem Counter - private static final Counter.Config LIVE_BLOCK_ITEM_COUNTER = - new Counter.Config(CATEGORY, "live_block_items").withDescription("Live BlockItems"); - - // Block Persistence Counter - private static final Counter.Config BLOCK_PERSISTENCE_COUNTER = - new Counter.Config(CATEGORY, "blocks_persisted").withDescription("Blocks Persisted"); - - // Subscriber Gauge - private static final LongGauge.Config SUBSCRIBER_GAUGE = - new LongGauge.Config(CATEGORY, "subscribers").withDescription("Subscribers"); - - // Single Block Retrieved Counter - private static final Counter.Config SINGLE_BLOCK_RETRIEVED_COUNTER = - new Counter.Config(CATEGORY, "single_blocks_retrieved") - .withDescription("Single Blocks Retrieved"); - - private final Counter liveBlockItems; - - private final Counter blocksPersisted; - - private final Counter singleBlocksRetrieved; - - private final LongGauge subscribers; +public interface MetricsService { /** Update the counter of live block items transiting via the live stream. */ - public final Counter liveBlockItems() { - return liveBlockItems; - } + Counter liveBlockItems(); /** Update the counter of blocks persisted to storage. */ - public final Counter blocksPersisted() { - return blocksPersisted; - } + Counter blocksPersisted(); /** Update the counter of single blocks retrieved from storage. */ - public final Counter singleBlocksRetrieved() { - return singleBlocksRetrieved; - } + Counter singleBlocksRetrieved(); /** Update the gauge of subscribers currently consuming to the live stream. */ - public final LongGauge subscribers() { - return subscribers; - } - - /** - * Create singleton instance of metrics service to be used throughout the application. - * - * @param metrics the metrics instance - */ - public MetricsService(@NonNull final Metrics metrics) { - this.liveBlockItems = metrics.getOrCreate(LIVE_BLOCK_ITEM_COUNTER); - this.blocksPersisted = metrics.getOrCreate(BLOCK_PERSISTENCE_COUNTER); - this.singleBlocksRetrieved = metrics.getOrCreate(SINGLE_BLOCK_RETRIEVED_COUNTER); - this.subscribers = metrics.getOrCreate(SUBSCRIBER_GAUGE); - } + LongGauge subscribers(); } diff --git a/server/src/main/java/com/hedera/block/server/metrics/MetricsServiceImpl.java b/server/src/main/java/com/hedera/block/server/metrics/MetricsServiceImpl.java new file mode 100644 index 000000000..3b4c5539e --- /dev/null +++ b/server/src/main/java/com/hedera/block/server/metrics/MetricsServiceImpl.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2024 Hedera Hashgraph, LLC + * + * 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. + */ + +package com.hedera.block.server.metrics; + +import com.swirlds.metrics.api.Counter; +import com.swirlds.metrics.api.LongGauge; +import com.swirlds.metrics.api.Metrics; +import edu.umd.cs.findbugs.annotations.NonNull; +import javax.inject.Inject; + +/** + * Use member variables of this class to update metric data for the Hedera Block Node. + * + *

Metrics are updated by calling the appropriate method on the metric object instance. For + * example, to increment a counter, call {@link Counter#increment()}. + */ +public class MetricsServiceImpl implements MetricsService { + + private static final String CATEGORY = "hedera_block_node"; + + // Live BlockItem Counter + private static final Counter.Config LIVE_BLOCK_ITEM_COUNTER = + new Counter.Config(CATEGORY, "live_block_items").withDescription("Live BlockItems"); + + // Block Persistence Counter + private static final Counter.Config BLOCK_PERSISTENCE_COUNTER = + new Counter.Config(CATEGORY, "blocks_persisted").withDescription("Blocks Persisted"); + + // Subscriber Gauge + private static final LongGauge.Config SUBSCRIBER_GAUGE = + new LongGauge.Config(CATEGORY, "subscribers").withDescription("Subscribers"); + + // Single Block Retrieved Counter + private static final Counter.Config SINGLE_BLOCK_RETRIEVED_COUNTER = + new Counter.Config(CATEGORY, "single_blocks_retrieved") + .withDescription("Single Blocks Retrieved"); + + private final Counter liveBlockItems; + + private final Counter blocksPersisted; + + private final Counter singleBlocksRetrieved; + + private final LongGauge subscribers; + + /** Update the counter of live block items transiting via the live stream. */ + @Override + public final Counter liveBlockItems() { + return liveBlockItems; + } + + /** Update the counter of blocks persisted to storage. */ + @Override + public final Counter blocksPersisted() { + return blocksPersisted; + } + + /** Update the counter of single blocks retrieved from storage. */ + @Override + public final Counter singleBlocksRetrieved() { + return singleBlocksRetrieved; + } + + /** Update the gauge of subscribers currently consuming to the live stream. */ + @Override + public final LongGauge subscribers() { + return subscribers; + } + + /** + * Create singleton instance of metrics service to be used throughout the application. + * + * @param metrics the metrics instance + */ + @Inject + public MetricsServiceImpl(@NonNull final Metrics metrics) { + this.liveBlockItems = metrics.getOrCreate(LIVE_BLOCK_ITEM_COUNTER); + this.blocksPersisted = metrics.getOrCreate(BLOCK_PERSISTENCE_COUNTER); + this.singleBlocksRetrieved = metrics.getOrCreate(SINGLE_BLOCK_RETRIEVED_COUNTER); + this.subscribers = metrics.getOrCreate(SUBSCRIBER_GAUGE); + } +} diff --git a/server/src/test/java/com/hedera/block/server/config/BlockNodeContextTest.java b/server/src/test/java/com/hedera/block/server/config/BlockNodeContextTest.java index ef2573003..09fb091b3 100644 --- a/server/src/test/java/com/hedera/block/server/config/BlockNodeContextTest.java +++ b/server/src/test/java/com/hedera/block/server/config/BlockNodeContextTest.java @@ -20,6 +20,7 @@ import static org.mockito.Mockito.*; import com.hedera.block.server.metrics.MetricsService; +import com.hedera.block.server.metrics.MetricsServiceImpl; import com.swirlds.config.api.Configuration; import org.junit.jupiter.api.Test; @@ -28,7 +29,7 @@ class BlockNodeContextTest { @Test void BlockNodeContext_initializesWithMetricsAndConfiguration() { Configuration configuration = mock(Configuration.class); - MetricsService metricsService = mock(MetricsService.class); + MetricsService metricsService = mock(MetricsServiceImpl.class); BlockNodeContext context = new BlockNodeContext(metricsService, configuration); diff --git a/server/src/test/java/com/hedera/block/server/metrics/MetricsInjectionModuleTest.java b/server/src/test/java/com/hedera/block/server/metrics/MetricsInjectionModuleTest.java index 525ef747e..21305a762 100644 --- a/server/src/test/java/com/hedera/block/server/metrics/MetricsInjectionModuleTest.java +++ b/server/src/test/java/com/hedera/block/server/metrics/MetricsInjectionModuleTest.java @@ -25,23 +25,11 @@ import java.io.IOException; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) class MetricsInjectionModuleTest { - @Mock private Metrics metrics; - - @Test - void testProvideMetricsService() { - // Call the method under test - MetricsService metricsService = MetricsInjectionModule.provideMetricsService(metrics); - - // Verify that the metricsService is correctly instantiated - assertNotNull(metricsService); - } - @Test void testProvideMetrics() throws IOException { BlockNodeContext context = TestConfigUtil.getTestBlockNodeContext(); diff --git a/server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java b/server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java index eba0ec506..d198d293e 100644 --- a/server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java +++ b/server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java @@ -32,7 +32,7 @@ void MetricsService_initializesLiveBlockItemsCounter() { Counter liveBlockItems = mock(Counter.class); when(metrics.getOrCreate(any(Counter.Config.class))).thenReturn(liveBlockItems); - MetricsService service = new MetricsService(metrics); + MetricsService service = new MetricsServiceImpl(metrics); assertEquals(liveBlockItems, service.liveBlockItems()); @@ -46,7 +46,7 @@ void MetricsService_initializesBlocksPersistedCounter() { Counter blocksPersisted = mock(Counter.class); when(metrics.getOrCreate(any(Counter.Config.class))).thenReturn(blocksPersisted); - MetricsService service = new MetricsService(metrics); + MetricsService service = new MetricsServiceImpl(metrics); assertEquals(blocksPersisted, service.blocksPersisted()); @@ -60,7 +60,7 @@ void MetricsService_initializesSingleBlocksRetrievedCounter() { Counter singleBlocksRetrieved = mock(Counter.class); when(metrics.getOrCreate(any(Counter.Config.class))).thenReturn(singleBlocksRetrieved); - MetricsService service = new MetricsService(metrics); + MetricsService service = new MetricsServiceImpl(metrics); assertEquals(singleBlocksRetrieved, service.singleBlocksRetrieved()); @@ -74,7 +74,7 @@ void MetricsService_initializesSubscribersGauge() { LongGauge subscribers = mock(LongGauge.class); when(metrics.getOrCreate(any(LongGauge.Config.class))).thenReturn(subscribers); - MetricsService service = new MetricsService(metrics); + MetricsService service = new MetricsServiceImpl(metrics); assertEquals(subscribers, service.subscribers()); diff --git a/server/src/test/java/com/hedera/block/server/util/TestConfigUtil.java b/server/src/test/java/com/hedera/block/server/util/TestConfigUtil.java index ef3789dcc..4653035af 100644 --- a/server/src/test/java/com/hedera/block/server/util/TestConfigUtil.java +++ b/server/src/test/java/com/hedera/block/server/util/TestConfigUtil.java @@ -20,6 +20,7 @@ import com.hedera.block.server.config.TestConfigBuilder; import com.hedera.block.server.consumer.ConsumerConfig; import com.hedera.block.server.metrics.MetricsService; +import com.hedera.block.server.metrics.MetricsServiceImpl; import com.swirlds.common.metrics.platform.DefaultMetricsProvider; import com.swirlds.config.api.Configuration; import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; @@ -60,7 +61,7 @@ public static BlockNodeContext getTestBlockNodeContext( Metrics metrics = getTestMetrics(testConfiguration); - MetricsService metricsService = new MetricsService(metrics); + MetricsService metricsService = new MetricsServiceImpl(metrics); return new BlockNodeContext(metricsService, testConfiguration); }