diff --git a/go.mod b/go.mod index b93c837..7a3fff6 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,6 @@ go 1.15 require ( github.com/gorilla/websocket v1.4.2 github.com/itering/substrate-api-rpc v0.2.1 - github.com/jessevdk/go-flags v1.4.0 - github.com/kr/pretty v0.1.0 // indirect - github.com/sirupsen/logrus v1.7.0 golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 // indirect - gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect + k8s.io/klog/v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 9abd0cc..43090d9 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v0.2.0 h1:QvGt2nLcHH0WK9orKa+ppBPAxREcH364nPUedEpK0TY= +github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= @@ -8,31 +8,17 @@ github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/itering/scale.go v0.7.0 h1:082rhT0di1JSxmr6DjlTxYWOrebjJL0y+Xp6TCMW6ls= github.com/itering/scale.go v0.7.0/go.mod h1:ufrA/8oz/qi4mtK8TXz4LFZzfJ4ZUQ+4z+cUNpY9jts= -github.com/itering/substrate-api-rpc v0.2.0 h1:gFpxU9WRk+AWAe6da+UeOg8arOnxADCVkOxPQu/mSG4= -github.com/itering/substrate-api-rpc v0.2.0/go.mod h1:mhWsiQusRakyHDvLF+QkA1KMVaohI0ntuVoWABlEc68= github.com/itering/substrate-api-rpc v0.2.1 h1:0p/S71njgp3RTczO71bqn2RhIs3+hvdcIW1UHe8O2nQ= github.com/itering/substrate-api-rpc v0.2.1/go.mod h1:mhWsiQusRakyHDvLF+QkA1KMVaohI0ntuVoWABlEc68= -github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= -github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo= github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= -github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -52,7 +38,6 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201008141435-b3e1573b7520/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -67,7 +52,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +k8s.io/klog/v2 v2.4.0 h1:7+X0fUguPyrKEC4WjH8iGDg3laWgMo5tMnRTIGTTxGQ= +k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y= diff --git a/handlers/handler.go b/handlers/handler.go index 5519552..3319916 100644 --- a/handlers/handler.go +++ b/handlers/handler.go @@ -7,7 +7,7 @@ import ( "time" ws "github.com/gorilla/websocket" - log "github.com/sirupsen/logrus" + "k8s.io/klog/v2" ) type Prober interface { @@ -23,19 +23,19 @@ type ProbeHandler struct { func (h *ProbeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { var statusCode int start := time.Now() - log.Debugf("Received request %s from %s %s", r.URL.Path, r.RemoteAddr, r.Header.Get("User-Agent")) + klog.V(4).Infof("Received request %s from %s %s", r.URL.Path, r.RemoteAddr, r.Header.Get("User-Agent")) timeout, err := h.parseTimeoutFromQuery(r) if err != nil { statusCode = http.StatusInternalServerError err = fmt.Errorf("parseTimeoutFromQuery: %w", err) - log.Error(err) + klog.Error(err) } else if statusCode, err = h.dialAndProbe(timeout); err != nil { - log.Warn(err) + klog.Warning(err) } elapsed := time.Since(start) - log.Infof("Probe %s returning %d in %s", r.URL.Path, statusCode, elapsed) + klog.Infof("Probe %s returning %d in %s", r.URL.Path, statusCode, elapsed) w.WriteHeader(statusCode) w.Write([]byte(http.StatusText(statusCode))) @@ -46,7 +46,7 @@ func (h *ProbeHandler) dialAndProbe(wsHandshakeTimeout *time.Duration) (int, err HandshakeTimeout: *wsHandshakeTimeout, } - log.Debugf("Dialer: %+v", dialer) + klog.V(5).Infof("Dialer: %+v", dialer) conn, _, err := dialer.Dial(h.WsEndpoint, nil) diff --git a/main.go b/main.go index 96bd1ff..7d86206 100644 --- a/main.go +++ b/main.go @@ -1,51 +1,49 @@ package main import ( - "fmt" + "flag" "net/http" - "os" "github.com/darwinia-network/node-liveness-probe/handlers" "github.com/darwinia-network/node-liveness-probe/probes" - flags "github.com/jessevdk/go-flags" - log "github.com/sirupsen/logrus" + "k8s.io/klog/v2" ) -var opts struct { - Listen string `long:"listen" description:"Listen address" value-name:"[ADDR]:PORT" default:":49944"` - NodeWsEndpoint string `long:"ws-endpoint" description:"Node websocket endpoint" value-name:"://ADDR[:PORT]" default:"ws://127.0.0.1:9944"` - LogLevel uint32 `long:"log-level" description:"The log level (0 ~ 6), use 5 for debugging, see https://pkg.go.dev/github.com/sirupsen/logrus#Level" value-name:"N" default:"4"` - BlockThresholdSeconds float64 `long:"block-threshold-seconds" description:"/healthz_block returns unhealthy if node's latest block is older than threshold" value-name:"N" default:"120"` -} - var ( buildVersion = "dev" buildCommit = "none" buildDate = "unknown" ) -func main() { - if _, err := flags.Parse(&opts); err != nil { - os.Exit(0) - } +var ( + listen = flag.String("listen", ":49944", "Listen address") + wsEndpoint = flag.String("ws-endpoint", "ws://127.0.0.1:9944", "Substrate node WebSocket endpoint") + blockThresholdSeconds = flag.Float64("block-threshold-seconds", 120, "/healthz_block returns unhealthy if node's latest block is older than threshold") +) - fmt.Printf("Substrate Node Livness Probe %v-%v (built %v)\n", buildVersion, buildCommit, buildDate) +func main() { + klog.InitFlags(nil) + flag.Set("logtostderr", "true") + flag.Parse() - log.SetLevel(log.Level(opts.LogLevel)) + klog.Infof("Substrate Node Livness Probe %v-%v (built %v)\n", buildVersion, buildCommit, buildDate) http.Handle("/healthz", &handlers.ProbeHandler{ Prober: &probes.LivenessProbe{}, - WsEndpoint: opts.NodeWsEndpoint, + WsEndpoint: *wsEndpoint, }) http.Handle("/healthz_block", &handlers.ProbeHandler{ - Prober: &probes.LivenessBlockProbe{BlockThresholdSeconds: opts.BlockThresholdSeconds}, - WsEndpoint: opts.NodeWsEndpoint, + Prober: &probes.LivenessBlockProbe{BlockThresholdSeconds: *blockThresholdSeconds}, + WsEndpoint: *wsEndpoint, }) http.Handle("/readiness", &handlers.ProbeHandler{ Prober: &probes.ReadinessProbe{}, - WsEndpoint: opts.NodeWsEndpoint, + WsEndpoint: *wsEndpoint, }) - log.Infof("Serving requests on %s", opts.Listen) - log.Fatal(http.ListenAndServe(opts.Listen, nil)) + klog.Infof("Serving requests on %s", *listen) + err := http.ListenAndServe(*listen, nil) + if err != nil { + klog.Fatalf("failed to start http server with error: %v", err) + } } diff --git a/probes/liveness_block_probe.go b/probes/liveness_block_probe.go index d5c72ef..20e6ddd 100644 --- a/probes/liveness_block_probe.go +++ b/probes/liveness_block_probe.go @@ -8,7 +8,7 @@ import ( ws "github.com/gorilla/websocket" "github.com/itering/substrate-api-rpc/rpc" - log "github.com/sirupsen/logrus" + "k8s.io/klog/v2" ) type LivenessBlockProbe struct { @@ -37,7 +37,7 @@ func (b *Block) IsStale(thresholdSeconds float64, status string) error { thresholdSeconds, ) } else { - log.Debugf( + klog.V(2).Infof( "The %s block %d was obtained %.2f second(s) ago, below the threshold %.2f", status, b.Number, @@ -58,7 +58,7 @@ func (p *LivenessBlockProbe) Probe(conn *ws.Conn) (int, error) { return http.StatusServiceUnavailable, err } - log.Infof("Retrieved block, best: #%d, finalized: #%d", p.bestBlock.Number, p.finalizedBlock.Number) + klog.Infof("Retrieved block, best: #%d, finalized: #%d", p.bestBlock.Number, p.finalizedBlock.Number) errBestBlock := p.bestBlock.IsStale(p.BlockThresholdSeconds, "best") errFinalizedBlock := p.finalizedBlock.IsStale(p.BlockThresholdSeconds, "finalized") diff --git a/probes/liveness_probe.go b/probes/liveness_probe.go index 98ae296..b0df66d 100644 --- a/probes/liveness_probe.go +++ b/probes/liveness_probe.go @@ -6,7 +6,7 @@ import ( ws "github.com/gorilla/websocket" "github.com/itering/substrate-api-rpc/rpc" - log "github.com/sirupsen/logrus" + "k8s.io/klog/v2" ) type LivenessProbe struct{} @@ -50,7 +50,7 @@ func (p *LivenessProbe) Probe(conn *ws.Conn) (int, error) { } func sendWsRequest(conn *ws.Conn, name string, data []byte) (*rpc.JsonRpcResult, error) { - log.Tracef("sendWsRequest: %s", data) + klog.V(5).Infof("sendWsRequest: %s", data) v := &rpc.JsonRpcResult{} if err := conn.WriteMessage(ws.TextMessage, data); err != nil { @@ -65,6 +65,6 @@ func sendWsRequest(conn *ws.Conn, name string, data []byte) (*rpc.JsonRpcResult, return nil, fmt.Errorf("RPC %s error: %s", name, v.Error.Message) } - log.Debugf("RPC %s result: %+v", name, v.Result) + klog.V(4).Infof("RPC %s result: %+v", name, v.Result) return v, nil }