-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
72 lines (58 loc) · 1.67 KB
/
state.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
package main
import (
"github.com/celestiaorg/celestia-node/api/rpc/client"
"net/http"
"time"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func StateHandler(w http.ResponseWriter, r *http.Request, rpcClient *client.Client) {
requestStart := time.Now()
sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()
addressBalanceGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "address_balance",
Help: "Balance of the given address",
ConstLabels: ConstLabels,
},
[]string{
"address",
"denom",
},
)
registry := prometheus.NewRegistry()
registry.MustRegister(addressBalanceGauge)
sublogger.Debug().Msg("Started querying balance for address data")
peersDataStart := time.Now()
balanceResponse, err := rpcClient.State.Balance(ctx)
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get balance data")
return
}
addressResponse, err := rpcClient.State.AccountAddress(ctx)
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get address data")
return
}
sublogger.Debug().
Float64("request-time", time.Since(peersDataStart).Seconds()).
Msg("Finished querying header data")
addressBalanceGauge.With(prometheus.Labels{
"address": addressResponse.String(),
"denom": balanceResponse.Denom,
}).Set(float64(balanceResponse.Amount.Int64()))
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
sublogger.Info().
Str("method", "GET").
Str("endpoint", "/metrics/state").
Float64("request-time", time.Since(requestStart).Seconds()).
Msg("Request processed")
}