项目作者: manifoldco

项目描述 :
Easily add health checks to your go services
高级语言: Go
项目地址: git://github.com/manifoldco/healthz.git
创建时间: 2017-11-27T13:58:31Z
项目社区:https://github.com/manifoldco/healthz

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Healthz

Code of Conduct |
Contribution Guidelines

GitHub release
GoDoc
Build Status
Go Report Card
License

This is a package that allows you to set up health checks for your services. By
default, the health checks will be available at the /_healthz endpoint but can
be configured with the Prefix and Endpoint variables.

By default, the health check package will add a single default test. This test
doesn’t validate anything, it simply returns no error.

Registering additional tests

If you want to add more tests in case your service/worker depends on a specific
set of tools (like a JWT key mounted on a volume), you can register a new test
as follows:

  1. func init() {
  2. healthz.RegisterTest("jwt-key", jwtCheck)
  3. }
  4. func jwtCheck(ctx context.Context) error {
  5. _, err := os.Stat("/jwt/ecdsa-private.pem")
  6. return err
  7. }

Attaching the endpoints to an existing server

If you have an existing server running, you can attach the /_healthz endpoint
to it without having to start a separate server.

  1. func main() {
  2. mux := http.NewServeMux()
  3. mux.HandleFunc("/hello", func(w http.ResponseWriter, _ *http.Request) {
  4. w.WriteHeader(http.StatusOK)
  5. })
  6. handler := healthz.NewHandler(mux)
  7. http.ListenAndServe(":3000", handler)
  8. }

This will create a new mux which listens to the /hello request. We then attach
the healthcheck handler by using healthz.NewHandler(mux).

Creating a standalone server

When your application isn’t a web server, but you still want to add health
checks, you can use the provided server implementation.

  1. func main() {
  2. stop := make(chan os.Signal, 1)
  3. signal.Notify(stop, os.Interrupt)
  4. srv := healthz.NewServer("0.0.0.0", 3000)
  5. go srv.Start()
  6. <-stop
  7. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  8. srv.Shutdown(ctx)
  9. }

Using middleware

With both the NewHandler and NewServer implementation, we’ve provided a way
to add middleware. This allows you to add logging to your health checks for
example.

The middleware functions are respectively NewHandlerWithMiddleware and
NewServerWithMiddleware. They accept the arguments of their parent function
but also a set of middlewares as variadic arguments.

  1. func main() {
  2. mux := http.NewServeMux()
  3. mux.HandleFunc("/hello", func(w http.ResponseWriter, _ *http.Request) {
  4. w.WriteHeader(http.StatusOK)
  5. })
  6. handler := healthz.NewHandlerWithMiddleware(mux, logMiddleware)
  7. http.ListenAndServe(":3000", handler)
  8. }
  9. func logMiddleware(next http.Handler) http.Handler {
  10. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  11. log.Print("start request")
  12. next.ServeHTTP(w, r)
  13. log.Print("end request")
  14. })
  15. }

Output

On the health check endpoint, we return a set of values useful to us. Extending
the example from above, these are both return values (one where the file is
present, one where it isn’t).

200 OK

  1. {
  2. "checked_at": "2017-11-22T14:18:50.339Z",
  3. "duration_ms": 0,
  4. "result": "success",
  5. "tests": {
  6. "default": {
  7. "duration_ms": 0,
  8. "result": "success"
  9. },
  10. "jwt-key": {
  11. "duration_ms": 0,
  12. "result": "success"
  13. }
  14. }
  15. }

503 Service Unavailable

  1. {
  2. "checked_at": "2017-11-22T14:18:50.339Z",
  3. "duration_ms": 1,
  4. "result": "failure",
  5. "tests": {
  6. "default": {
  7. "duration_ms": 0,
  8. "result": "success"
  9. },
  10. "jwt-key": {
  11. "duration_ms": 0,
  12. "result": "failure"
  13. }
  14. }
  15. }

Middlewares

We’ve included a set of standard middlewares that can be useful for general use.

Cache

The cache middleware allows you to cache a response for a specific duration.
This prevents the health check to overload due to different sources asking for
the health status. This is especially useful when the health checks are used to
check the health of other services as well.

To use this middleware, simply add it to the chain:

  1. func main() {
  2. mux := http.NewServeMux()
  3. mux.HandleFunc("/hello", func(w http.ResponseWriter, _ *http.Request) {
  4. w.WriteHeader(http.StatusOK)
  5. })
  6. handler := healthz.NewHandlerWithMiddleware(
  7. mux,
  8. healthz.CacheMiddleware(5*time.Second),
  9. )
  10. http.ListenAndServe(":3000", handler)
  11. }