11#  beacon_chain
2- #  Copyright (c) 2018-2024  Status Research & Development GmbH
2+ #  Copyright (c) 2018-2025  Status Research & Development GmbH
33#  Licensed and distributed under either of
44#    * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
55#    * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -161,38 +161,6 @@ proc initClock(
161161         current_slot =  currentSlot, current_epoch =  currentEpoch
162162  res
163163
164- proc  initMetrics (
165-     vc: ValidatorClientRef 
166- ): Future [bool ] {.async : (raises: [CancelledError ]).} = 
167-   if  vc.config.metricsEnabled:
168-     let 
169-       metricsAddress =  vc.config.metricsAddress
170-       metricsPort =  vc.config.metricsPort
171-       url =  " http://"   &  $ metricsAddress &  " :"   &  $ metricsPort &  " /metrics" 
172-     info  " Starting metrics HTTP server"  , url =  url
173-     let  server = 
174-       block :
175-         let  res =  MetricsHttpServerRef .new ($ metricsAddress, metricsPort)
176-         if  res.isErr ():
177-           error  " Could not start metrics HTTP server"  , url =  url,
178-                 error_msg =  res.error ()
179-           return  false 
180-         res.get ()
181-     vc.metricsServer =  Opt .some (server)
182-     try :
183-       await  server.start ()
184-     except  MetricsError  as  exc:
185-       error  " Could not start metrics HTTP server"  , url =  url,
186-             error_msg =  exc.msg, error_name =  exc.name
187-       return  false 
188-   true 
189- 
190- proc  shutdownMetrics (vc: ValidatorClientRef ) {.async : (raises: []).} = 
191-   if  vc.config.metricsEnabled:
192-     if  vc.metricsServer.isSome ():
193-       info  " Shutting down metrics HTTP server" 
194-       await  vc.metricsServer.get ().close ()
195- 
196164proc  shutdownSlashingProtection (vc: ValidatorClientRef ) = 
197165  info  " Closing slashing protection"  , path =  vc.config.validatorsDir ()
198166  vc.attachedValidators[].slashingProtection.close ()
@@ -351,7 +319,7 @@ proc asyncInit(vc: ValidatorClientRef): Future[ValidatorClientRef] {.
351319
352320  vc.beaconClock =  await  vc.initClock ()
353321
354-   if   not (await  initMetrics (vc)) :
322+   vc.metricsServer  =   (await  vc.config. initMetricsServer ()).valueOr :
355323    raise  newException (ValidatorClientError ,
356324                       " Could not initialize metrics server"  )
357325
@@ -368,7 +336,7 @@ proc asyncInit(vc: ValidatorClientRef): Future[ValidatorClientRef] {.
368336  vc.attachedValidators =  validatorPool
369337
370338  if  not (await  initValidators (vc)):
371-     await  vc.shutdownMetrics ()
339+     await  vc.metricsServer. stopMetricsServer ()
372340    raise  newException (ValidatorClientError ,
373341                       " Could not initialize local validators"  )
374342
@@ -432,7 +400,7 @@ proc asyncInit(vc: ValidatorClientRef): Future[ValidatorClientRef] {.
432400        )
433401  except  CancelledError :
434402    debug  " Initialization process interrupted" 
435-     await  vc.shutdownMetrics ()
403+     await  vc.metricsServer. stopMetricsServer ()
436404    vc.shutdownSlashingProtection ()
437405    return 
438406
@@ -522,7 +490,7 @@ proc asyncRun*(
522490  except  CancelledError :
523491    debug  " Main loop interrupted" 
524492
525-   await  vc.shutdownMetrics ()
493+   await  vc.metricsServer. stopMetricsServer ()
526494  vc.shutdownSlashingProtection ()
527495
528496  if  doppelEventFut.completed ():
0 commit comments