@@ -2,12 +2,11 @@ package keypermetrics
22
33import (
44 "context"
5- "strconv"
6- "strings"
7-
85 "github.com/jackc/pgx/v4/pgxpool"
96 "github.com/prometheus/client_golang/prometheus"
107 "github.com/rs/zerolog/log"
8+ "strconv"
9+ "strings"
1110
1211 "github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/database"
1312 "github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/kprconfig"
@@ -141,20 +140,50 @@ func InitMetrics(dbpool *pgxpool.Pool, config kprconfig.Config) {
141140 }
142141
143142 eons , err := queries .GetAllEons (ctx )
144- if err != nil || len (eons ) == 0 {
145- log .Error ().Err (err ).Msg ("keypermetrics | No eons found or failed to fetch eons" )
146- return
143+ if err != nil {
144+ log .Error ().Err (err ).Msg ("keypermetrics | Failed to fetch eons" )
145+ } else if len (eons ) == 0 {
146+ log .Warn ().Msg ("keypermetrics | No eons found" )
147147 }
148148
149- currentEon := eons [len (eons )- 1 ]
149+ if len (eons ) > 0 {
150+ currentEon := eons [len (eons )- 1 ]
150151
151- MetricsKeyperCurrentEon .Set (float64 (currentEon .Eon ))
152+ MetricsKeyperCurrentEon .Set (float64 (currentEon .Eon ))
152153
153- MetricsKeyperCurrentBatchConfigIndex .Set (float64 (currentEon .KeyperConfigIndex ))
154+ MetricsKeyperCurrentBatchConfigIndex .Set (float64 (currentEon .KeyperConfigIndex ))
154155
155- for _ , eon := range eons {
156- eonStr := strconv .FormatInt (eon .Eon , 10 )
157- MetricsKeyperEonStartBlock .WithLabelValues (eonStr ).Set (float64 (eon .ActivationBlockNumber ))
156+ for _ , eon := range eons {
157+ eonStr := strconv .FormatInt (eon .Eon , 10 )
158+ MetricsKeyperEonStartBlock .WithLabelValues (eonStr ).Set (float64 (eon .ActivationBlockNumber ))
159+ }
160+
161+ // Populate MetricsKeyperDKGStatus
162+ dkgResults , err := queries .GetAllDKGResults (ctx )
163+ if err != nil {
164+ log .Error ().Err (err ).Msg ("keypermetrics | Failed to fetch DKG results" )
165+ } else {
166+ dkgResultMap := make (map [int64 ]database.DkgResult )
167+ for _ , result := range dkgResults {
168+ dkgResultMap [result .Eon ] = result
169+ }
170+
171+ // Set DKG status for all eons
172+ for _ , eon := range eons {
173+ eonStr := strconv .FormatInt (eon .Eon , 10 )
174+
175+ if dkgResult , exists := dkgResultMap [eon .Eon ]; exists {
176+ var dkgStatusValue float64
177+ if dkgResult .Success {
178+ dkgStatusValue = 1
179+ }
180+ MetricsKeyperDKGStatus .WithLabelValues (eonStr ).Set (dkgStatusValue )
181+ } else {
182+ // No DKG result found for this eon, set to 0
183+ MetricsKeyperDKGStatus .WithLabelValues (eonStr ).Set (0 )
184+ }
185+ }
186+ }
158187 }
159188
160189 // Populate MetricsKeyperBatchConfigInfo && MetricsKeyperIsKeyper
@@ -188,30 +217,6 @@ func InitMetrics(dbpool *pgxpool.Pool, config kprconfig.Config) {
188217 }
189218 }
190219
191- // Populate MetricsKeyperDKGStatus
192- dkgResults , err := queries .GetAllDKGResults (ctx )
193- if err != nil {
194- log .Error ().Err (err ).Msg ("keypermetrics | Failed to fetch DKG results" )
195- } else {
196- dkgResultMap := make (map [int64 ]database.DkgResult )
197- for _ , result := range dkgResults {
198- dkgResultMap [result .Eon ] = result
199- }
200-
201- // Set DKG status for all eons
202- for _ , eon := range eons {
203- eonStr := strconv .FormatInt (eon .Eon , 10 )
220+ log .Info ().Msg ("keypermetrics | Metrics population completed" )
204221
205- if dkgResult , exists := dkgResultMap [eon .Eon ]; exists {
206- var dkgStatusValue float64
207- if dkgResult .Success {
208- dkgStatusValue = 1
209- }
210- MetricsKeyperDKGStatus .WithLabelValues (eonStr ).Set (dkgStatusValue )
211- } else {
212- // No DKG result found for this eon, set to 0
213- MetricsKeyperDKGStatus .WithLabelValues (eonStr ).Set (0 )
214- }
215- }
216- }
217- }
222+ }
0 commit comments