-
Notifications
You must be signed in to change notification settings - Fork 0
/
shares.go
122 lines (101 loc) · 2.74 KB
/
shares.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
package main
import (
"github.com/celestiaorg/celestia-node/api/rpc/client"
"github.com/celestiaorg/go-square/v2/share"
"net/http"
"strconv"
"sync"
"time"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
namespaces = []InternalNamespace{
{
Name: "TxNamespace",
Namespace: share.TxNamespace,
},
{
Name: "IntermediateStateRootsNamespace",
Namespace: share.IntermediateStateRootsNamespace,
},
{
Name: "PayForBlobNamespace",
Namespace: share.PayForBlobNamespace,
},
{
Name: "PrimaryReservedPaddingNamespace",
Namespace: share.PrimaryReservedPaddingNamespace,
},
{
Name: "MaxPrimaryReservedNamespace",
Namespace: share.MaxPrimaryReservedNamespace,
},
{
Name: "MinSecondaryReservedNamespace",
Namespace: share.MinSecondaryReservedNamespace,
},
}
)
type InternalNamespace struct {
Name string
Namespace share.Namespace
}
func SharesHandler(w http.ResponseWriter, r *http.Request, rpcClient *client.Client) {
requestStart := time.Now()
sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()
sharesByNamespaceGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "shares_by_namespace",
Help: "All shares with proofs within a specific namespace by local height",
ConstLabels: ConstLabels,
},
[]string{
"namespace",
"local_height",
},
)
registry := prometheus.NewRegistry()
registry.MustRegister(sharesByNamespaceGauge)
sublogger.Debug().Msg("Started querying shares data")
peersDataStart := time.Now()
localHead, err := rpcClient.Header.LocalHead(ctx)
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get local head data")
return
}
var wg sync.WaitGroup
for _, ns := range namespaces {
wg.Add(1)
go func(ns InternalNamespace) {
defer wg.Done()
sharesByNamespaceResponse, err := rpcClient.Share.GetNamespaceData(ctx, localHead.Height(), ns.Namespace)
if err != nil {
sublogger.Error().
Err(err).
Msgf("Could not get shares by namespace: %s", ns)
return
}
sharesByNamespaceGauge.With(prometheus.Labels{
"namespace": ns.Name,
"local_height": strconv.FormatUint(localHead.Height(), 10),
}).Set(float64(len(sharesByNamespaceResponse.Flatten())))
}(ns)
}
wg.Wait()
sublogger.Debug().
Float64("request-time", time.Since(peersDataStart).Seconds()).
Msg("Finished querying shares data")
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
sublogger.Info().
Str("method", "GET").
Str("endpoint", "/metrics/shares").
Float64("request-time", time.Since(requestStart).Seconds()).
Msg("Request processed")
}