From 83b3eeeb1880479ae8568ee98d443c4459c6756a Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Thu, 20 Jun 2024 19:06:58 +0200 Subject: [PATCH] metrics: enable during import (#2401) This allows monitoring the import process using prometheus/grafana/etc --- nimbus/config.nim | 34 +++++++++++++++++----------------- nimbus/nimbus.nim | 9 ++++----- nimbus/nimbus_import.nim | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/nimbus/config.nim b/nimbus/config.nim index e878c8992..d6e2d091c 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -258,6 +258,23 @@ type defaultValue: 10 name: "log-metrics-interval" .}: int + metricsEnabled* {. + desc: "Enable the built-in metrics HTTP server" + defaultValue: false + name: "metrics" }: bool + + metricsPort* {. + desc: "Listening port of the built-in metrics HTTP server" + defaultValue: defaultMetricsServerPort + defaultValueDesc: $defaultMetricsServerPort + name: "metrics-port" }: Port + + metricsAddress* {. + desc: "Listening IP address of the built-in metrics HTTP server" + defaultValue: defaultAdminListenAddress + defaultValueDesc: $defaultAdminListenAddressDesc + name: "metrics-address" }: IpAddress + bootstrapNodes {. separator: "\pNETWORKING OPTIONS:" desc: "Specifies one or more bootstrap nodes(as enode URL) to use when connecting to the network" @@ -478,23 +495,6 @@ type defaultValueDesc: "\"jwt.hex\" in the data directory (see --data-dir)" name: "jwt-secret" .}: Option[InputFile] - metricsEnabled* {. - desc: "Enable the built-in metrics HTTP server" - defaultValue: false - name: "metrics" }: bool - - metricsPort* {. - desc: "Listening port of the built-in metrics HTTP server" - defaultValue: defaultMetricsServerPort - defaultValueDesc: $defaultMetricsServerPort - name: "metrics-port" }: Port - - metricsAddress* {. - desc: "Listening IP address of the built-in metrics HTTP server" - defaultValue: defaultAdminListenAddress - defaultValueDesc: $defaultAdminListenAddressDesc - name: "metrics-address" }: IpAddress - of `import`: blocksFile* {. argument diff --git a/nimbus/nimbus.nim b/nimbus/nimbus.nim index 50e5df85b..47ab633bc 100644 --- a/nimbus/nimbus.nim +++ b/nimbus/nimbus.nim @@ -174,8 +174,7 @@ proc setupP2P(nimbus: NimbusNode, conf: NimbusConf, waitForPeers = waitForPeers) -proc localServices(nimbus: NimbusNode, conf: NimbusConf, - com: CommonRef, protocols: set[ProtocolFlag]) = +proc setupMetrics(nimbus: NimbusNode, conf: NimbusConf) = # metrics logging if conf.logMetricsEnabled: # https://github.com/nim-lang/Nim/issues/17369 @@ -187,8 +186,6 @@ proc localServices(nimbus: NimbusNode, conf: NimbusConf, discard setTimer(Moment.fromNow(conf.logMetricsInterval.seconds), logMetrics) discard setTimer(Moment.fromNow(conf.logMetricsInterval.seconds), logMetrics) - nimbus.setupRpc(conf, com, protocols) - # metrics server if conf.metricsEnabled: info "Starting metrics HTTP server", address = conf.metricsAddress, port = conf.metricsPort @@ -235,6 +232,8 @@ proc run(nimbus: NimbusNode, conf: NimbusConf) = of Aristo,AriPrune: AristoDbRocks.newCoreDbRef(string conf.dataDir, conf.dbOptions()) + setupMetrics(nimbus, conf) + let com = CommonRef.new( db = coreDB, pruneHistory = (conf.chainDbMode == AriPrune), @@ -255,7 +254,7 @@ proc run(nimbus: NimbusNode, conf: NimbusConf) = basicServices(nimbus, conf, com) manageAccounts(nimbus, conf) setupP2P(nimbus, conf, com, protocols) - localServices(nimbus, conf, com, protocols) + setupRpc(nimbus, conf, com, protocols) if conf.maxPeers > 0: case conf.syncMode: diff --git a/nimbus/nimbus_import.nim b/nimbus/nimbus_import.nim index d91324a35..92fb3c65a 100644 --- a/nimbus/nimbus_import.nim +++ b/nimbus/nimbus_import.nim @@ -11,6 +11,7 @@ import chronicles, + metrics, chronos/timer, std/[strformat, strutils], stew/io2, @@ -20,6 +21,15 @@ import ./db/era1_db, beacon_chain/era_db +declareCounter nec_imported_blocks, + "Blocks processed during import" + +declareCounter nec_imported_transactions, + "Transactions processed during import" + +declareCounter nec_imported_gas, + "Gas processed during import" + var running {.volatile.} = true func shortLog(a: timer.Duration, parts = int.high): string {.inline.} = @@ -145,6 +155,10 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) = avgMGps = f(gas.float / 1000000 / diff0), elapsed = shortLog(time2 - time0, 3) + nec_imported_blocks.inc(blocks.len) + nec_imported_transactions.inc(statsRes[].txs) + nec_imported_gas.inc(statsRes[].gas) + if csv != nil: # In the CSV, we store a line for every chunk of blocks processed so # that the file can meaningfully be appended to when restarting the