-
Notifications
You must be signed in to change notification settings - Fork 2
/
celbridge_export.go
116 lines (95 loc) · 2.88 KB
/
celbridge_export.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
113
114
115
116
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
localHeight = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "bridge_local_height",
Help: "Local height of the Celestia node",
})
networkHeight = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "bridge_network_height",
Help: "Network height of the Celestia node",
})
)
func init() {
prometheus.MustRegister(localHeight)
prometheus.MustRegister(networkHeight)
}
func main() {
listenPort := flag.String("listen.port", "8380", "port to listen on")
endpoint := flag.String("endpoint", "http://localhost:26658", "endpoint to connect to")
p2pNetwork := flag.String("p2p.network", "blockspacerace", "network to use")
flag.Parse()
http.Handle("/metrics", promhttp.Handler())
go func() {
client := &http.Client{}
authToken := getAuthToken(*p2pNetwork)
for {
updateMetrics(client, authToken, *endpoint)
time.Sleep(5 * time.Second)
}
}()
fmt.Printf("Celestia Bridge Exporter started on port %s\n", *listenPort)
http.ListenAndServe(":"+*listenPort, nil)
}
func updateMetrics(client *http.Client, authToken, endpoint string) {
local, network, err := getHeights(client, authToken, endpoint)
if err != nil {
fmt.Println("Error getting heights:", err)
return
}
localHeight.Set(float64(local))
networkHeight.Set(float64(network))
}
func getHeights(client *http.Client, authToken, endpoint string) (int, int, error) {
local := getHeight(client, authToken, "header.LocalHead", endpoint)
network := getHeight(client, authToken, "header.NetworkHead", endpoint)
return local, network, nil
}
func getHeight(client *http.Client, authToken, method, endpoint string) int {
reqData := map[string]interface{}{
"jsonrpc": "2.0",
"id": 1,
"method": method,
"params": []interface{}{},
}
reqBytes, _ := json.Marshal(reqData)
req, _ := http.NewRequest("POST", endpoint, bytes.NewReader(reqBytes))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authToken))
resp, err := client.Do(req)
if err != nil {
return 0
}
defer resp.Body.Close()
respBytes, _ := ioutil.ReadAll(resp.Body)
var respData map[string]interface{}
json.Unmarshal(respBytes, &respData)
heightStr := respData["result"].(map[string]interface{})["header"].(map[string]interface{})["height"].(string)
height, err := strconv.Atoi(heightStr)
if err != nil {
fmt.Printf("Error converting height to int: %v\n", err)
return 0
}
return height
}
func getAuthToken(p2pNetwork string) string {
out, err := exec.Command("celestia", "bridge", "auth", "admin", "--p2p.network", p2pNetwork).Output()
if err != nil {
fmt.Println("Error getting auth token:", err)
return ""
}
return strings.TrimSpace(string(out))
}