Skip to content

Commit

Permalink
refactor: metrics server. Simplify app.nim module (#2650)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status authored Apr 30, 2024
1 parent 6382ded commit 4a110f6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 50 deletions.
3 changes: 2 additions & 1 deletion apps/wakunode2/wakunode2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions tests/wakunode2/test_app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 4 additions & 46 deletions waku/factory/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type
node: WakuNode

restServer*: WakuRestServerRef
metricsServer: Option[MetricsHttpServerRef]
metricsServer*: MetricsHttpServerRef

AppResult*[T] = Result[T, string]

Expand Down Expand Up @@ -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()
Expand Down
35 changes: 34 additions & 1 deletion waku/node/waku_metrics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 4a110f6

Please sign in to comment.