Skip to content

Commit

Permalink
fix: metric service light refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Sep 3, 2024
1 parent b444460 commit 3c61790
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import static com.hedera.block.server.Constants.SINGLE_BLOCK_METHOD_NAME;
import static com.hedera.block.server.Translator.fromPbj;
import static com.hedera.block.server.Translator.toPbj;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.SingleBlocksNotFound;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.SingleBlocksRetrieved;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.SingleBlocksNotFound;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.SingleBlocksRetrieved;
import static java.lang.System.Logger;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.hedera.block.server.consumer;

import static com.hedera.block.server.Translator.fromPbj;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockItemsConsumed;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItemsConsumed;
import static java.lang.System.Logger;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.hedera.block.server.mediator;

import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockItems;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockStreamMediatorError;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Gauge.Subscribers;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItems;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockStreamMediatorError;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Gauge.Subscribers;
import static java.lang.System.Logger;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,48 @@

import edu.umd.cs.findbugs.annotations.NonNull;

public final class BlockNodeMetricNames {
private BlockNodeMetricNames() {}

/**
* The BlockNodeMetricNames class contains the names of the metrics used by the BlockNode.
*
* <p>These names are used to register the metrics with the metrics service.
*/
public final class BlockNodeMetricTypes {
private BlockNodeMetricTypes() {}

/**
* Add new counting metrics to this enum to automatically register them with the metrics
* service.
*
* <p>Each enum value should have a unique grafana label and meaningful description. These
* counters can capture data on standard operations or errors.
*/
public enum Counter implements MetricMetadata {
// Standard counters
/** The number of live block items received from a producer. */
LiveBlockItemsReceived("live_block_items_received", "Live Block Items Received"),

/** The number of live block items received before publishing to the RingBuffer. */
LiveBlockItems("live_block_items", "Live BlockItems"),

/**
* The number of blocks persisted to storage.
*
* <p>Block items are not counted here, only the blocks.
*/
BlocksPersisted("blocks_persisted", "Blocks Persisted"),

/** The number of live block items consumed from the by each consumer observer. */
LiveBlockItemsConsumed("live_block_items_consumed", "Live Block Items Consumed"),

/** The number of single blocks retrieved from the singleBlock rpc service. */
SingleBlocksRetrieved("single_blocks_retrieved", "Single Blocks Retrieved"),

/** The number of single blocks not found via the singleBlock rpc service. */
SingleBlocksNotFound("single_blocks_not_found", "Single Blocks Not Found"),

// Error counters

/** The number of errors encountered by the live block stream mediator. */
LiveBlockStreamMediatorError(
"live_block_stream_mediator_error", "Live Block Stream Mediator Error");

Expand All @@ -55,7 +84,15 @@ public String description() {
}
}

/**
* Add new gauge metrics to this enum to automatically register them with the metrics service.
*
* <p>Each enum value should have a unique grafana label and meaningful description. These
* gauges can capture data on standard operations or errors.
*/
public enum Gauge implements MetricMetadata {

/** The number of subscribers receiving the live block stream. */
Subscribers("subscribers", "Subscribers");

private final String grafanaLabel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@

/** Use member variables of this class to update metric data for the Hedera Block Node. */
public interface MetricsService {
Counter get(@NonNull BlockNodeMetricNames.Counter key);
/**
* Use this method to get a specific counter for the given metric type.
*
* @param key to get a specific counter
* @return the counter
*/
Counter get(@NonNull BlockNodeMetricTypes.Counter key);

LongGauge get(@NonNull BlockNodeMetricNames.Gauge key);
/**
* Use this method to get a specific gauge for the given metric type.
*
* @param key to get a specific gauge
* @return the gauge
*/
LongGauge get(@NonNull BlockNodeMetricTypes.Gauge key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class MetricsServiceImpl implements MetricsService {

private static final String CATEGORY = "hedera_block_node";

private final EnumMap<BlockNodeMetricNames.Counter, Counter> counters =
new EnumMap<>(BlockNodeMetricNames.Counter.class);
private final EnumMap<BlockNodeMetricNames.Gauge, LongGauge> gauges =
new EnumMap<>(BlockNodeMetricNames.Gauge.class);
private final EnumMap<BlockNodeMetricTypes.Counter, Counter> counters =
new EnumMap<>(BlockNodeMetricTypes.Counter.class);
private final EnumMap<BlockNodeMetricTypes.Gauge, LongGauge> gauges =
new EnumMap<>(BlockNodeMetricTypes.Gauge.class);

/**
* Create singleton instance of metrics service to be used throughout the application.
Expand All @@ -46,7 +46,7 @@ public class MetricsServiceImpl implements MetricsService {
@Inject
public MetricsServiceImpl(@NonNull final Metrics metrics) {
// Initialize the counters
for (BlockNodeMetricNames.Counter counter : BlockNodeMetricNames.Counter.values()) {
for (BlockNodeMetricTypes.Counter counter : BlockNodeMetricTypes.Counter.values()) {
counters.put(
counter,
metrics.getOrCreate(
Expand All @@ -55,7 +55,7 @@ public MetricsServiceImpl(@NonNull final Metrics metrics) {
}

// Initialize the gauges
for (BlockNodeMetricNames.Gauge gauge : BlockNodeMetricNames.Gauge.values()) {
for (BlockNodeMetricTypes.Gauge gauge : BlockNodeMetricTypes.Gauge.values()) {
gauges.put(
gauge,
metrics.getOrCreate(
Expand All @@ -64,15 +64,27 @@ public MetricsServiceImpl(@NonNull final Metrics metrics) {
}
}

/**
* Use this method to get a specific counter for the given metric type.
*
* @param key to get a specific counter
* @return the counter
*/
@NonNull
@Override
public Counter get(@NonNull BlockNodeMetricNames.Counter key) {
public Counter get(@NonNull BlockNodeMetricTypes.Counter key) {
return counters.get(key);
}

/**
* Use this method to get a specific gauge for the given metric type.
*
* @param key to get a specific gauge
* @return the gauge
*/
@NonNull
@Override
public LongGauge get(@NonNull BlockNodeMetricNames.Gauge key) {
public LongGauge get(@NonNull BlockNodeMetricTypes.Gauge key) {
return gauges.get(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.hedera.block.server.persistence.storage.write;

import static com.hedera.block.server.Constants.BLOCK_FILE_EXTENSION;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.BlocksPersisted;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.BlocksPersisted;
import static java.lang.System.Logger;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static com.hedera.block.server.Translator.fromPbj;
import static com.hedera.block.server.Translator.toPbj;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockItemsReceived;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItemsReceived;
import static com.hedera.block.server.producer.Util.getFakeHash;
import static java.lang.System.Logger;
import static java.lang.System.Logger.Level.DEBUG;
Expand Down Expand Up @@ -64,9 +64,11 @@ public class ProducerBlockItemObserver
* to the upstream producer via the responseStreamObserver.
*
* @param publisher the block item publisher to used to pass block items to consumers as they
* arrive from the upstream producer
* arrive from the upstream producer.
* @param publishStreamResponseObserver the response stream observer to send responses back to
* the upstream producer for each block item processed
* the upstream producer for each block item processed.
* @param blockNodeContext the block node context used to access context objects for the Block
* Node (e.g. - the metrics service).
* @param serviceStatus the service status used to determine if the downstream service is
* accepting block items. In the event of an unrecoverable exception, it will be used to
* stop the web server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.hedera.block.server;

import static com.hedera.block.server.Translator.fromPbj;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockItems;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItems;
import static com.hedera.block.server.producer.Util.getFakeHash;
import static com.hedera.block.server.util.PersistTestUtils.generateBlockItems;
import static java.lang.System.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.hedera.block.server.mediator;

import static com.hedera.block.server.Translator.fromPbj;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockItems;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItems;
import static com.hedera.block.server.util.PersistTestUtils.generateBlockItems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.hedera.block.server.metrics;

import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.BlocksPersisted;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockItems;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockItemsConsumed;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.SingleBlocksRetrieved;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Gauge.Subscribers;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.BlocksPersisted;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItems;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItemsConsumed;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.SingleBlocksRetrieved;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Gauge.Subscribers;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.hedera.block.server.config.BlockNodeContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.hedera.block.server.producer;

import static com.hedera.block.server.Translator.fromPbj;
import static com.hedera.block.server.metrics.BlockNodeMetricNames.Counter.LiveBlockItems;
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItems;
import static com.hedera.block.server.producer.Util.getFakeHash;
import static com.hedera.block.server.util.PersistTestUtils.generateBlockItems;
import static com.hedera.block.server.util.PersistTestUtils.reverseByteArray;
Expand Down

0 comments on commit 3c61790

Please sign in to comment.