@@ -176,6 +176,7 @@ import qualified Data.Set as Set
176
176
import qualified GHC.Clock
177
177
import qualified Kupo.Data.Http.Default as Default
178
178
import qualified Kupo.Data.Http.Error as Errors
179
+ import qualified Network.HTTP.Types.Status as Http
179
180
import qualified Network.HTTP.Types.Header as Http
180
181
import qualified Network.HTTP.Types.URI as Http
181
182
import qualified Network.Wai.Handler.Warp as Warp
@@ -262,6 +263,9 @@ app withDatabase forceRollback fetchBlock patternsVar readHealth req send =
262
263
(" health" : args) ->
263
264
routeHealth (requestMethod req, args)
264
265
266
+ (" metrics" : args) ->
267
+ routeMetrics (requestMethod req, args)
268
+
265
269
(" checkpoints" : args) ->
266
270
cacheOr readHealth req send $ flip routeCheckpoints (requestMethod req, args)
267
271
@@ -286,10 +290,19 @@ app withDatabase forceRollback fetchBlock patternsVar readHealth req send =
286
290
_unmatchedRoutes ->
287
291
send Errors. notFound
288
292
293
+ routeMetrics = \ case
294
+ (" GET" , [] ) -> do
295
+ health <- readHealth
296
+ send =<< handleGetHealth (requestHeaders req) (Just status200) health
297
+ (" GET" , _) ->
298
+ send Errors. notFound
299
+ (_, _) ->
300
+ send Errors. methodNotAllowed
301
+
289
302
routeHealth = \ case
290
303
(" GET" , [] ) -> do
291
304
health <- readHealth
292
- send =<< handleGetHealth (requestHeaders req) health
305
+ send =<< handleGetHealth (requestHeaders req) Nothing health
293
306
(" GET" , _) ->
294
307
send Errors. notFound
295
308
(_, _) ->
@@ -448,9 +461,10 @@ pathParametersToText = \case
448
461
449
462
handleGetHealth
450
463
:: [Http. Header ]
464
+ -> Maybe Http. Status
451
465
-> Health
452
466
-> IO Response
453
- handleGetHealth reqHeaders health =
467
+ handleGetHealth reqHeaders forcedStatus health =
454
468
case findContentType reqHeaders of
455
469
Just ct | cTextPlain `BS.isInfixOf` ct -> do
456
470
let resHeaders = addCacheHeaders [(hContentType, cTextPlain <> " ;charset=utf-8" )] health
@@ -467,21 +481,23 @@ handleGetHealth reqHeaders health =
467
481
Just {} ->
468
482
return $ Errors. unsupportedContentType (prettyContentTypes <$> [cApplicationJson, cTextPlain])
469
483
where
470
- status = case connectionStatus of
471
- -- We consider the server 'far away' from another point if it is more than a 'batch' distance
472
- -- from that point. The 'mailboxCapacity' is given in slot, and we consider an active slot
473
- -- coefficient of 1/20 (which has been the default on ALL cardano networks).
474
- --
475
- -- If the distance is lower than that, it means we are one roll-forward away from being
476
- -- synchronized, in which case, we consider the server synchronized. Note that we could
477
- -- theoritically consider 0 here, but this gives us some resilience to rollbacks so long as
478
- -- they aren't longer than 'mailboxCapacity' blocks.
479
- Connected | fromMaybe maxBound d < fromIntegral (mailboxCapacity * 20 ) ->
480
- status200
481
- Connected ->
482
- status202
483
- Disconnected ->
484
- status503
484
+ status = fromMaybe
485
+ (case connectionStatus of
486
+ -- We consider the server 'far away' from another point if it is more than a 'batch' distance
487
+ -- from that point. The 'mailboxCapacity' is given in slot, and we consider an active slot
488
+ -- coefficient of 1/20 (which has been the default on ALL cardano networks).
489
+ --
490
+ -- If the distance is lower than that, it means we are one roll-forward away from being
491
+ -- synchronized, in which case, we consider the server synchronized. Note that we could
492
+ -- theoritically consider 0 here, but this gives us some resilience to rollbacks so long as
493
+ -- they aren't longer than 'mailboxCapacity' blocks.
494
+ Connected | fromMaybe maxBound d < fromIntegral (mailboxCapacity * 20 ) ->
495
+ status200
496
+ Connected ->
497
+ status202
498
+ Disconnected ->
499
+ status503
500
+ ) forcedStatus
485
501
where
486
502
Health {.. } = health
487
503
d = distanceToSlot
@@ -502,6 +518,7 @@ handleGetHealth reqHeaders health =
502
518
503
519
prettyContentTypes ct = decodeUtf8 (" '" <> ct <> " '" )
504
520
521
+
505
522
--
506
523
-- /checkpoints
507
524
--
0 commit comments