Skip to content

Commit da27095

Browse files
committed
Added the metrics to keep track for requests failed and received.
Signed-off-by: Harsh Sawarkar <[email protected]>
1 parent c676c8f commit da27095

File tree

9 files changed

+84
-17
lines changed

9 files changed

+84
-17
lines changed

server/src/main/java/com/hedera/block/server/BlockNodeApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public BlockNodeApp(
6666
this.pbjBlockStreamService = requireNonNull(pbjBlockStreamService);
6767
this.pbjBlockAccessService = requireNonNull(pbjBlockAccessService);
6868
this.webServerBuilder = requireNonNull(webServerBuilder);
69-
this.pbjServerStatusService = pbjServerStatusService;
69+
this.pbjServerStatusService = requireNonNull(pbjServerStatusService);
7070
this.serverConfig = requireNonNull(serverConfig);
7171
this.configurationLogging = requireNonNull(configurationLogging);
7272
}

server/src/main/java/com/hedera/block/server/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public final class Constants {
1212
/** Constant mapped to the name of the BlockAccess service in the .proto file */
1313
public static final String SERVICE_NAME_BLOCK_ACCESS = "BlockAccessService";
1414

15+
/** Constant mapped to the name of the BlockNode service in the .proto file */
1516
public static final String SERVICE_NAME_BLOCK_NODE = "BlockNodeService";
1617

1718
/** Constant representing the service domain */
@@ -23,6 +24,7 @@ public final class Constants {
2324
/** Constant mapped to the full name of the BlockAccess service */
2425
public static final String FULL_SERVICE_NAME_BLOCK_ACCESS = SERVICE_DOMAIN + SERVICE_NAME_BLOCK_ACCESS;
2526

27+
/** Constant mapped to the full name of the BlockNode service */
2628
public static final String FULL_SERVICE_NAME_BLOCK_NODE = SERVICE_DOMAIN + SERVICE_NAME_BLOCK_NODE;
2729

2830
/** Constant defining the block file extension */

server/src/main/java/com/hedera/block/server/metrics/BlockNodeMetricTypes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public enum Counter implements MetricMetadata {
2626
/** The number of live block items received before publishing to the RingBuffer. */
2727
LiveBlockItems("live_block_items", "Live BlockItems"),
2828

29+
/** The number of requests received by the ServerStatus API */
30+
RequestsReceived("requests_received", "Total Requests Received by ServerStatus API"),
31+
32+
/** The number of errors encountered while handling requests */
33+
RequestsFailed("requests_failed", "Total Requests Failed in ServerStatus API"),
34+
2935
/** The number of PublishStreamResponses generated and published to the subscribers. */
3036
SuccessfulPubStreamResp("successful_pub_stream_resp", "Successful Publish Stream Responses"),
3137

server/src/main/java/com/hedera/block/server/pbj/PbjServerStatusService.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,48 @@
99
import java.util.Arrays;
1010
import java.util.List;
1111

12+
/**
13+
* The PbjServerStatusService interface provides type definitions and default method
14+
* implementations for PBJ to route gRPC requests to the server status service.
15+
*/
1216
public interface PbjServerStatusService extends ServiceInterface {
17+
18+
/**
19+
* ServerStatusMethod types define the gRPC methods available on the ServerStatusService.
20+
*/
1321
enum ServerStatusMethod implements Method {
22+
/**
23+
* The serverStatus method represents the unary gRPC method
24+
* consumers should use to retrieve the status of the Block Node.
25+
*/
1426
serverStatus
1527
}
1628

29+
/**
30+
* Provides the service name of the ServerStatusService.
31+
*
32+
* @return the service name of the ServerStatusService.
33+
*/
1734
@NonNull
1835
default String serviceName() {
1936
return SERVICE_NAME_BLOCK_NODE;
2037
}
2138

39+
/**
40+
* Provides the full name of the ServerStatusService.
41+
*
42+
* @return the full name of the ServerStatusService.
43+
*/
2244
@NonNull
2345
default String fullName() {
2446
return FULL_SERVICE_NAME_BLOCK_NODE;
2547
}
2648

49+
/**
50+
* Provides the list of methods available in the ServerStatusService.
51+
*
52+
* @return the list of available methods in the ServerStatusService.
53+
*/
2754
@NonNull
2855
default List<Method> methods() {
2956
return Arrays.asList(PbjServerStatusService.ServerStatusMethod.values());

server/src/main/java/com/hedera/block/server/pbj/PbjServerStatusServiceProxy.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.hedera.block.server.pbj;
33

4+
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.RequestsFailed;
5+
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.RequestsReceived;
46
import static java.lang.System.Logger.Level.DEBUG;
57
import static java.lang.System.Logger.Level.ERROR;
68

79
import com.hedera.block.server.metrics.MetricsService;
8-
import com.hedera.block.server.persistence.storage.read.BlockReader;
910
import com.hedera.block.server.service.ServiceStatus;
10-
import com.hedera.hapi.block.BlockUnparsed;
1111
import com.hedera.hapi.block.ServerStatusRequest;
1212
import com.hedera.hapi.block.ServerStatusResponse;
1313
import com.hedera.pbj.runtime.ParseException;
@@ -16,31 +16,28 @@
1616
import com.hedera.pbj.runtime.grpc.Pipelines;
1717
import com.hedera.pbj.runtime.io.buffer.Bytes;
1818
import edu.umd.cs.findbugs.annotations.NonNull;
19+
import java.util.Objects;
1920
import javax.inject.Inject;
2021

2122
public class PbjServerStatusServiceProxy implements PbjServerStatusService {
2223

2324
private final System.Logger LOGGER = System.getLogger(getClass().getName());
2425

2526
private final ServiceStatus serviceStatus;
26-
private final BlockReader<BlockUnparsed> blockReader;
2727
private final MetricsService metricsService;
2828

2929
/**
3030
* Creates a new PbjBlockAccessServiceProxy instance.
3131
*
3232
* @param serviceStatus the service status
33-
* @param blockReader the block reader
33+
* // * @param blockReader the block reader
3434
* @param metricsService the metrics service
3535
*/
3636
@Inject
3737
public PbjServerStatusServiceProxy(
38-
@NonNull final ServiceStatus serviceStatus,
39-
@NonNull final BlockReader<BlockUnparsed> blockReader,
40-
@NonNull final MetricsService metricsService) {
41-
this.serviceStatus = serviceStatus;
42-
this.blockReader = blockReader;
43-
this.metricsService = metricsService;
38+
@NonNull final ServiceStatus serviceStatus, @NonNull final MetricsService metricsService) {
39+
this.serviceStatus = Objects.requireNonNull(serviceStatus);
40+
this.metricsService = Objects.requireNonNull(metricsService);
4441
}
4542

4643
@NonNull
@@ -71,15 +68,16 @@ private Bytes createServerStatusResponse(@NonNull final ServerStatusResponse ser
7168

7269
ServerStatusResponse serverStatus(ServerStatusRequest serverStatusRequest) {
7370
LOGGER.log(DEBUG, "Executing Unary serverStatus gRPC method");
74-
7571
if (serviceStatus.isRunning()) {
72+
metricsService.get(RequestsReceived).increment();
7673
return ServerStatusResponse.newBuilder()
7774
.firstAvailableBlock(serviceStatus.getFirstAvailableBlockNumber())
7875
.lastAvailableBlock(serviceStatus.getLatestReceivedBlockNumber())
7976
.onlyLatestState(serviceStatus.getOnlyLatestState())
8077
.versionInformation(serviceStatus.getVersionInformation())
8178
.build();
8279
} else {
80+
metricsService.get(RequestsFailed).increment();
8381
LOGGER.log(ERROR, "Unary serverStatus gRPC method is not currently running");
8482

8583
return ServerStatusResponse.newBuilder().build();

server/src/main/java/com/hedera/block/server/service/ServiceStatus.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,31 @@ public interface ServiceStatus {
8686
*/
8787
void setFirstAvailableBlockNumber(long firstAvailableBlockNumber);
8888

89+
/**
90+
* Checks if only the latest state should be maintained.
91+
*
92+
* @return true if only the latest state is maintained, false otherwise.
93+
*/
8994
boolean getOnlyLatestState();
9095

96+
/**
97+
* Sets whether only the latest state should be maintained.
98+
*
99+
* @param onlyLatestState true to maintain only the latest state, false otherwise.
100+
*/
91101
void setOnlyLatestState(boolean onlyLatestState);
92102

103+
/**
104+
* Retrieves the version information of the Block Node.
105+
*
106+
* @return the {@link BlockNodeVersions} containing version details.
107+
*/
93108
BlockNodeVersions getVersionInformation();
94109

110+
/**
111+
* Sets the version information of the Block Node.
112+
*
113+
* @param versionInformation the {@link BlockNodeVersions} containing version details.
114+
*/
95115
void setVersionInformation(BlockNodeVersions versionInformation);
96116
}

server/src/test/java/com/hedera/block/server/BlockNodeAppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void setup() throws IOException {
101101
consumerConfig,
102102
producerConfig),
103103
new PbjBlockAccessServiceProxy(serviceStatus, blockReader, metricsService),
104-
new PbjServerStatusServiceProxy(serviceStatus, blockReader, metricsService),
104+
new PbjServerStatusServiceProxy(serviceStatus, metricsService),
105105
webServerBuilder,
106106
serverConfig,
107107
configurationLogging);

server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.BlocksPersisted;
55
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItems;
66
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.LiveBlockItemsConsumed;
7+
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.RequestsReceived;
78
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.SingleBlocksRetrieved;
89
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Gauge.Consumers;
910
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -56,6 +57,21 @@ void MetricsService_verifyLiveBlockItemsCounter() {
5657
assertEquals(10, metricsService.get(LiveBlockItems).get());
5758
}
5859

60+
@Test
61+
void MetricsService_verifyRequestsReceivedCounter() {
62+
for (int i = 0; i < 5; i++) {
63+
metricsService.get(RequestsReceived).increment();
64+
}
65+
66+
assertEquals(
67+
RequestsReceived.grafanaLabel(),
68+
metricsService.get(RequestsReceived).getName());
69+
assertEquals(
70+
RequestsReceived.description(),
71+
metricsService.get(RequestsReceived).getDescription());
72+
assertEquals(5, metricsService.get(RequestsReceived).get());
73+
}
74+
5975
@Test
6076
void MetricsService_verifyBlocksPersistedCounter() {
6177

server/src/test/java/com/hedera/block/server/pbj/PbjServerStatusServiceProxyTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public void setUp() throws IOException {
5252

5353
@Test
5454
public void testServerStatus() {
55-
final PbjServerStatusServiceProxy serviceProxy =
56-
new PbjServerStatusServiceProxy(serviceStatus, blockReader, metricsService);
55+
final PbjServerStatusServiceProxy serviceProxy = new PbjServerStatusServiceProxy(serviceStatus, metricsService);
5756

5857
Pipeline<? super Bytes> pipeline =
5958
serviceProxy.open(PbjServerStatusService.ServerStatusMethod.serverStatus, options, replies);
@@ -88,8 +87,7 @@ public void testServerStatus() {
8887

8988
@Test
9089
public void testServerStatusWhenNotRunning() {
91-
final PbjServerStatusServiceProxy serviceProxy =
92-
new PbjServerStatusServiceProxy(serviceStatus, blockReader, metricsService);
90+
final PbjServerStatusServiceProxy serviceProxy = new PbjServerStatusServiceProxy(serviceStatus, metricsService);
9391

9492
Pipeline<? super Bytes> pipeline =
9593
serviceProxy.open(PbjServerStatusService.ServerStatusMethod.serverStatus, options, replies);

0 commit comments

Comments
 (0)