Skip to content

Commit

Permalink
feat: Add metrics base (#84)
Browse files Browse the repository at this point in the history
Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 authored Aug 8, 2024
1 parent 5161410 commit 1a98d20
Show file tree
Hide file tree
Showing 16 changed files with 428 additions and 2 deletions.
22 changes: 22 additions & 0 deletions buildSrc/src/main/kotlin/com.hedera.block.jpms-modules.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ jvmDependencyConflicts.patch {
}
module("io.grpc:grpc-stub") { annotationLibraries.forEach { removeDependency(it) } }
module("io.grpc:grpc-util") { annotationLibraries.forEach { removeDependency(it) } }
// Added for metrics and logging, but also several platform classes
module("io.prometheus:simpleclient") {
removeDependency("io.prometheus:simpleclient_tracer_otel")
removeDependency("io.prometheus:simpleclient_tracer_otel_agent")
}
}

// Fix or enhance the 'module-info.class' of third-party Modules. This is about the
Expand Down Expand Up @@ -95,4 +100,21 @@ extraJavaModuleInfo {
// spotbugs
module("com.github.spotbugs:spotbugs-annotations", "com.github.spotbugs.annotations")
module("com.google.code.findbugs:jsr305", "java.annotation") { exportAllPackages() }

// needed for metrics and logging, but also several platform classes
module("com.goterl:resource-loader", "resource.loader")
module("com.goterl:lazysodium-java", "lazysodium.java")
module("org.hyperledger.besu:secp256k1", "org.hyperledger.besu.nativelib.secp256k1")
module("io.prometheus:simpleclient", "io.prometheus.simpleclient")
module("io.prometheus:simpleclient_common", "io.prometheus.simpleclient_common")
module("io.prometheus:simpleclient_httpserver", "io.prometheus.simpleclient.httpserver") {
exportAllPackages()
requireAllDefinedDependencies()
requires("jdk.httpserver")
}

// Annotation processing only
module("com.google.auto.service:auto-service-annotations", "com.google.auto.service")
module("com.google.auto.service:auto-service", "com.google.auto.service.processor")
module("com.google.auto:auto-common", "com.google.auto.common")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
* limitations under the License.
*/

repositories { mavenCentral() }
repositories {
mavenCentral()
maven {
url = uri("https://hyperledger.jfrog.io/artifactory/besu-maven")
content { includeGroupByRegex("org\\.hyperledger\\..*") }
}
}
10 changes: 9 additions & 1 deletion gradle/modules.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
com.github.spotbugs.annotations=com.github.spotbugs:spotbugs-annotations
com.swirlds.metrics.api=com.swirlds:swirlds-metrics-api
com.swirlds.metrics.impl=com.swirlds:swirlds-metrics-impl
com.swirlds.common=com.swirlds:swirlds-common
com.swirlds.config.impl=com.swirlds:swirlds-config-impl
com.swirlds.config.extensions=com.swirlds:swirlds-config-extensions
com.swirlds.config.processor=com.swirlds:swirlds-config-processor
com.google.auto.service=com.google.auto.service:auto-service-annotations
com.google.auto.service.processor=com.google.auto.service:auto-service
com.google.auto.common=com.google.auto:auto-common
io.helidon.webserver=io.helidon.webserver:helidon-webserver
io.helidon.webserver.grpc=io.helidon.webserver:helidon-webserver-grpc
io.helidon.webserver.testing.junit5=io.helidon.webserver.testing.junit5:helidon-webserver-testing-junit5
Expand Down
5 changes: 5 additions & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ application {
mainClass = "com.hedera.block.server.Server"
}

mainModuleInfo {
annotationProcessor("com.google.auto.service.processor")
runtimeOnly("com.swirlds.config.impl")
}

testModuleInfo {
requires("org.junit.jupiter.api")
requires("org.mockito")
Expand Down
37 changes: 37 additions & 0 deletions server/docs/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Metrics
This document describes the metrics that are available in the system, its purpose, and how to use them.

## Overview
We are using Prometheus to collect metrics from the system, through the use of [Hedera Platform SDK - Metrics](https://github.com/hashgraph/hedera-services/blob/develop/platform-sdk/docs/base/metrics/metrics.md).


## Purpose

The purpose of metrics is to provide a way to measure the performance of the system. Metrics are used to monitor the system and to detect any issues that may arise. Metrics can be used to identify bottlenecks, track the performance of the system over time, and to help diagnose problems.

## Configuration

### Prometheus

Prefix: prometheus, ie. `prometheus.configKey`

| ConfigKey | Description | Default |
|----------------------------|---------------------------------------------------------------------------------------|-------------|
| enableEndpoint | either `true` or `false`. Enables or disables the endpoint for metrics | true |
| endpointPortNumber | Port of the Prometheus endpoint | 9999 |
| endpointMaxBacklogAllowed | The maximum number of incoming TCP connections which the system will queue internally | 1 |

## Usage
All classes that need to observe metrics can get them from the BlockNodeContext.

If a new metric is needed, it can be added to MetricsService, and then used in the classes that need it. This is to avoid having to define the metrics in several places.
MetricsService centralizes the creation of metrics and provides a way to access them from any other class.

To check the metrics you can access the Prometheus endpoint at `http://localhost:9999/metrics`.

## Existing Metrics

| Metric Name | Description | Type |
|----------------|---------------------------|---------|
| exampleGauge | An example gauge metric | Gauge |
| exampleCounter | An example counter metric | Counter |
9 changes: 9 additions & 0 deletions server/src/main/java/com/hedera/block/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import static com.hedera.block.server.Constants.*;

import com.hedera.block.protos.BlockStreamServiceGrpcProto;
import com.hedera.block.server.config.BlockNodeContext;
import com.hedera.block.server.config.BlockNodeContextFactory;
import com.hedera.block.server.mediator.LiveStreamMediatorImpl;
import com.hedera.block.server.metrics.MetricsService;
import com.hedera.block.server.persistence.WriteThroughCacheHandler;
import com.hedera.block.server.persistence.storage.BlockStorage;
import com.hedera.block.server.persistence.storage.FileSystemBlockStorage;
Expand Down Expand Up @@ -57,6 +60,12 @@ private Server() {}
public static void main(final String[] args) {

try {
// init metrics
BlockNodeContext blockNodeContext = BlockNodeContextFactory.create();

// increase by 1 just for the sake of an example
MetricsService metricsService = blockNodeContext.metricsService();
metricsService.exampleCounter.increment();

// Set the global configuration
final Config config = Config.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.config;

import com.google.auto.service.AutoService;
import com.swirlds.common.config.BasicCommonConfig;
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. */
@AutoService(ConfigurationExtension.class)
public class BlockNodeConfigExtension implements ConfigurationExtension {

/**
* {@inheritDoc}
*
* @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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.config;

import com.hedera.block.server.metrics.MetricsService;
import com.swirlds.config.api.Configuration;
import com.swirlds.metrics.api.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Context for the block node. This record is returned by the BlockNodeContextFactory when a new
* configuration is created.
*
* @param metrics the metrics used for monitoring and reporting
* @param metricsService the service responsible for handling metrics
* @param configuration the configuration settings for the block node
*/
public record BlockNodeContext(
@NonNull Metrics metrics,
@NonNull MetricsService metricsService,
@NonNull Configuration configuration) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.config;

import com.hedera.block.server.metrics.MetricsService;
import com.swirlds.common.metrics.platform.DefaultMetricsProvider;
import com.swirlds.config.api.Configuration;
import com.swirlds.config.api.ConfigurationBuilder;
import com.swirlds.config.extensions.sources.ClasspathFileConfigSource;
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 String APPLICATION_PROPERTIES_1 = "app.properties";

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() 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() throws IOException {

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

@NonNull
private static Metrics getMetrics(@NonNull final 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,49 @@
/*
* 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;

public class MetricsService {

private static final String CATEGORY = "app";

private static final LongGauge.Config EXAMPLE_GAUGE =
new LongGauge.Config(CATEGORY, "exampleGauge").withDescription("An example gauge");

/** An example gauge. */
public final LongGauge exampleGauge;

private static final Counter.Config EXAMPLE_COUNTER =
new Counter.Config(CATEGORY, "exampleCounter").withDescription("An example counter");

/** An example counter. */
public final Counter exampleCounter;

/**
* Creates a new instance of {@link MetricsService}.
*
* @param metrics the metrics instance
*/
public MetricsService(@NonNull final Metrics metrics) {
this.exampleGauge = metrics.getOrCreate(EXAMPLE_GAUGE);
this.exampleCounter = metrics.getOrCreate(EXAMPLE_COUNTER);
}
}
10 changes: 10 additions & 0 deletions server/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import com.hedera.block.server.config.BlockNodeConfigExtension;

/** Runtime module of the server. */
module com.hedera.block.server {
requires com.hedera.block.protos;
requires com.google.protobuf;
requires com.swirlds.common;
requires com.swirlds.config.api;
requires com.swirlds.config.extensions;
requires com.swirlds.metrics.api;
requires io.grpc.stub;
requires io.helidon.common;
requires io.helidon.config;
requires io.helidon.webserver.grpc;
requires io.helidon.webserver;
requires static com.github.spotbugs.annotations;
requires static com.google.auto.service;

provides com.swirlds.config.api.ConfigurationExtension with
BlockNodeConfigExtension;
}
1 change: 1 addition & 0 deletions server/src/main/resources/app.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prometheus.endpointPortNumber=9999
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.config;

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

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

class BlockNodeContextFactoryTest {

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

assertNotNull(context.metrics());
assertNotNull(context.configuration());
}
}
Loading

0 comments on commit 1a98d20

Please sign in to comment.