Skip to content

Commit

Permalink
Extracted Interface and refactored Dagger Metrics Injection Module to…
Browse files Browse the repository at this point in the history
… reflect the changes.

Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 committed Aug 27, 2024
1 parent 3257eca commit c8e0aeb
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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();
}
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c8e0aeb

Please sign in to comment.