-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (90 loc) · 3.94 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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type blockchainStats struct {
Data struct {
Attributes struct {
TipBlockNumber string `json:"tip_block_number"`
} `json:"attributes"`
} `json:"data"`
}
func main() {
// 获取 CKB 浏览器地址变量
browserURL := os.Getenv("BROWSER_URL")
if browserURL == "" {
log.Fatalf("BROWSER_URL environment variable not set")
}
// 获取抓取时间变量
timeoutStr := os.Getenv("TIMEOUT")
if timeoutStr == "" {
timeoutStr = "30s" // 默认 30 秒抓取一次
}
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
log.Fatalf("Failed to parse timeout duration: %v", err)
}
// 注册自定义指标
blockchainHeight := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "blockchain_height",
Help: "The current height of the blockchain",
})
prometheus.MustRegister(blockchainHeight)
// 注册接口状态指标
apiStatus := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "api_status",
Help: "The status of the API endpoint",
})
prometheus.MustRegister(apiStatus)
// 每隔一段时间抓取区块高度数据并更新指标
go func() {
for {
client := &http.Client{
Timeout: 5 * time.Second, // 设置请求超时时间
}
req, err := http.NewRequest("GET", browserURL+"/api/v1/statistics", nil)
if err != nil {
log.Printf("Failed to create request: %v", err)
continue
}
req.Header.Set("Content-Type", "application/vnd.api+json")
req.Header.Set("Accept", "application/vnd.api+json")
resp, err := client.Do(req)
if err != nil {
log.Printf("Failed to fetch data from CKB browser: %v", err)
apiStatus.Set(0) // 设置接口状态为失败
continue
}
defer resp.Body.Close()
var stats blockchainStats
err = json.NewDecoder(resp.Body).Decode(&stats)
if err != nil {
log.Printf("Failed to parse response from CKB browser: %v", err)
apiStatus.Set(0) // 设置接口状态为失败
continue
}
blockNumberStr := stats.Data.Attributes.TipBlockNumber
blockNumber, err := strconv.ParseFloat(blockNumberStr, 64)
if err != nil {
log.Printf("Failed to convert block number to float64: %v", err)
apiStatus.Set(0) // 设置接口状态为失败
continue
}
log.Printf("Fetched blockchain height: %s, response status: %s", blockNumberStr, resp.Status)
blockchainHeight.Set(blockNumber)
apiStatus.Set(1) // 设置接口状态为成功
time.Sleep(timeout)
}
}()
// 启动 HTTP 服务器并暴露指
// 启动 HTTP 服务器并暴露指标
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}