-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
73 lines (61 loc) · 2.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"flag"
"net/http"
"strings"
"github.com/darwinia-network/node-liveness-probe/handlers"
"github.com/darwinia-network/node-liveness-probe/probes"
"k8s.io/klog/v2"
)
type stringListValue []string
func (i *stringListValue) String() string {
return strings.Join(*i, ",")
}
func (i *stringListValue) Set(s string) error {
*i = append(*i, s)
return nil
}
var (
buildVersion = "dev"
buildCommit = "none"
buildDate = "unknown"
wsEndpoints stringListValue
metricsEndpoint = flag.String("metrics-endpoint", "http://127.0.0.1:9615/metrics", "Substrate node metrics endpoint; may be specified multiple times to probe both relaychain and parachain sequentially (default \"http://127.0.0.1:9615/metrics\")")
useMetrics = flag.Bool("use-metrics", false, "Use metrics to check node's health,if useMetrics equals false, /healthz_block will use ws to check block")
finalizedBlockThresholdSeconds = flag.Int64("finalized-block-threshold-seconds", 300, "If the finalized block does not increase beyond this time, the node is considered unhealthy. This value is invalid if useMetrics is false.")
listen = flag.String("listen", ":49944", "Listen address")
blockThresholdSeconds = flag.Float64("block-threshold-seconds", 300, "/healthz_block returns unhealthy if node's latest block is older than threshold")
)
func initFlags() {
klog.InitFlags(nil)
flag.Var(&wsEndpoints, "ws-endpoint", "Substrate node WebSocket endpoint; may be specified multiple times to probe both relaychain and parachain sequentially (default \"ws://127.0.0.1:9944\")")
flag.Set("logtostderr", "true")
flag.Parse()
if len(wsEndpoints) == 0 {
wsEndpoints = append(wsEndpoints, "ws://127.0.0.1:9944")
}
}
func main() {
initFlags()
klog.Infof("Substrate Node Livness Probe %v-%v (built %v)\n", buildVersion, buildCommit, buildDate)
http.Handle("/healthz", &handlers.ProbeHandler{
Prober: &probes.LivenessProbe{},
WsEndpoints: wsEndpoints,
MetricsEndpoint: *metricsEndpoint,
FinalizedBlockThresholdSeconds: *finalizedBlockThresholdSeconds,
UseMetrics: *useMetrics,
})
http.Handle("/healthz_block", &handlers.ProbeHandler{
Prober: &probes.LivenessBlockProbe{BlockThresholdSeconds: *blockThresholdSeconds},
WsEndpoints: wsEndpoints,
})
http.Handle("/readiness", &handlers.ProbeHandler{
Prober: &probes.ReadinessProbe{},
WsEndpoints: wsEndpoints,
})
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)
}
}