Skip to content

Commit 2991c6e

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

File tree

7 files changed

+61
-28
lines changed

7 files changed

+61
-28
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: 17 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,16 @@ 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+
119+
}
120+
106121
private ZkEvmWorldState fromWorldStateStorage(WorldStateStorage storage) {
107122
return new ZkEvmWorldState(storage, traceManager);
108123
}

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@
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+
31+
}
32+
2533
class MetricsServiceProvider {
2634
private static final AtomicReference<MetricsService> METRICS_SERVICE = new AtomicReference<>();
2735

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

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

1616
import io.micrometer.core.instrument.MeterRegistry;
17+
import io.micrometer.core.instrument.Tag;
1718
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
1819
import io.vertx.core.Context;
1920
import io.vertx.core.Promise;
2021
import io.vertx.core.Vertx;
2122

23+
import java.util.function.Supplier;
24+
2225
public class NoOpMetricsService implements MetricsService {
2326

2427
private final MeterRegistry meterRegistry;
25-
private Vertx vertx;
2628

2729
public NoOpMetricsService() {
2830
this.meterRegistry = new SimpleMeterRegistry();
@@ -34,22 +36,8 @@ public MeterRegistry getRegistry() {
3436
}
3537

3638
@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 {
53-
// no-op
39+
public void addGauge(final String name, final String description, final Iterable<Tag> tags,
40+
final Supplier<Number> supplier) {
41+
//no-op
5442
}
5543
}

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

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

1414
package net.consensys.shomei.metrics;
1515

16+
import io.micrometer.core.instrument.Gauge;
1617
import io.micrometer.core.instrument.MeterRegistry;
18+
import io.micrometer.core.instrument.Tag;
1719
import io.micrometer.prometheus.PrometheusConfig;
1820
import io.micrometer.prometheus.PrometheusMeterRegistry;
1921
import io.vertx.core.AbstractVerticle;
2022
import io.vertx.core.Promise;
2123
import io.vertx.ext.web.Router;
2224

23-
public class PrometheusMetricsService extends AbstractVerticle implements MetricsService {
25+
import java.util.List;
26+
import java.util.function.Supplier;
27+
28+
public class PrometheusMetricsService extends AbstractVerticle implements
29+
MetricsService.VertxMetricsService {
2430
private final PrometheusMeterRegistry prometheusMeterRegistry;
2531
private final String host;
2632
private final int port;
@@ -36,6 +42,20 @@ public MeterRegistry getRegistry() {
3642
return prometheusMeterRegistry;
3743
}
3844

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import net.consensys.zkevm.HashProvider;
3232

3333
import java.io.IOException;
34+
import java.util.Collections;
3435
import java.util.Optional;
3536

3637
import io.micrometer.core.instrument.MeterRegistry;
@@ -50,7 +51,7 @@ public class Runner {
5051
private final FullSyncDownloader fullSyncDownloader;
5152
private final JsonRpcService jsonRpcService;
5253

53-
private final MetricsService metricsService;
54+
private final MetricsService.VertxMetricsService metricsService;
5455

5556
private final ZkWorldStateArchive worldStateArchive;
5657

@@ -103,9 +104,9 @@ private void setupHashFunction(HashFunctionOption hashFunctionOption) {
103104
HashProvider.setTrieHashFunction(hashFunctionOption.getHashFunction());
104105
}
105106

106-
private MetricsService setupMetrics(MetricsOption metricsOption) {
107+
private MetricsService.VertxMetricsService setupMetrics(MetricsOption metricsOption) {
107108
// use prometheus as metrics service
108-
MetricsService metricsService =
109+
MetricsService.VertxMetricsService metricsService =
109110
new PrometheusMetricsService(
110111
metricsOption.getMetricsHttpHost(), metricsOption.getMetricsHttpPort());
111112
MeterRegistry meterRegistry = metricsService.getRegistry();

0 commit comments

Comments
 (0)