1024programmer Java healthcheck is an easy-to-use extensible health check library for Go applications

healthcheck is an easy-to-use extensible health check library for Go applications

Warehouse:

https://github.com/heptiolabs/healthcheck.git

https://pkg.go.dev/github.com/heptiolabs/healthcheck#section-readme

healthcheck implements an out-of-the-box Kubernetes liveness&& readiness prob implementation, which we can use directly. Already includes tcp, dns, http, Goroutine prob, and also supports prometheus.

demo example:

package main

 import (
     "fmt"
     "github.com/heptiolabs/healthcheck"
     "github.com/prometheus/client_golang/prometheus"
     "github.com/prometheus/client_golang/prometheus/promhttp"
     "log"
     "net/http"
     "net/http/httptest"
     "net/http/httputil"
     "os"
     "os/signal"
     "strings"
     "syscall"
     "time"
 )

 func main() {
     registry := prometheus.NewRegistry()
     // Create a metrics-exposing Handler for the Prometheus registry
     // The healthcheck related metrics will be prefixed with the provided namespace
     health := healthcheck.NewMetricsHandler(registry, "example")

     // Add a readiness check against the health of an upstream HTTP dependency
     upstreamURL := "https://www.cnblogs.com/wangjq19920210"

     health.AddReadinessCheck("upstream-dep-http", healthcheck.HTTPGetCheck(upstreamURL, 500*time.Millisecond))
     // Implement a custom check with a 50 millisecond timeout.
     health.AddLivenessCheck("custom-check-with-timeout", healthcheck.Timeout(func() error {
         // Simulate some work that could take a long time
         time.Sleep(time.Millisecond * 100)
         return nil
     }, 500*time.Millisecond))

     // Expose the readiness endpoints on a custom path /healthz mixed into
     // our main application mux.
     mux := http.NewServeMux()
     mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
         w.Write([]byte("Hello, world!")  )
     })
     mux.HandleFunc("/healthz", health.LiveEndpoint)
     mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))

     // Sleep for just a moment to make sure our Async handler had a chance to run
     //time.Sleep(500 * time.Millisecond)
     go http.ListenAndServe("0.0.0.0:9402", mux)

     // Make a sample request to the /healthz endpoint and print the response.
     fmt.Println(dumpRequest(mux, "GET", "/healthz  "))
     sigChan := make(chan os.Signal, 1)
     signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
     sig := <-sigChan
     log.Printf("shutting down %v signal received", sig)
 }

 func dumpRequest(handler http.Handler, method string, path string) string {
     req, err:= http.NewRequest(method, path, nil)
     if err != nil {
         panic(err)
     }
     rr := httptest.NewRecorder()
     handler.ServeHTTP(rr, req)
     dump, err := httputil.DumpResponse(rr.Result(), true)
     if err != nil {
         panic(err)
     }
     return strings.Replace(string(dump), "\r\n  ", "\n", -1)
 }

Test results:

#curl http://0.0.0.0:9402/metrics
 # HELP example_healthcheck_status Current check status (0 indicates success, 1 indicates failure)
 # TYPE example_healthcheck_status gauge
 example_healthcheck_status{check="custom-check-with-timeout"} 0
 example_healthcheck_status{check="upstream-dep-http"} 0

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/736954

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索