2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2022-07-17 15:16:57 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import chronicles, chronos, metrics, metrics/chronos_httpserver
|
2022-10-18 17:35:26 +00:00
|
|
|
import
|
2023-09-11 06:32:31 +00:00
|
|
|
../waku_rln_relay/protocol_metrics as rln_metrics,
|
2022-10-21 08:33:36 +00:00
|
|
|
../utils/collector,
|
2023-02-06 09:03:30 +00:00
|
|
|
./peer_manager,
|
2024-04-30 13:07:17 +00:00
|
|
|
./waku_node,
|
|
|
|
../factory/external_config
|
2022-10-18 17:35:26 +00:00
|
|
|
|
2024-02-19 21:25:20 +00:00
|
|
|
const LogInterval = 10.minutes
|
2022-07-17 15:16:57 +00:00
|
|
|
|
|
|
|
logScope:
|
2022-11-03 15:36:24 +00:00
|
|
|
topics = "waku node metrics"
|
2022-07-17 15:16:57 +00:00
|
|
|
|
|
|
|
proc startMetricsLog*() =
|
2023-09-18 16:51:49 +00:00
|
|
|
var logMetrics: CallbackFunc
|
2022-07-17 15:16:57 +00:00
|
|
|
|
2022-08-31 09:53:28 +00:00
|
|
|
var cumulativeErrors = 0.float64
|
|
|
|
var cumulativeConns = 0.float64
|
|
|
|
|
2023-09-11 06:32:31 +00:00
|
|
|
let logRlnMetrics = getRlnMetricsLogger()
|
2022-10-18 11:37:44 +00:00
|
|
|
|
2023-09-18 16:51:49 +00:00
|
|
|
logMetrics = CallbackFunc(
|
|
|
|
proc(udata: pointer) {.gcsafe.} =
|
2022-07-17 15:16:57 +00:00
|
|
|
# TODO: libp2p_pubsub_peers is not public, so we need to make this either
|
|
|
|
# public in libp2p or do our own peer counting after all.
|
2022-08-31 09:53:28 +00:00
|
|
|
|
2022-10-18 11:37:44 +00:00
|
|
|
# track cumulative values
|
|
|
|
let freshErrorCount = parseAndAccumulate(waku_node_errors, cumulativeErrors)
|
2024-03-15 23:08:47 +00:00
|
|
|
let freshConnCount =
|
|
|
|
parseAndAccumulate(waku_node_conns_initiated, cumulativeConns)
|
2022-10-21 08:33:36 +00:00
|
|
|
|
2022-12-07 11:30:32 +00:00
|
|
|
let totalMessages = collectorAsF64(waku_node_messages)
|
|
|
|
let storePeers = collectorAsF64(waku_store_peers)
|
|
|
|
let pxPeers = collectorAsF64(waku_px_peers)
|
|
|
|
let lightpushPeers = collectorAsF64(waku_lightpush_peers)
|
|
|
|
let filterPeers = collectorAsF64(waku_filter_peers)
|
|
|
|
|
|
|
|
info "Total connections initiated", count = $freshConnCount
|
|
|
|
info "Total messages", count = totalMessages
|
|
|
|
info "Total store peers", count = storePeers
|
|
|
|
info "Total peer exchange peers", count = pxPeers
|
|
|
|
info "Total lightpush peers", count = lightpushPeers
|
|
|
|
info "Total filter peers", count = filterPeers
|
|
|
|
info "Total errors", count = $freshErrorCount
|
2022-08-31 09:53:28 +00:00
|
|
|
|
2022-10-18 11:37:44 +00:00
|
|
|
# Start protocol specific metrics logging
|
2023-09-11 06:32:31 +00:00
|
|
|
logRlnMetrics()
|
2022-10-18 11:37:44 +00:00
|
|
|
|
2023-09-18 16:51:49 +00:00
|
|
|
discard setTimer(Moment.fromNow(LogInterval), logMetrics)
|
|
|
|
)
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2022-10-21 08:33:36 +00:00
|
|
|
discard setTimer(Moment.fromNow(LogInterval), logMetrics)
|
2024-04-30 13:07:17 +00:00
|
|
|
|
|
|
|
proc startMetricsServer(
|
|
|
|
serverIp: IpAddress, serverPort: Port
|
|
|
|
): Result[MetricsHttpServerRef, string] =
|
|
|
|
info "Starting metrics HTTP server", serverIp = $serverIp, serverPort = $serverPort
|
|
|
|
|
|
|
|
let server = MetricsHttpServerRef.new($serverIp, serverPort).valueOr:
|
|
|
|
return err("metrics HTTP server start failed: " & $error)
|
|
|
|
|
|
|
|
try:
|
|
|
|
waitFor server.start()
|
|
|
|
except CatchableError:
|
|
|
|
return err("metrics HTTP server start failed: " & getCurrentExceptionMsg())
|
|
|
|
|
|
|
|
info "Metrics HTTP server started", serverIp = $serverIp, serverPort = $serverPort
|
|
|
|
return ok(server)
|
|
|
|
|
|
|
|
proc startMetricsServerAndLogging*(
|
|
|
|
conf: WakuNodeConf
|
|
|
|
): Result[MetricsHttpServerRef, string] =
|
|
|
|
var metricsServer: MetricsHttpServerRef
|
|
|
|
if conf.metricsServer:
|
|
|
|
metricsServer = startMetricsServer(
|
|
|
|
conf.metricsServerAddress, Port(conf.metricsServerPort + conf.portsShift)
|
|
|
|
).valueOr:
|
|
|
|
return
|
|
|
|
err("Starting metrics server failed. Continuing in current state:" & $error)
|
|
|
|
|
|
|
|
if conf.metricsLogging:
|
|
|
|
startMetricsLog()
|
|
|
|
|
|
|
|
return ok(metricsServer)
|