forked from yujinlim/blockchain-monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (93 loc) · 2.29 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/yujinlim/blockchain-monitoring/coin"
)
// parse cli
func main() {
var client coin.Coin
var err error
log.Println("starting...")
// load env
err = godotenv.Load()
if err != nil {
// ignore err
log.Println(err)
}
coinType := os.Getenv("COIN_TYPE")
port := os.Getenv("PORT")
namespace := os.Getenv("COIN_NAMESPACE")
host := os.Getenv("COIN_HOST")
username := os.Getenv("COIN_USER")
password := os.Getenv("COIN_PASSWORD")
network := os.Getenv("COIN_NETWORK")
if len(namespace) == 0 {
namespace = "coin"
}
if len(port) == 0 {
port = "8080"
}
addr := strings.Join([]string{":", port}, "")
if coinType == "ethereum" {
client, err = coin.NewEthCoin(host, coin.NetworkType(network))
} else {
client, err = coin.NewBitcoinCoin(host, username, password, coin.Type(coinType), coin.NetworkType(network))
}
if err != nil {
log.Fatal(err)
os.Exit(1)
}
log.Print("creating...")
start(client, namespace, addr)
}
// start setup prometheus and http
func start(coin coin.Coin, namespace string, addr string) {
blockCounter := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "block_total",
Help: "Total block count for current node",
})
statusCounter := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "status",
Help: "Status of coin node",
})
diffCounter := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "block_differences",
Help: "Block differences between current node and other api service",
})
prometheus.MustRegister(blockCounter, statusCounter, diffCounter)
// spin go routine
// monitor block counter
go func() {
for {
coin.MonitorCount(blockCounter)
time.Sleep(30 * time.Second)
}
}()
// monitor node state
go func() {
for {
coin.MonitorStatus(statusCounter)
time.Sleep(30 * time.Second)
}
}()
// monitor block differences
go func() {
for {
coin.MonitorDifferences(diffCounter)
time.Sleep(30 * time.Second)
}
}()
// setup routes with handler
http.Handle("/metrics", prometheusHandler())
http.Handle("/probe", probHandler(coin))
log.Fatal(http.ListenAndServe(addr, nil))
}