Skip to content

Commit 2233c97

Browse files
fix: update metrics on startup
1 parent e02ee61 commit 2233c97

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

rolling-shutter/keyper/epochkghandler/key.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package epochkghandler
33
import (
44
"bytes"
55
"context"
6-
"strconv"
76

87
"github.com/jackc/pgx/v4"
98
"github.com/jackc/pgx/v4/pgxpool"
@@ -15,7 +14,6 @@ import (
1514
"github.com/shutter-network/shutter/shlib/shcrypto"
1615

1716
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/database"
18-
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/keypermetrics"
1917
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
2018
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2p"
2119
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg"
@@ -54,31 +52,25 @@ func (handler *DecryptionKeyHandler) ValidateMessage(ctx context.Context, msg p2
5452
return pubsub.ValidationReject, err
5553
}
5654
if !isKeyper {
57-
keypermetrics.MetricsKeyperIsKeyper.WithLabelValues(strconv.FormatInt(eon, 10)).Set(0)
5855
log.Debug().Int64("eon", eon).Msg("Ignoring decryptionKey for eon; we're not a Keyper")
5956
return pubsub.ValidationReject, nil
6057
}
61-
keypermetrics.MetricsKeyperIsKeyper.WithLabelValues(strconv.FormatInt(eon, 10)).Set(1)
6258

6359
dkgResultDB, err := queries.GetDKGResultForKeyperConfigIndex(ctx, eon)
6460
if errors.Is(err, pgx.ErrNoRows) {
65-
keypermetrics.MetricsKeyperSuccessfulDKG.WithLabelValues(strconv.FormatInt(eon, 10)).Set(0)
6661
return pubsub.ValidationReject, errors.Errorf("no DKG result found for eon %d", eon)
6762
}
6863
if err != nil {
69-
keypermetrics.MetricsKeyperSuccessfulDKG.WithLabelValues(strconv.FormatInt(eon, 10)).Set(0)
7064
return pubsub.ValidationReject, errors.Wrapf(err, "failed to get dkg result for eon %d from db", eon)
7165
}
7266
if !dkgResultDB.Success {
73-
keypermetrics.MetricsKeyperSuccessfulDKG.WithLabelValues(strconv.FormatInt(eon, 10)).Set(0)
7467
return pubsub.ValidationReject, errors.Errorf("no successful DKG result found for eon %d", eon)
7568
}
7669
pureDKGResult, err := shdb.DecodePureDKGResult(dkgResultDB.PureResult)
7770
if err != nil {
7871
return pubsub.ValidationReject, errors.Wrapf(err, "error while decoding pure DKG result for eon %d", eon)
7972
}
8073

81-
keypermetrics.MetricsKeyperSuccessfulDKG.WithLabelValues(strconv.FormatInt(eon, 10)).Set(1)
8274
if len(decryptionKeys.Keys) == 0 {
8375
return pubsub.ValidationReject, errors.New("no keys in message")
8476
}

rolling-shutter/keyper/keyper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (kpr *KeyperCore) Start(ctx context.Context, runner service.Runner) error {
147147
messageSender := fx.NewRPCMessageSender(shuttermintClient, config.Ethereum.PrivateKey.Key)
148148

149149
if kpr.config.Metrics.Enabled {
150-
keypermetrics.InitMetrics()
150+
keypermetrics.InitMetrics(kpr.dbpool, kpr.config)
151151
epochkghandler.InitMetrics()
152152
deployment.InitMetrics()
153153
kpr.metricsServer = metricsserver.New(kpr.config.Metrics)

rolling-shutter/keyper/keypermetrics/metrics.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package keypermetrics
22

33
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/jackc/pgx/v4/pgxpool"
48
"github.com/prometheus/client_golang/prometheus"
9+
"github.com/rs/zerolog/log"
10+
11+
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/database"
12+
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/kprconfig"
513
)
614

715
var MetricsKeyperCurrentBlockL1 = prometheus.NewGauge(
@@ -89,7 +97,7 @@ var MetricsKeyperSuccessfulDKG = prometheus.NewGaugeVec(
8997
[]string{"eon"},
9098
)
9199

92-
func InitMetrics() {
100+
func InitMetrics(dbpool *pgxpool.Pool, config *kprconfig.Config) {
93101
prometheus.MustRegister(MetricsKeyperCurrentBlockL1)
94102
prometheus.MustRegister(MetricsKeyperCurrentBlockShuttermint)
95103
prometheus.MustRegister(MetricsKeyperCurrentEon)
@@ -99,4 +107,33 @@ func InitMetrics() {
99107
prometheus.MustRegister(MetricsKeyperCurrentBatchConfigIndex)
100108
prometheus.MustRegister(MetricsKeyperBatchConfigInfo)
101109
prometheus.MustRegister(MetricsKeyperSuccessfulDKG)
110+
111+
queries := database.New(dbpool)
112+
eons, err := queries.GetAllEons(context.Background())
113+
if err != nil {
114+
log.Error().Err(err).Msg("Failed to get all eons")
115+
return
116+
}
117+
keyperIndex, isKeyper, err := queries.GetKeyperIndex(context.Background(), eons[len(eons)-1].KeyperConfigIndex, config.GetAddress())
118+
if err != nil {
119+
log.Error().Err(err).Msg("Failed to get keyper index")
120+
return
121+
}
122+
if isKeyper {
123+
MetricsKeyperIsKeyper.WithLabelValues(strconv.FormatInt(keyperIndex, 10)).Set(1)
124+
} else {
125+
MetricsKeyperIsKeyper.WithLabelValues(strconv.FormatInt(keyperIndex, 10)).Set(0)
126+
}
127+
128+
dkgResult, err := queries.GetDKGResultForKeyperConfigIndex(context.Background(), eons[len(eons)-1].KeyperConfigIndex)
129+
if err != nil {
130+
MetricsKeyperSuccessfulDKG.WithLabelValues(strconv.FormatInt(eons[len(eons)-1].Eon, 10)).Set(0)
131+
log.Error().Err(err).Msg("Failed to get dkg result")
132+
return
133+
}
134+
if dkgResult.Success {
135+
MetricsKeyperSuccessfulDKG.WithLabelValues(strconv.FormatInt(eons[len(eons)-1].Eon, 10)).Set(1)
136+
} else {
137+
MetricsKeyperSuccessfulDKG.WithLabelValues(strconv.FormatInt(eons[len(eons)-1].Eon, 10)).Set(0)
138+
}
102139
}

0 commit comments

Comments
 (0)