Skip to content

Commit

Permalink
Replace logrus and go-flags with klog and built-in flag package.
Browse files Browse the repository at this point in the history
  • Loading branch information
wi1dcard committed Jan 7, 2021
1 parent 425acac commit 8fde098
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 58 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
23 changes: 4 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
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=
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=
Expand All @@ -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=
Expand All @@ -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=
12 changes: 6 additions & 6 deletions handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

ws "github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"k8s.io/klog/v2"
)

type Prober interface {
Expand All @@ -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)))
Expand All @@ -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)

Expand Down
44 changes: 21 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
@@ -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:"<ws|wss>://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)
}
}
6 changes: 3 additions & 3 deletions probes/liveness_block_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions probes/liveness_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 8fde098

Please sign in to comment.