Skip to content

Commit dad764d

Browse files
committed
add head metrics gauge
Signed-off-by: garyschulte <[email protected]>
1 parent 44b9d79 commit dad764d

File tree

8 files changed

+62
-32
lines changed

8 files changed

+62
-32
lines changed

core/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
implementation project(':crypto')
2121
implementation project(':services:storage:api')
2222
implementation project(':services:storage:rocksdb')
23+
implementation project(':services:metrics')
2324

2425
implementation 'org.hyperledger.besu.internal:trie'
2526
implementation 'org.hyperledger.besu.internal:core'
@@ -40,4 +41,4 @@ dependencies {
4041
testImplementation 'junit:junit'
4142
testImplementation 'org.assertj:assertj-core'
4243

43-
}
44+
}

core/src/main/java/net/consensys/shomei/storage/ZkWorldStateArchive.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package net.consensys.shomei.storage;
1515

1616
import net.consensys.shomei.exception.MissingTrieLogException;
17+
import net.consensys.shomei.metrics.MetricsService;
1718
import net.consensys.shomei.observer.TrieLogObserver.TrieLogIdentifier;
1819
import net.consensys.shomei.storage.worldstate.WorldStateStorage;
1920
import net.consensys.shomei.trielog.TrieLogLayer;
@@ -22,6 +23,7 @@
2223

2324
import java.io.Closeable;
2425
import java.io.IOException;
26+
import java.util.Collections;
2527
import java.util.Comparator;
2628
import java.util.Map;
2729
import java.util.Optional;
@@ -56,14 +58,16 @@ public ZkWorldStateArchive(
5658
storageProvider.getTrieLogManager(),
5759
storageProvider.getTraceManager(),
5860
storageProvider.getWorldStateStorage(),
59-
enableFinalizedBlockLimit);
61+
enableFinalizedBlockLimit,
62+
MetricsService.MetricsServiceProvider.getMetricsService());
6063
}
6164

6265
public ZkWorldStateArchive(
6366
final TrieLogManager trieLogManager,
6467
final TraceManager traceManager,
6568
final WorldStateStorage headWorldStateStorage,
66-
final boolean enableFinalizedBlockLimit) {
69+
final boolean enableFinalizedBlockLimit,
70+
final MetricsService metricsService) {
6771
this.trieLogManager = trieLogManager;
6872
this.traceManager = traceManager;
6973
this.headWorldStateStorage = headWorldStateStorage;
@@ -75,6 +79,7 @@ public ZkWorldStateArchive(
7579
headWorldStateStorage);
7680
LOG.debug("Worldstate archive created snapshot for " + headWorldState.getBlockNumber());
7781
}
82+
setupHeadMetrics(metricsService);
7883
}
7984

8085
public Optional<ZkEvmWorldState> getCachedWorldState(Hash blockHash) {
@@ -103,6 +108,14 @@ Map<TrieLogIdentifier, WorldStateStorage> getCachedWorldStates() {
103108
return cachedWorldStates;
104109
}
105110

111+
private void setupHeadMetrics(MetricsService metricsService) {
112+
metricsService.addGauge(
113+
"shomei_blockchain_head",
114+
"current chain head block number which corresponds to shomei state",
115+
Collections.emptyList(),
116+
this::getCurrentBlockNumber);
117+
}
118+
106119
private ZkEvmWorldState fromWorldStateStorage(WorldStateStorage storage) {
107120
return new ZkEvmWorldState(storage, traceManager);
108121
}

core/src/test/java/net/consensys/shomei/storage/ZkWorldStateArchiveTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import static org.assertj.core.api.Assertions.assertThat;
1717

1818
import net.consensys.shomei.exception.MissingTrieLogException;
19+
import net.consensys.shomei.metrics.MetricsService;
20+
import net.consensys.shomei.metrics.NoOpMetricsService;
1921
import net.consensys.shomei.observer.TrieLogObserver.TrieLogIdentifier;
2022
import net.consensys.shomei.trielog.PluginTrieLogLayer;
2123
import net.consensys.shomei.trielog.TrieLogLayerConverter;
@@ -33,6 +35,7 @@ public class ZkWorldStateArchiveTests {
3335
ZkWorldStateArchive archive = new ZkWorldStateArchive(new InMemoryStorageProvider());
3436
TrieLogLayerConverter converter = new TrieLogLayerConverter(archive.getHeadWorldStateStorage());
3537
ZkTrieLogFactory encoder = new ZkTrieLogFactory();
38+
MetricsService noopMetrics = new NoOpMetricsService();
3639

3740
@Test
3841
public void shouldDropWorldStatesFromHead() {
@@ -108,7 +111,7 @@ public void verifyStorageSnapshotAtStart() throws MissingTrieLogException {
108111
archive.getTrieLogManager(),
109112
archive.getTraceManager(),
110113
archive.getHeadWorldStateStorage(),
111-
false);
114+
false, noopMetrics);
112115

113116
assertThat(zkWorldStateArchive.getCachedWorldState(0L).isPresent()).isFalse();
114117

@@ -117,7 +120,7 @@ public void verifyStorageSnapshotAtStart() throws MissingTrieLogException {
117120
archive.getTrieLogManager(),
118121
archive.getTraceManager(),
119122
archive.getHeadWorldStateStorage(),
120-
true);
123+
true, noopMetrics);
121124

122125
assertThat(zkWorldStateArchive.getCachedWorldState(0L).isPresent()).isTrue();
123126
}

services/metrics/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ dependencies {
2020
implementation 'io.vertx:vertx-core'
2121
implementation 'io.vertx:vertx-web'
2222
implementation 'io.vertx:vertx-micrometer-metrics'
23-
implementation 'io.micrometer:micrometer-core'
2423
implementation 'io.micrometer:micrometer-registry-prometheus'
24+
api 'io.micrometer:micrometer-core'
2525

2626

2727
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
@@ -31,4 +31,4 @@ dependencies {
3131

3232
test {
3333
useJUnitPlatform()
34-
}
34+
}

services/metrics/src/main/java/net/consensys/shomei/metrics/MetricsService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@
1414
package net.consensys.shomei.metrics;
1515

1616
import java.util.concurrent.atomic.AtomicReference;
17+
import java.util.function.Supplier;
1718

1819
import io.micrometer.core.instrument.MeterRegistry;
20+
import io.micrometer.core.instrument.Tag;
1921
import io.vertx.core.Verticle;
2022

21-
public interface MetricsService extends Verticle {
23+
public interface MetricsService {
2224

2325
MeterRegistry getRegistry();
2426

27+
void addGauge(String name, String description, Iterable<Tag> tags, Supplier<Number> supplier);
28+
29+
interface VertxMetricsService extends MetricsService, Verticle {}
30+
2531
class MetricsServiceProvider {
2632
private static final AtomicReference<MetricsService> METRICS_SERVICE = new AtomicReference<>();
2733

services/metrics/src/main/java/net/consensys/shomei/metrics/NoOpMetricsService.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313

1414
package net.consensys.shomei.metrics;
1515

16+
import java.util.function.Supplier;
17+
1618
import io.micrometer.core.instrument.MeterRegistry;
19+
import io.micrometer.core.instrument.Tag;
1720
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
18-
import io.vertx.core.Context;
19-
import io.vertx.core.Promise;
20-
import io.vertx.core.Vertx;
2121

2222
public class NoOpMetricsService implements MetricsService {
2323

2424
private final MeterRegistry meterRegistry;
25-
private Vertx vertx;
2625

2726
public NoOpMetricsService() {
2827
this.meterRegistry = new SimpleMeterRegistry();
@@ -34,22 +33,11 @@ public MeterRegistry getRegistry() {
3433
}
3534

3635
@Override
37-
public Vertx getVertx() {
38-
return vertx;
39-
}
40-
41-
@Override
42-
public void init(final Vertx vertx, final Context context) {
43-
this.vertx = vertx;
44-
}
45-
46-
@Override
47-
public void start(final Promise<Void> startPromise) throws Exception {
48-
// no-op
49-
}
50-
51-
@Override
52-
public void stop(final Promise<Void> stopPromise) throws Exception {
36+
public void addGauge(
37+
final String name,
38+
final String description,
39+
final Iterable<Tag> tags,
40+
final Supplier<Number> supplier) {
5341
// no-op
5442
}
5543
}

services/metrics/src/main/java/net/consensys/shomei/metrics/PrometheusMetricsService.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313

1414
package net.consensys.shomei.metrics;
1515

16+
import java.util.function.Supplier;
17+
18+
import io.micrometer.core.instrument.Gauge;
1619
import io.micrometer.core.instrument.MeterRegistry;
20+
import io.micrometer.core.instrument.Tag;
1721
import io.micrometer.prometheus.PrometheusConfig;
1822
import io.micrometer.prometheus.PrometheusMeterRegistry;
1923
import io.vertx.core.AbstractVerticle;
2024
import io.vertx.core.Promise;
2125
import io.vertx.ext.web.Router;
2226

23-
public class PrometheusMetricsService extends AbstractVerticle implements MetricsService {
27+
public class PrometheusMetricsService extends AbstractVerticle
28+
implements MetricsService.VertxMetricsService {
2429
private final PrometheusMeterRegistry prometheusMeterRegistry;
2530
private final String host;
2631
private final int port;
@@ -36,6 +41,20 @@ public MeterRegistry getRegistry() {
3641
return prometheusMeterRegistry;
3742
}
3843

44+
@Override
45+
public void addGauge(
46+
final String name,
47+
final String description,
48+
final Iterable<Tag> tags,
49+
final Supplier<Number> supplier) {
50+
51+
// Register a gauge to track this value
52+
Gauge.builder(name, supplier)
53+
.description(description)
54+
.tags(tags)
55+
.register(prometheusMeterRegistry);
56+
}
57+
3958
@Override
4059
public void start(Promise<Void> startPromise) {
4160

shomei/src/main/java/net/consensys/shomei/Runner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class Runner {
5050
private final FullSyncDownloader fullSyncDownloader;
5151
private final JsonRpcService jsonRpcService;
5252

53-
private final MetricsService metricsService;
53+
private final MetricsService.VertxMetricsService metricsService;
5454

5555
private final ZkWorldStateArchive worldStateArchive;
5656

@@ -103,9 +103,9 @@ private void setupHashFunction(HashFunctionOption hashFunctionOption) {
103103
HashProvider.setTrieHashFunction(hashFunctionOption.getHashFunction());
104104
}
105105

106-
private MetricsService setupMetrics(MetricsOption metricsOption) {
106+
private MetricsService.VertxMetricsService setupMetrics(MetricsOption metricsOption) {
107107
// use prometheus as metrics service
108-
MetricsService metricsService =
108+
MetricsService.VertxMetricsService metricsService =
109109
new PrometheusMetricsService(
110110
metricsOption.getMetricsHttpHost(), metricsOption.getMetricsHttpPort());
111111
MeterRegistry meterRegistry = metricsService.getRegistry();

0 commit comments

Comments
 (0)