All Projects → manifoldco → healthz

manifoldco / healthz

Licence: BSD-3-Clause License
Easily add health checks to your go services

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to healthz

golang-health-checker
A simple package to allow you to track your application healthy
Stars: ✭ 12 (-42.86%)
Mutual labels:  healthcheck
airbud
Retrieving stuff from the web is unreliable. Airbud adds retries for production, and fixture support for test.
Stars: ✭ 15 (-28.57%)
Mutual labels:  reliability
pylife
a general library for fatigue and reliability
Stars: ✭ 45 (+114.29%)
Mutual labels:  reliability
CalibrationErrors.jl
Estimation of calibration errors.
Stars: ✭ 13 (-38.1%)
Mutual labels:  reliability
maikai
RFC-compliant, Kubernetes-ready, intelligent health check middleware for HTTP APIs and microservices written in Node
Stars: ✭ 23 (+9.52%)
Mutual labels:  healthcheck
health-go
A golang implementation of the upcoming IETF RFC Health Check Response Format for HTTP APIs
Stars: ✭ 31 (+47.62%)
Mutual labels:  healthcheck
vigor
Main repository of the Vigor NF verification project.
Stars: ✭ 40 (+90.48%)
Mutual labels:  reliability
aarogya seva
A beautiful 😍 covid-19 app with self - assessment and more.
Stars: ✭ 118 (+461.9%)
Mutual labels:  healthcheck
python-holdup
A tool to wait for services and execute command. Useful in Docker containers.
Stars: ✭ 29 (+38.1%)
Mutual labels:  healthcheck
gorift
A toolkit for reverse proxy and load balancer
Stars: ✭ 20 (-4.76%)
Mutual labels:  healthcheck
watcher
watcher(守望者)提供java应用暴露监控/健康检查的能力。
Stars: ✭ 65 (+209.52%)
Mutual labels:  healthcheck
optimize-ubuntu
Optimize Ubuntu for usability, security, privacy and stability
Stars: ✭ 15 (-28.57%)
Mutual labels:  reliability
The-Nurse
Alert whoever you want when your apps are in a bad shape.
Stars: ✭ 18 (-14.29%)
Mutual labels:  healthcheck
mapi-action
🤖 Run a Mayhem for API scan in GitHub Actions
Stars: ✭ 16 (-23.81%)
Mutual labels:  reliability
mingine
A module to get the minimum usable engine(s)
Stars: ✭ 17 (-19.05%)
Mutual labels:  reliability
active-monitor
Provides deep monitoring and self-healing of Kubernetes clusters
Stars: ✭ 135 (+542.86%)
Mutual labels:  healthcheck
xk6-chaos
xk6 extension for running chaos experiments with k6 💣
Stars: ✭ 18 (-14.29%)
Mutual labels:  reliability
software-practice-thoughts
📚 🐣 软件实践文集。主题不限,思考讨论有趣有料就好,包含如 系统的模型分析/量化分析、开源漫游者指南、软件可靠性设计实践…… 🥤
Stars: ✭ 122 (+480.95%)
Mutual labels:  reliability
ngx stream upstream check module
nginx health checker (tcp/udp/http) for stream upstream servers.
Stars: ✭ 18 (-14.29%)
Mutual labels:  healthcheck
CleanArchitecture-Template
This is a solution template for Clean Architecture and CQRS implementation with ASP.NET Core.
Stars: ✭ 60 (+185.71%)
Mutual labels:  healthcheck

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:

func init() {
	healthz.RegisterTest("jwt-key", jwtCheck)
}

func jwtCheck(ctx context.Context) error {
	_, err := os.Stat("/jwt/ecdsa-private.pem")
	return err
}

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.

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/hello", func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
	})

	handler := healthz.NewHandler(mux)
	http.ListenAndServe(":3000", handler)
}

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.

func main() {
	stop := make(chan os.Signal, 1)
	signal.Notify(stop, os.Interrupt)

	srv := healthz.NewServer("0.0.0.0", 3000)
	go srv.Start()

	<-stop

	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
	srv.Shutdown(ctx)
}

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.

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/hello", func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
	})

	handler := healthz.NewHandlerWithMiddleware(mux, logMiddleware)
	http.ListenAndServe(":3000", handler)
}

func logMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		log.Print("start request")
		next.ServeHTTP(w, r)
		log.Print("end request")
	})
}

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

{
  "checked_at": "2017-11-22T14:18:50.339Z",
  "duration_ms": 0,
  "result": "success",
  "tests": {
    "default": {
      "duration_ms": 0,
      "result": "success"
    },
    "jwt-key": {
      "duration_ms": 0,
      "result": "success"
    }
  }
}

503 Service Unavailable

{
  "checked_at": "2017-11-22T14:18:50.339Z",
  "duration_ms": 1,
  "result": "failure",
  "tests": {
    "default": {
      "duration_ms": 0,
      "result": "success"
    },
    "jwt-key": {
      "duration_ms": 0,
      "result": "failure"
    }
  }
}

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:

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/hello", func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
	})

	handler := healthz.NewHandlerWithMiddleware(
		mux,
		healthz.CacheMiddleware(5*time.Second),
	)
	http.ListenAndServe(":3000", handler)
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].