• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

healthcheck 用于Go应用易于使用可扩展的健康检查库

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

仓库:

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

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

healthcheck 实现了一个开箱即用的Kubernetes liveness&& readiness prob 实现,我们可以直接拿来使用。已经包含了tcp,dns,http,Goroutine prob,同时也支持prometheus。

demo 示例:

 

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)
}

 

测试结果:

 

#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

 

 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
goprintln与printf区别发布时间:2022-07-10
下一篇:
基于Web的Go语言IDE-Wide1.5.2发布!发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap