Skip to content

Commit 688397f

Browse files
authored
Merge pull request #618 from shutter-network/fix/fix-metrics
Fix metrics to generate from DB and avoid early returns
2 parents 5e08d65 + 21049ae commit 688397f

File tree

2 files changed

+103
-56
lines changed

2 files changed

+103
-56
lines changed

rolling-shutter/keyper/keypermetrics/metrics.go

Lines changed: 101 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package keypermetrics
33
import (
44
"context"
55
"strconv"
6+
"strings"
67

78
"github.com/jackc/pgx/v4/pgxpool"
89
"github.com/prometheus/client_golang/prometheus"
@@ -88,7 +89,7 @@ var MetricsKeyperBatchConfigInfo = prometheus.NewGaugeVec(
8889
},
8990
[]string{"batch_config_index", "keyper_addresses"})
9091

91-
var MetricsKeyperDKGstatus = prometheus.NewGaugeVec(
92+
var MetricsKeyperDKGStatus = prometheus.NewGaugeVec(
9293
prometheus.GaugeOpts{
9394
Namespace: "shutter",
9495
Subsystem: "keyper",
@@ -98,6 +99,23 @@ var MetricsKeyperDKGstatus = prometheus.NewGaugeVec(
9899
[]string{"eon"},
99100
)
100101

102+
var MetricsKeyperEthAddress = prometheus.NewGaugeVec(
103+
prometheus.GaugeOpts{
104+
Namespace: "shutter",
105+
Subsystem: "keyper",
106+
Name: "address",
107+
Help: "Ethereum address of the Keyper",
108+
}, []string{"address"})
109+
110+
var MetricsExecutionClientVersion = prometheus.NewGaugeVec(
111+
prometheus.GaugeOpts{
112+
Namespace: "shutter",
113+
Subsystem: "keyper",
114+
Name: "execution_client_version",
115+
Help: "Version of the execution client",
116+
},
117+
[]string{"version"})
118+
101119
func InitMetrics(dbpool *pgxpool.Pool, config kprconfig.Config) {
102120
prometheus.MustRegister(MetricsKeyperCurrentBlockL1)
103121
prometheus.MustRegister(MetricsKeyperCurrentBlockShuttermint)
@@ -107,69 +125,98 @@ func InitMetrics(dbpool *pgxpool.Pool, config kprconfig.Config) {
107125
prometheus.MustRegister(MetricsKeyperCurrentPhase)
108126
prometheus.MustRegister(MetricsKeyperCurrentBatchConfigIndex)
109127
prometheus.MustRegister(MetricsKeyperBatchConfigInfo)
110-
prometheus.MustRegister(MetricsKeyperDKGstatus)
128+
prometheus.MustRegister(MetricsKeyperDKGStatus)
129+
prometheus.MustRegister(MetricsKeyperEthAddress)
130+
prometheus.MustRegister(MetricsExecutionClientVersion)
111131

132+
ctx := context.Background()
112133
queries := database.New(dbpool)
113-
eons, err := queries.GetAllEons(context.Background())
114-
if err != nil {
115-
log.Error().Err(err).Msg("keypermetrics | Failed to get all eons")
116-
return
117-
}
118-
keyperIndex, isKeyper, err := queries.GetKeyperIndex(context.Background(), eons[len(eons)-1].KeyperConfigIndex, config.GetAddress())
119-
if err != nil {
120-
log.Error().Err(err).Msg("keypermetrics | Failed to get keyper index")
121-
return
122-
}
123-
if isKeyper {
124-
MetricsKeyperIsKeyper.WithLabelValues(strconv.FormatInt(keyperIndex, 10)).Set(1)
134+
135+
MetricsKeyperEthAddress.WithLabelValues(config.GetAddress().Hex()).Set(1)
136+
137+
if version, err := chainsync.GetClientVersion(ctx, config.Ethereum.EthereumURL); err != nil {
138+
log.Error().Err(err).Msg("keypermetrics | Failed to get execution client version")
125139
} else {
126-
MetricsKeyperIsKeyper.WithLabelValues(strconv.FormatInt(keyperIndex, 10)).Set(0)
140+
MetricsExecutionClientVersion.WithLabelValues(version).Set(1)
127141
}
128142

129-
dkgResult, err := queries.GetDKGResultForKeyperConfigIndex(context.Background(), eons[len(eons)-1].KeyperConfigIndex)
143+
eons, err := queries.GetAllEons(ctx)
130144
if err != nil {
131-
MetricsKeyperDKGstatus.WithLabelValues(strconv.FormatInt(eons[len(eons)-1].Eon, 10)).Set(0)
132-
log.Error().Err(err).Msg("keypermetrics | Failed to get dkg result")
133-
return
145+
log.Error().Err(err).Msg("keypermetrics | Failed to fetch eons")
146+
} else if len(eons) == 0 {
147+
log.Warn().Msg("keypermetrics | No eons found")
134148
}
135-
if dkgResult.Success {
136-
MetricsKeyperDKGstatus.WithLabelValues(strconv.FormatInt(eons[len(eons)-1].Eon, 10)).Set(1)
137-
} else {
138-
MetricsKeyperDKGstatus.WithLabelValues(strconv.FormatInt(eons[len(eons)-1].Eon, 10)).Set(0)
149+
150+
if len(eons) > 0 {
151+
currentEon := eons[len(eons)-1]
152+
153+
MetricsKeyperCurrentEon.Set(float64(currentEon.Eon))
154+
155+
MetricsKeyperCurrentBatchConfigIndex.Set(float64(currentEon.KeyperConfigIndex))
156+
157+
for _, eon := range eons {
158+
eonStr := strconv.FormatInt(eon.Eon, 10)
159+
MetricsKeyperEonStartBlock.WithLabelValues(eonStr).Set(float64(eon.ActivationBlockNumber))
160+
}
161+
162+
// Populate MetricsKeyperDKGStatus
163+
dkgResults, err := queries.GetAllDKGResults(ctx)
164+
if err != nil {
165+
log.Error().Err(err).Msg("keypermetrics | Failed to fetch DKG results")
166+
} else {
167+
dkgResultMap := make(map[int64]database.DkgResult)
168+
for _, result := range dkgResults {
169+
dkgResultMap[result.Eon] = result
170+
}
171+
172+
// Set DKG status for all eons
173+
for _, eon := range eons {
174+
eonStr := strconv.FormatInt(eon.Eon, 10)
175+
176+
if dkgResult, exists := dkgResultMap[eon.Eon]; exists {
177+
var dkgStatusValue float64
178+
if dkgResult.Success {
179+
dkgStatusValue = 1
180+
}
181+
MetricsKeyperDKGStatus.WithLabelValues(eonStr).Set(dkgStatusValue)
182+
} else {
183+
// No DKG result found for this eon, set to 0
184+
MetricsKeyperDKGStatus.WithLabelValues(eonStr).Set(0)
185+
}
186+
}
187+
}
139188
}
140189

141-
version, err := chainsync.GetClientVersion(context.Background(), config.Ethereum.EthereumURL)
190+
// Populate MetricsKeyperBatchConfigInfo && MetricsKeyperIsKeyper
191+
batchConfigs, err := queries.GetBatchConfigs(ctx)
142192
if err != nil {
143-
log.Error().Err(err).Msg("execution_client_version metrics | Failed to get execution client version")
144-
return
193+
log.Error().Err(err).Msg("keypermetrics | Failed to fetch batch configs")
194+
} else {
195+
currentAddress := config.GetAddress().Hex()
196+
197+
for _, batchConfig := range batchConfigs {
198+
batchConfigIndexStr := strconv.Itoa(int(batchConfig.KeyperConfigIndex))
199+
200+
// Join keyper addresses for the label
201+
keyperAddresses := strings.Join(batchConfig.Keypers, ",")
202+
MetricsKeyperBatchConfigInfo.WithLabelValues(batchConfigIndexStr, keyperAddresses).Set(1)
203+
204+
// Check if current node is a keyper in this batch config
205+
isKeyper := false
206+
for _, keyperAddr := range batchConfig.Keypers {
207+
if strings.EqualFold(keyperAddr, currentAddress) {
208+
isKeyper = true
209+
break
210+
}
211+
}
212+
213+
var isKeyperValue float64
214+
if isKeyper {
215+
isKeyperValue = 1
216+
}
217+
MetricsKeyperIsKeyper.WithLabelValues(batchConfigIndexStr).Set(isKeyperValue)
218+
}
145219
}
146220

147-
executionClientVersion := prometheus.NewGauge(
148-
prometheus.GaugeOpts{
149-
Namespace: "shutter",
150-
Subsystem: "keyper",
151-
Name: "execution_client_version",
152-
Help: "Version of the execution client",
153-
ConstLabels: prometheus.Labels{
154-
"version": version,
155-
},
156-
},
157-
)
158-
executionClientVersion.Set(1)
159-
160-
prometheus.MustRegister(executionClientVersion)
161-
metricsKeyperEthAddress := prometheus.NewGauge(
162-
prometheus.GaugeOpts{
163-
Namespace: "shutter",
164-
Subsystem: "keyper",
165-
Name: "address",
166-
Help: "Ethereum address of the Keyper",
167-
ConstLabels: prometheus.Labels{
168-
"address": config.GetAddress().Hex(),
169-
},
170-
},
171-
)
172-
metricsKeyperEthAddress.Set(1)
173-
174-
prometheus.MustRegister(metricsKeyperEthAddress)
221+
log.Info().Msg("keypermetrics | Metrics population completed")
175222
}

rolling-shutter/keyper/smobserver/smstate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func (st *ShuttermintState) finalizeDKG(
489489
dkgresultmsg := shmsg.NewDKGResult(eon, err == nil)
490490

491491
if err != nil {
492-
keypermetrics.MetricsKeyperDKGstatus.WithLabelValues(strconv.FormatUint(eon, 10)).Set(0)
492+
keypermetrics.MetricsKeyperDKGStatus.WithLabelValues(strconv.FormatUint(eon, 10)).Set(0)
493493
log.Error().Err(err).Uint64("eon", eon).Bool("success", false).
494494
Msg("DKG process failed")
495495
dkgerror = sql.NullString{String: err.Error(), Valid: true}
@@ -498,7 +498,7 @@ func (st *ShuttermintState) finalizeDKG(
498498
return err
499499
}
500500
} else {
501-
keypermetrics.MetricsKeyperDKGstatus.WithLabelValues(strconv.FormatUint(eon, 10)).Set(1)
501+
keypermetrics.MetricsKeyperDKGStatus.WithLabelValues(strconv.FormatUint(eon, 10)).Set(1)
502502
log.Info().Uint64("eon", eon).Bool("success", true).Msg("DKG process succeeded")
503503
pureResult, err = shdb.EncodePureDKGResult(&dkgresult)
504504
if err != nil {

0 commit comments

Comments
 (0)