From 54c265e0f0df7ee42ba3d188e17b95c18468188d Mon Sep 17 00:00:00 2001 From: Tirumarai Selvan A Date: Thu, 28 Nov 2019 14:25:25 +0530 Subject: [PATCH 1/3] check db connection in healthz endpoint --- server/src-lib/Hasura/Server/App.hs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/server/src-lib/Hasura/Server/App.hs b/server/src-lib/Hasura/Server/App.hs index 4e83c1ad6d21f..b06a586674119 100644 --- a/server/src-lib/Hasura/Server/App.hs +++ b/server/src-lib/Hasura/Server/App.hs @@ -8,6 +8,7 @@ import Control.Concurrent.MVar import Control.Exception (IOException, try) import Control.Monad.Stateless import Data.Aeson hiding (json) +import Data.Either (isRight) import Data.Int (Int64) import Data.IORef import Data.Time.Clock (UTCTime) @@ -531,8 +532,11 @@ httpApp corsCfg serverCtx enableConsole consoleAssetsDir enableTelemetry = do -- Health check endpoint Spock.get "healthz" $ do sc <- liftIO $ getSCFromRef $ scCacheRef serverCtx - if null $ scInconsistentObjs sc - then Spock.setStatus HTTP.status200 >> Spock.lazyBytes "OK" + dbOk <- checkDbConnection + if dbOk + then Spock.setStatus HTTP.status200 >> (Spock.lazyBytes $ if null (scInconsistentObjs sc) + then "OK" + else "WARN: inconsistent objects in schema") else Spock.setStatus HTTP.status500 >> Spock.lazyBytes "ERROR" Spock.get "v1/version" $ do @@ -619,6 +623,14 @@ httpApp corsCfg serverCtx enableConsole consoleAssetsDir enableTelemetry = do enablePGDump = isPGDumpEnabled serverCtx enableConfig = isConfigEnabled serverCtx + checkDbConnection = do + e <- liftIO $ runExceptT $ runLazyTx' (scPGExecCtx serverCtx) select1Query + pure $ isRight e + where + select1Query :: (MonadTx m) => m Int + select1Query = liftTx $ runIdentity . Q.getRow <$> Q.withQE defaultTxErrorHandler + [Q.sql| SELECT 1 |] () False + serveApiConsole = do -- redirect / to /console Spock.get Spock.root $ Spock.redirect "console" From 6d80f370273423cfed4552c9d15bca661d57cbc1 Mon Sep 17 00:00:00 2001 From: Tirumarai Selvan A Date: Tue, 3 Dec 2019 11:52:24 +0530 Subject: [PATCH 2/3] use Spock.text to automatically set Content-Type --- server/src-lib/Hasura/Server/App.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src-lib/Hasura/Server/App.hs b/server/src-lib/Hasura/Server/App.hs index c2b179908f973..6a8e0b81580dd 100644 --- a/server/src-lib/Hasura/Server/App.hs +++ b/server/src-lib/Hasura/Server/App.hs @@ -533,10 +533,10 @@ httpApp corsCfg serverCtx enableConsole consoleAssetsDir enableTelemetry = do sc <- liftIO $ getSCFromRef $ scCacheRef serverCtx dbOk <- checkDbConnection if dbOk - then Spock.setStatus HTTP.status200 >> (Spock.lazyBytes $ if null (scInconsistentObjs sc) + then Spock.setStatus HTTP.status200 >> (Spock.text $ if null (scInconsistentObjs sc) then "OK" else "WARN: inconsistent objects in schema") - else Spock.setStatus HTTP.status500 >> Spock.lazyBytes "ERROR" + else Spock.setStatus HTTP.status500 >> Spock.text "ERROR" Spock.get "v1/version" $ do uncurry Spock.setHeader jsonHeader From 1a223d393b88da91cf9d030780631dd8a98c73e4 Mon Sep 17 00:00:00 2001 From: Tirumarai Selvan A Date: Tue, 3 Dec 2019 12:14:17 +0530 Subject: [PATCH 3/3] update docs --- docs/graphql/manual/api-reference/index.rst | 24 ++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/graphql/manual/api-reference/index.rst b/docs/graphql/manual/api-reference/index.rst index 6087788e26c9b..74f725f0f850b 100644 --- a/docs/graphql/manual/api-reference/index.rst +++ b/docs/graphql/manual/api-reference/index.rst @@ -67,11 +67,25 @@ in JSON format: Health check API ^^^^^^^^^^^^^^^^ -A ``GET`` request to the public ``/healthz`` endpoint will respond with ``200`` -if the GraphQL engine is ready to serve requests and there are no inconsistencies -with the metadata. The response will be ``500`` if there are metadata -inconsistencies and you should use the console or check the server logs to find -out what the errors are. +A ``GET`` request to the public ``/healthz`` endpoint will respond with the following: + +.. list-table:: + :header-rows: 1 + + * - Server condition + - HTTP Status + - Message + * - All healthy + - 200 + - OK + * - Serving requests but some metadata objects are inconsistent/not-available + - 200 + - WARN: inconsistent objects in schema + * - Unhealthy + - 500 + - ERROR + +If there are metadata inconsistencies, you should use the console or use the `get_inconsistent_metadata `_ API to find out what the inconsistent objects are. .. _pg_dump_api: