mirror of https://github.com/waku-org/nwaku.git
refactor: metrics server. Simplify app.nim module (#2650)
This commit is contained in:
parent
6382dedb42
commit
4a110f65ff
|
@ -20,6 +20,7 @@ import
|
|||
../../waku/factory/networks_config,
|
||||
../../waku/factory/app,
|
||||
../../waku/node/health_monitor,
|
||||
../../waku/node/waku_metrics,
|
||||
../../waku/waku_api/rest/builder as rest_server_builder
|
||||
|
||||
logScope:
|
||||
|
@ -152,7 +153,7 @@ when isMainModule:
|
|||
error "Starting protocols support REST server failed.", error = $error
|
||||
quit(QuitFailure)
|
||||
|
||||
wakunode2.startMetricsServerAndLogging().isOkOr:
|
||||
wakunode2.metricsServer = waku_metrics.startMetricsServerAndLogging(conf).valueOr:
|
||||
error "Starting monitoring and external interfaces failed", error = error
|
||||
quit(QuitFailure)
|
||||
|
||||
|
|
|
@ -51,8 +51,8 @@ suite "Wakunode2 - App initialization":
|
|||
wakunode2.startApp().isOkOr:
|
||||
raiseAssert error
|
||||
|
||||
let mountRes = wakunode2.startMetricsServerAndLogging()
|
||||
assert mountRes.isOk(), mountRes.error
|
||||
wakunode2.metricsServer = waku_metrics.startMetricsServerAndLogging(conf).valueOr:
|
||||
raiseAssert error
|
||||
|
||||
## Then
|
||||
let node = wakunode2.node
|
||||
|
|
|
@ -65,7 +65,7 @@ type
|
|||
node: WakuNode
|
||||
|
||||
restServer*: WakuRestServerRef
|
||||
metricsServer: Option[MetricsHttpServerRef]
|
||||
metricsServer*: MetricsHttpServerRef
|
||||
|
||||
AppResult*[T] = Result[T, string]
|
||||
|
||||
|
@ -273,56 +273,14 @@ proc startApp*(app: var App): AppResult[void] =
|
|||
|
||||
return ok()
|
||||
|
||||
proc startMetricsServer(
|
||||
serverIp: IpAddress, serverPort: Port
|
||||
): AppResult[MetricsHttpServerRef] =
|
||||
info "Starting metrics HTTP server", serverIp = $serverIp, serverPort = $serverPort
|
||||
|
||||
let metricsServerRes = MetricsHttpServerRef.new($serverIp, serverPort)
|
||||
if metricsServerRes.isErr():
|
||||
return err("metrics HTTP server start failed: " & $metricsServerRes.error)
|
||||
|
||||
let server = metricsServerRes.value
|
||||
try:
|
||||
waitFor server.start()
|
||||
except CatchableError:
|
||||
return err("metrics HTTP server start failed: " & getCurrentExceptionMsg())
|
||||
|
||||
info "Metrics HTTP server started", serverIp = $serverIp, serverPort = $serverPort
|
||||
ok(server)
|
||||
|
||||
proc startMetricsLogging(): AppResult[void] =
|
||||
startMetricsLog()
|
||||
ok()
|
||||
|
||||
proc startMetricsServerAndLogging*(app: var App): AppResult[void] =
|
||||
if app.conf.metricsServer:
|
||||
let startMetricsServerRes = startMetricsServer(
|
||||
app.conf.metricsServerAddress,
|
||||
Port(app.conf.metricsServerPort + app.conf.portsShift),
|
||||
)
|
||||
if startMetricsServerRes.isErr():
|
||||
error "Starting metrics server failed. Continuing in current state.",
|
||||
error = startMetricsServerRes.error
|
||||
else:
|
||||
app.metricsServer = some(startMetricsServerRes.value)
|
||||
|
||||
if app.conf.metricsLogging:
|
||||
let startMetricsLoggingRes = startMetricsLogging()
|
||||
if startMetricsLoggingRes.isErr():
|
||||
error "Starting metrics console logging failed. Continuing in current state.",
|
||||
error = startMetricsLoggingRes.error
|
||||
|
||||
ok()
|
||||
|
||||
# App shutdown
|
||||
|
||||
proc stop*(app: App): Future[void] {.async: (raises: [Exception]).} =
|
||||
if app.conf.rest:
|
||||
if not app.restServer.isNil():
|
||||
await app.restServer.stop()
|
||||
|
||||
if app.metricsServer.isSome():
|
||||
await app.metricsServer.get().stop()
|
||||
if not app.metricsServer.isNil():
|
||||
await app.metricsServer.stop()
|
||||
|
||||
if app.wakuDiscv5.isSome():
|
||||
await app.wakuDiscv5.get().stop()
|
||||
|
|
|
@ -8,7 +8,8 @@ import
|
|||
../waku_rln_relay/protocol_metrics as rln_metrics,
|
||||
../utils/collector,
|
||||
./peer_manager,
|
||||
./waku_node
|
||||
./waku_node,
|
||||
../factory/external_config
|
||||
|
||||
const LogInterval = 10.minutes
|
||||
|
||||
|
@ -54,3 +55,35 @@ proc startMetricsLog*() =
|
|||
)
|
||||
|
||||
discard setTimer(Moment.fromNow(LogInterval), logMetrics)
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue