Skip to content

Commit

Permalink
more PR Review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 committed Aug 7, 2024
1 parent b29bca6 commit 4c6ccd2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;

/** Registers configuration types for the server. */
Expand All @@ -32,6 +33,7 @@ public class BlockNodeConfigExtension implements ConfigurationExtension {
*
* @return Set of configuration data types for the server
*/
@NonNull
@Override
public Set<Class<? extends Record>> getConfigDataTypes() {
return Set.of(BasicCommonConfig.class, MetricsConfig.class, PrometheusConfig.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource;
import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource;
import com.swirlds.metrics.api.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.nio.file.Path;

/** Static factory that creates {@link BlockNodeContext} */
public class BlockNodeContextFactory {
private static final System.Logger logger =
System.getLogger(BlockNodeContextFactory.class.getName());

private static final String APPLICATION_PROPERTIES_1 = "app.properties";

Expand All @@ -40,28 +39,25 @@ private BlockNodeContextFactory() {}
* @return an instance of {@link BlockNodeContext} which holds {@link Configuration}, {@link
* Metrics} and {@link MetricsService} for the rest of the application to use.
*/
public static BlockNodeContext create() {
public static BlockNodeContext create() throws IOException {
final Configuration configuration = getConfiguration();
final Metrics metrics = getMetrics(configuration);
final MetricsService metricsService = new MetricsService(metrics);
return new BlockNodeContext(metrics, metricsService, configuration);
}

private static Configuration getConfiguration() {
try {
return ConfigurationBuilder.create()
.withSource(SystemEnvironmentConfigSource.getInstance())
.withSource(SystemPropertiesConfigSource.getInstance())
.withSource(new ClasspathFileConfigSource(Path.of(APPLICATION_PROPERTIES_1)))
.autoDiscoverExtensions()
.build();
} catch (IOException e) {
logger.log(System.Logger.Level.ERROR, "Error reading configuration", e);
throw new RuntimeException("Error reading configuration", e);
}
private static Configuration getConfiguration() throws IOException {

return ConfigurationBuilder.create()
.withSource(SystemEnvironmentConfigSource.getInstance())
.withSource(SystemPropertiesConfigSource.getInstance())
.withSource(new ClasspathFileConfigSource(Path.of(APPLICATION_PROPERTIES_1)))
.autoDiscoverExtensions()
.build();
}

private static Metrics getMetrics(final Configuration configuration) {
@NonNull
private static Metrics getMetrics(@NonNull final Configuration configuration) {
final DefaultMetricsProvider metricsProvider = new DefaultMetricsProvider(configuration);
final Metrics metrics = metricsProvider.createGlobalMetrics();
metricsProvider.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;

public class MetricsService {

Expand All @@ -34,7 +35,7 @@ public class MetricsService {

public final Counter exampleCounter;

public MetricsService(final Metrics metrics) {
public MetricsService(@NonNull final Metrics metrics) {
this.exampleGauge = metrics.getOrCreate(EXAMPLE_GAUGE);
this.exampleCounter = metrics.getOrCreate(EXAMPLE_COUNTER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import org.junit.jupiter.api.Test;

class BlockNodeContextFactoryTest {

@Test
void create_returnsBlockNodeContext() {
void create_returnsBlockNodeContext() throws IOException {
BlockNodeContext context = BlockNodeContextFactory.create();

assertNotNull(context.metrics());
Expand Down

0 comments on commit 4c6ccd2

Please sign in to comment.