Skip to content

Commit

Permalink
feat: setup metrics in the simulator to report block items sent (#316)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs authored Oct 31, 2024
1 parent 791af49 commit ccff828
Show file tree
Hide file tree
Showing 19 changed files with 458 additions and 47 deletions.
5 changes: 4 additions & 1 deletion server/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ services:
- ./metrics/prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
# Exposing Prometheus outside the stack can create issues with
# the MetricsService in the Simulator. Here it's exposed within
# the stack.
ports:
- "9090:9090"
- "9090"

grafana:
image: grafana/grafana
Expand Down
1 change: 0 additions & 1 deletion simulator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ testModuleInfo {
requires("org.mockito")
requires("org.mockito.junit.jupiter")
requiresStatic("com.github.spotbugs.annotations")
requires("com.swirlds.common")
}

tasks.register<Copy>("untarTestBlockStream") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package com.hedera.block.simulator;

import static java.lang.System.Logger.Level.INFO;
import static java.util.Objects.requireNonNull;

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.SimulatorMode;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.block.simulator.generator.BlockStreamManager;
import com.hedera.block.simulator.grpc.PublishStreamGrpcClient;
import com.hedera.block.simulator.metrics.MetricsService;
import com.hedera.block.simulator.mode.CombinedModeHandler;
import com.hedera.block.simulator.mode.ConsumerModeHandler;
import com.hedera.block.simulator.mode.PublisherModeHandler;
Expand All @@ -37,24 +39,30 @@
public class BlockStreamSimulatorApp {

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

private final PublishStreamGrpcClient publishStreamGrpcClient;
private final SimulatorModeHandler simulatorModeHandler;
private final AtomicBoolean isRunning = new AtomicBoolean(false);
private final MetricsService metricsService;

/**
* Creates a new BlockStreamSimulatorApp instance.
*
* @param configuration the configuration to be used by the block stream simulator
* @param blockStreamManager the block stream manager to be used by the block stream simulator
* @param publishStreamGrpcClient the gRPC client to be used by the block stream simulator
* @param metricsService the metrics service to be used by the block stream simulator
*/
@Inject
public BlockStreamSimulatorApp(
@NonNull Configuration configuration,
@NonNull BlockStreamManager blockStreamManager,
@NonNull PublishStreamGrpcClient publishStreamGrpcClient) {
requireNonNull(blockStreamManager);
@NonNull PublishStreamGrpcClient publishStreamGrpcClient,
@NonNull MetricsService metricsService) {

requireNonNull(configuration);
requireNonNull(blockStreamManager);
this.metricsService = requireNonNull(metricsService);
this.publishStreamGrpcClient = requireNonNull(publishStreamGrpcClient);
final BlockStreamConfig blockStreamConfig =
requireNonNull(configuration.getConfigData(BlockStreamConfig.class));
Expand Down Expand Up @@ -97,6 +105,6 @@ public boolean isRunning() {
public void stop() {
publishStreamGrpcClient.shutdown();
isRunning.set(false);
LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has stopped");
LOGGER.log(INFO, "Block Stream Simulator has stopped");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.hedera.block.simulator.config.ConfigInjectionModule;
import com.hedera.block.simulator.generator.GeneratorInjectionModule;
import com.hedera.block.simulator.grpc.GrpcInjectionModule;
import com.hedera.block.simulator.metrics.MetricsInjectionModule;
import com.swirlds.config.api.Configuration;
import dagger.BindsInstance;
import dagger.Component;
Expand All @@ -28,6 +29,7 @@
@Singleton
@Component(
modules = {
MetricsInjectionModule.class,
ConfigInjectionModule.class,
GeneratorInjectionModule.class,
GrpcInjectionModule.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.hedera.block.simulator.config.data.BlockGeneratorConfig;
import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.data.GrpcConfig;
import com.swirlds.common.metrics.platform.prometheus.PrometheusConfig;
import com.swirlds.config.api.Configuration;
import dagger.Module;
import dagger.Provides;
Expand Down Expand Up @@ -63,4 +64,16 @@ static GrpcConfig provideGrpcConfig(Configuration configuration) {
static BlockGeneratorConfig provideBlockGeneratorConfig(Configuration configuration) {
return configuration.getConfigData(BlockGeneratorConfig.class);
}

/**
* Provides a Prometheus configuration singleton using the configuration.
*
* @param configuration is the configuration singleton
* @return a Prometheus configuration singleton
*/
@Singleton
@Provides
static PrometheusConfig providePrometheusConfig(Configuration configuration) {
return configuration.getConfigData(PrometheusConfig.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.hedera.block.simulator.config.data.BlockGeneratorConfig;
import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.data.GrpcConfig;
import com.swirlds.common.metrics.config.MetricsConfig;
import com.swirlds.common.metrics.platform.prometheus.PrometheusConfig;
import com.swirlds.config.api.ConfigurationExtension;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Set;
Expand All @@ -36,6 +38,11 @@ public SimulatorConfigExtension() {
@NonNull
@Override
public Set<Class<? extends Record>> getConfigDataTypes() {
return Set.of(BlockStreamConfig.class, GrpcConfig.class, BlockGeneratorConfig.class);
return Set.of(
BlockStreamConfig.class,
GrpcConfig.class,
BlockGeneratorConfig.class,
MetricsConfig.class,
PrometheusConfig.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package com.hedera.block.simulator.grpc;

import static com.hedera.block.simulator.metrics.SimulatorMetricTypes.Counter.LiveBlockItemsSent;
import static java.lang.System.Logger.Level.INFO;
import static java.util.Objects.requireNonNull;

import com.hedera.block.common.utils.ChunkUtils;
import com.hedera.block.simulator.Translator;
import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.data.GrpcConfig;
import com.hedera.block.simulator.metrics.MetricsService;
import com.hedera.hapi.block.protoc.BlockStreamServiceGrpc;
import com.hedera.hapi.block.protoc.PublishStreamRequest;
import com.hedera.hapi.block.stream.Block;
Expand All @@ -39,22 +42,29 @@
*/
public class PublishStreamGrpcClientImpl implements PublishStreamGrpcClient {

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

private StreamObserver<PublishStreamRequest> requestStreamObserver;
private final BlockStreamConfig blockStreamConfig;
private final GrpcConfig grpcConfig;
private ManagedChannel channel;
private final MetricsService metricsService;

/**
* Creates a new PublishStreamGrpcClientImpl instance.
*
* @param grpcConfig the gRPC configuration
* @param blockStreamConfig the block stream configuration
* @param metricsService the metrics service
*/
@Inject
public PublishStreamGrpcClientImpl(
@NonNull final GrpcConfig grpcConfig, @NonNull final BlockStreamConfig blockStreamConfig) {
@NonNull final GrpcConfig grpcConfig,
@NonNull final BlockStreamConfig blockStreamConfig,
@NonNull final MetricsService metricsService) {
this.grpcConfig = requireNonNull(grpcConfig);
this.blockStreamConfig = requireNonNull(blockStreamConfig);
this.metricsService = requireNonNull(metricsService);
}

/**
Expand Down Expand Up @@ -85,6 +95,11 @@ public boolean streamBlockItem(List<BlockItem> blockItems) {
requestStreamObserver.onNext(PublishStreamRequest.newBuilder()
.addAllBlockItems(blockItemsProtoc)
.build());
metricsService.get(LiveBlockItemsSent).add(blockItemsProtoc.size());
LOGGER.log(
INFO,
"Total Block items sent: {0}",
metricsService.get(LiveBlockItemsSent).get());

return true;
}
Expand All @@ -106,6 +121,11 @@ public boolean streamBlock(Block block) {
requestStreamObserver.onNext(PublishStreamRequest.newBuilder()
.addAllBlockItems(streamingBatch)
.build());
metricsService.get(LiveBlockItemsSent).add(streamingBatch.size());
LOGGER.log(
INFO,
"Total Block items sent: {0}",
metricsService.get(LiveBlockItemsSent).get());
}

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.simulator.metrics;

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;

/** The module used to inject the metrics service and metrics into the application. */
@Module
public interface MetricsInjectionModule {

/**
* Provides the metrics service.
*
* @param metricsService the metrics service to be used
* @return the metrics service
*/
@Singleton
@Binds
MetricsService bindMetricsService(MetricsServiceImpl metricsService);

/**
* Provides the metrics.
*
* @param configuration the configuration to be used by the metrics
* @return the metrics
*/
@Singleton
@Provides
static Metrics provideMetrics(Configuration configuration) {
final DefaultMetricsProvider metricsProvider = new DefaultMetricsProvider(configuration);
final Metrics metrics = metricsProvider.createGlobalMetrics();
metricsProvider.start();
return metrics;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.simulator.metrics;

import com.swirlds.metrics.api.Counter;
import edu.umd.cs.findbugs.annotations.NonNull;

/** Use member variables of this class to update metric data for the Hedera Block Node. */
public interface MetricsService {
/**
* 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 SimulatorMetricTypes.Counter key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.simulator.metrics;

import com.swirlds.metrics.api.Counter;
import com.swirlds.metrics.api.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.EnumMap;
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_simulator";

private final EnumMap<SimulatorMetricTypes.Counter, Counter> counters =
new EnumMap<>(SimulatorMetricTypes.Counter.class);

/**
* Create singleton instance of metrics service to be used throughout the application.
*
* @param metrics the metrics instance
*/
@Inject
public MetricsServiceImpl(@NonNull final Metrics metrics) {
// Initialize the counters
for (SimulatorMetricTypes.Counter counter : SimulatorMetricTypes.Counter.values()) {
counters.put(
counter,
metrics.getOrCreate(new Counter.Config(CATEGORY, counter.grafanaLabel())
.withDescription(counter.description())));
}
}

/**
* 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 SimulatorMetricTypes.Counter key) {
return counters.get(key);
}
}
Loading

0 comments on commit ccff828

Please sign in to comment.