forked from sei-protocol/sei-cosmos-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sei.go
84 lines (67 loc) · 2.3 KB
/
sei.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
// original here - https://gist.github.com/jumanzii/031cfea1b2aa3c2a43b63aa62a919285
package main
import (
"context"
"net/http"
"sync"
"time"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types"
"google.golang.org/grpc"
)
type votePenaltyCounter struct {
MissCount string `json:"miss_count"`
AbstainCount string `json:"abstain_count"`
SuccessCount string `json:"success_count"`
}
func OracleMetricHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
requestStart := time.Now()
sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()
address := r.URL.Query().Get("address")
votePenaltyCount := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "cosmos_oracle_vote_penalty_count",
Help: "Vote penalty miss count",
ConstLabels: ConstLabels,
},
[]string{"type"},
)
registry := prometheus.NewRegistry()
registry.MustRegister(votePenaltyCount)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().Msg("Started querying oracle feeder metrics")
queryStart := time.Now()
oracleClient := oracletypes.NewQueryClient(grpcConn)
response, err := oracleClient.VotePenaltyCounter(context.Background(), &oracletypes.QueryVotePenaltyCounterRequest{ValidatorAddr: address})
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get oracle feeder metrics")
return
}
sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying oracle feeder metrics")
missCount := float64(response.VotePenaltyCounter.MissCount)
abstainCount := float64(response.VotePenaltyCounter.AbstainCount)
successCount := float64(response.VotePenaltyCounter.SuccessCount)
votePenaltyCount.WithLabelValues("miss").Add(missCount)
votePenaltyCount.WithLabelValues("abstain").Add(abstainCount)
votePenaltyCount.WithLabelValues("success").Add(successCount)
}()
wg.Wait()
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
sublogger.Info().
Str("method", "GET").
Str("endpoint", "/metrics/oracle").
Float64("request-time", time.Since(requestStart).Seconds()).
Msg("Request processed")
}