Skip to content

Commit ff7ca58

Browse files
committed
Health check endpoint at /health
Problem: AWS ALB doesn't support health checking things except by status code (and nor does basically any other standard load balancer), and Glean only exports health checks as a Thrift service. Solution: turn health checks into HTTP status with a middleware. Requires: facebookincubator/hsthrift#167
1 parent 297f701 commit ff7ca58

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

glean.cabal.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ library stubs
230230
tasty,
231231
tasty-hunit
232232

233+
if !flag(fbthrift)
234+
other-modules: Facebook.Service.HttpFb303
235+
build-depends: wai, http-types
236+
233237
library logger
234238
import: fb-haskell, fb-cpp, deps
235239
visibility: private

glean/github/Facebook/Service.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Thrift.Processor
3232
import Thrift.Server.CppServer
3333
#else
3434
import Thrift.Server.HTTP
35+
import Facebook.Service.HttpFb303
3536
#endif
3637

3738
-- | Runs a facebook service on a CPPServer
@@ -166,7 +167,12 @@ withBackgroundFacebookService_
166167
-> IO a
167168
withBackgroundFacebookService_ fb303 handler postProcess opts waitAction = do
168169
bracketFb303 fb303 Fb303_status_STARTING Fb303_status_STOPPED $ do
169-
withBackgroundServer' handler postProcess opts waitAction
170+
#if FBTHRIFT
171+
let opts' = opts
172+
#else
173+
let opts' = opts { middleware = middleware opts . fb303Middleware fb303 }
174+
#endif
175+
withBackgroundServer' handler postProcess opts' waitAction
170176

171177
nilPostProcess :: a -> b -> [c]
172178
nilPostProcess _ _ = []
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{-
2+
Copyright (c) Meta Platforms, Inc. and affiliates.
3+
All rights reserved.
4+
5+
This source code is licensed under the BSD-style license found in the
6+
LICENSE file in the root directory of this source tree.
7+
-}
8+
9+
module Facebook.Service.HttpFb303 where
10+
11+
import qualified Data.ByteString.Lazy as LBS
12+
import Data.IORef (readIORef)
13+
import Network.HTTP.Types
14+
import Network.Wai
15+
16+
import Facebook.Fb303
17+
import Fb303Core.Types
18+
19+
-- | Middleware exposing Fb303 as a more standard health check endpoint at @/health@.
20+
fb303Middleware :: Fb303State -> Middleware
21+
fb303Middleware fb303 app req respond = do
22+
case pathInfo req of
23+
["health"] -> do
24+
status <- readIORef (fb303_status fb303)
25+
let replyStatus = case status of
26+
Fb303_status_ALIVE -> ok200
27+
Fb303_status_WARNING -> mkStatus 290 "warning"
28+
Fb303_status_STARTING -> mkStatus 291 "starting"
29+
Fb303_status_STOPPING -> mkStatus 292 "stopping"
30+
Fb303_status_STOPPED -> mkStatus 293 "stopped"
31+
Fb303_status_DEAD -> mkStatus 294 "dead"
32+
Fb303_status__UNKNOWN _ -> mkStatus 299 "unknown status"
33+
respond $ responseLBS replyStatus [(hContentType, "text/plain")] (LBS.fromStrict $ statusMessage replyStatus)
34+
_ -> app req respond

0 commit comments

Comments
 (0)