-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (190 loc) · 6.37 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// A minimal example of how to include Prometheus instrumentation.
package main
import (
"encoding/json"
"crypto/tls"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const namespace = "aleo"
var (
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr}
listenAddress = flag.String("web.listen-address", ":9200",
"Address to listen on for telemetry")
metricsPath = flag.String("web.telemetry-path", "/metrics",
"Path under which to expose metrics")
// Metrics
nodeType = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "type"),
"Type of node.",
nil, nil,
)
nodeStatus = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "status"),
"Node status.",
[]string{"channel"}, nil,
)
connectedSyncNodes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "connected_sync_nodes"),
"Number of connected sync nodes.",
[]string{"channel"}, nil,
)
connectedPeers = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "connected_peers"),
"Number of connected peers.",
[]string{"channel"}, nil,
)
candidatePeers = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "candidate_peers"),
"Number of candidate peers",
[]string{"channel"}, nil,
)
cumulativeWeight = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "cumulative_weight"),
".",
[]string{"channel"}, nil,
)
latestBlockHeight = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "latest_block_height"),
"Latest block height of node.",
[]string{"channel"}, nil,
)
blocksMined = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "blocks_mined"),
"Blocks mined after 18000 block.",
[]string{"channel"}, nil,
)
blocksMinedCalibrate = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "blocks_mined_calibrate"),
"Blocks mined before 18000 block.",
[]string{"channel"}, nil,
)
)
type Response struct {
Number int `json:"number"`
CandidatePeers []string `json:"candidate_peers"`
ConnectedPeers []string `json:"connected_peers"`
LatestBlockHash string `json:"latest_block_hash"`
BlockHeight int `json:latest_block_height`
CumulativeWeight int `json:cumulative_weight`
CandidatePeers int `json:candidate_peers`
ConnectedPeers int `json:connected_peers`
ConnectedSyncNodes int `json:connected_sync_nodes`
Software string `json:software`
Status string `json:status`
Type string `json:type`
Version float `json:version`
}
type Exporter struct {
aleorpcEndpoint, aleorpcUsername, aleorpcPassword string
}
func NewExporter(aleorpcEndpoint string, aleorpcUsername string, aleorpcPassword string) *Exporter {
return &Exporter{
aleorpcEndpoint: aleorpcEndpoint,
aleorpcUsername: aleorpcUsername,
aleorpcPassword: aleorpcPassword,
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- nodeType
ch <- nodeStatus
ch <- connectedSyncNodes
ch <- connectedPeers
ch <- candidatePeers
ch <- cumulativeWeight
ch <- latestBlockHeight
ch <- blocksMined
ch <- blocksMinedCalibrate
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.UpdateMetrics(ch)
}
func (e *Exporter) UpdateMetrics(ch chan<- prometheus.Metric) {
requestString := map[string]string{"jsonrpc": "2.0", "id":"", "method": "getnodestate", "params": [] }
jsonValue, _ := json.Marshal(requestString)
req, err := http.NewRequest(http.MethodGet, e.aleorpcEndpoint, requestString)
req.Header.Set("user-agent": "aleo-exporter-go/0.1")
if err != nil {
log.Fatal(err)
}
// This one line implements the authentication required for the task.
if e.aleorpcUsername != nil && e.aleorpcPassword != nil {
req.SetBasicAuth(e.aleorpcUsername, e.aleorpcPassword)
}
// Make request and show output.
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// fmt.Println(string(body))
// we unmarshal our byteArray which contains our
Response := Response{}
err = json.Unmarshal(body, &Response)
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(channelStatsList.Channels); i++ {
channelName := channelIdNameMap[channelStatsList.Channels[i].ChannelId]
channelReceived, _ := strconv.ParseFloat(channelStatsList.Channels[i].Received, 64)
ch <- prometheus.MustNewConstMetric(
messagesReceived, prometheus.GaugeValue, channelReceived, channelName,
)
channelSent, _ := strconv.ParseFloat(channelStatsList.Channels[i].Sent, 64)
ch <- prometheus.MustNewConstMetric(
messagesSent, prometheus.GaugeValue, channelSent, channelName,
)
channelError, _ := strconv.ParseFloat(channelStatsList.Channels[i].Error, 64)
ch <- prometheus.MustNewConstMetric(
messagesErrored, prometheus.GaugeValue, channelError, channelName,
)
channelFiltered, _ := strconv.ParseFloat(channelStatsList.Channels[i].Filtered, 64)
ch <- prometheus.MustNewConstMetric(
messagesFiltered, prometheus.GaugeValue, channelFiltered, channelName,
)
channelQueued, _ := strconv.ParseFloat(channelStatsList.Channels[i].Queued, 64)
ch <- prometheus.MustNewConstMetric(
messagesQueued, prometheus.GaugeValue, channelQueued, channelName,
)
}
log.Println("Endpoint scraped")
}
func main() {
err := godotenv.Load()
if err != nil {
log.Println("Error loading .env file, assume env variables are set.")
}
flag.Parse()
aleorpcEndpoint := os.Getenv("ALEO_RPC_ENDPOINT")
aleorpcUsername := os.Getenv("ALEO_RPC_USERNAME")
aleorpcPassword := os.Getenv("ALEO_RPC_PASSWORD")
exporter := NewExporter(aleorpcEndpoint, aleorpcUsername, aleorpcPassword)
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Aleo Exporter</title></head>
<body>
<h1>Aleo Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}