2022-05-18 16:12:54 +00:00
// VulcanizeDB
// Copyright © 2022 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2022-05-12 13:52:13 +00:00
package beaconclient
2022-05-17 20:05:15 +00:00
import (
"sync/atomic"
2022-05-25 14:19:29 +00:00
"github.com/prometheus/client_golang/prometheus"
2022-06-09 21:32:46 +00:00
log "github.com/sirupsen/logrus"
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/loghelper"
2022-05-17 20:05:15 +00:00
)
2022-05-12 13:52:13 +00:00
2022-05-25 14:19:29 +00:00
//Create a metric struct and register each channel with prometheus
2022-06-03 16:47:13 +00:00
func CreateBeaconClientMetrics ( ) ( * BeaconClientMetrics , error ) {
2022-05-25 14:19:29 +00:00
metrics := & BeaconClientMetrics {
2022-06-09 21:32:46 +00:00
SlotInserts : 0 ,
ReorgInserts : 0 ,
KnownGapsInserts : 0 ,
KnownGapsProcessed : 0 ,
KnownGapsReprocessError : 0 ,
HeadError : 0 ,
HeadReorgError : 0 ,
2022-05-25 14:19:29 +00:00
}
2022-06-03 16:47:13 +00:00
err := prometheusRegisterHelper ( "slot_inserts" , "Keeps track of the number of slots we have inserted." , & metrics . SlotInserts )
if err != nil {
return nil , err
}
err = prometheusRegisterHelper ( "reorg_inserts" , "Keeps track of the number of reorgs we have inserted." , & metrics . ReorgInserts )
if err != nil {
return nil , err
}
err = prometheusRegisterHelper ( "known_gaps_inserts" , "Keeps track of the number of known gaps we have inserted." , & metrics . KnownGapsInserts )
if err != nil {
return nil , err
}
2022-06-09 21:32:46 +00:00
err = prometheusRegisterHelper ( "known_gaps_reprocess_error" , "Keeps track of the number of known gaps that had errors when reprocessing, but the error was updated successfully." , & metrics . KnownGapsReprocessError )
2022-06-03 16:47:13 +00:00
if err != nil {
return nil , err
}
2022-06-09 21:32:46 +00:00
err = prometheusRegisterHelper ( "known_gaps_processed" , "Keeps track of the number of known gaps we successfully processed." , & metrics . KnownGapsProcessed )
2022-06-03 16:47:13 +00:00
if err != nil {
return nil , err
}
err = prometheusRegisterHelper ( "head_error" , "Keeps track of the number of errors we had processing head messages." , & metrics . HeadError )
if err != nil {
return nil , err
}
err = prometheusRegisterHelper ( "head_reorg_error" , "Keeps track of the number of errors we had processing reorg messages." , & metrics . HeadReorgError )
if err != nil {
return nil , err
}
return metrics , nil
2022-05-25 14:19:29 +00:00
}
2022-06-03 16:47:13 +00:00
func prometheusRegisterHelper ( name string , help string , varPointer * uint64 ) error {
2022-05-25 14:19:29 +00:00
err := prometheus . Register ( prometheus . NewCounterFunc (
prometheus . CounterOpts {
Namespace : "beacon_client" ,
Subsystem : "" ,
Name : name ,
Help : help ,
ConstLabels : map [ string ] string { } ,
} ,
func ( ) float64 {
return float64 ( atomic . LoadUint64 ( varPointer ) )
} ) )
if err != nil && err . Error ( ) != "duplicate metrics collector registration attempted" {
loghelper . LogError ( err ) . WithField ( "name" , name ) . Error ( "Unable to register counter." )
2022-06-03 16:47:13 +00:00
return err
2022-05-25 14:19:29 +00:00
}
2022-06-03 16:47:13 +00:00
return nil
2022-05-25 14:19:29 +00:00
}
2022-05-19 13:46:38 +00:00
// A structure utilized for keeping track of various metrics. Currently, mostly used in testing.
type BeaconClientMetrics struct {
2022-06-09 21:32:46 +00:00
SlotInserts uint64 // Number of head events we successfully wrote to the DB.
ReorgInserts uint64 // Number of reorg events we successfully wrote to the DB.
KnownGapsInserts uint64 // Number of known_gaps we successfully wrote to the DB.
KnownGapsProcessed uint64 // Number of knownGaps processed.
KnownGapsReprocessError uint64 // Number of knownGaps that were updated with an error.
HeadError uint64 // Number of errors that occurred when decoding the head message.
HeadReorgError uint64 // Number of errors that occurred when decoding the reorg message.
2022-05-19 13:46:38 +00:00
}
2022-05-12 13:52:13 +00:00
// Wrapper function to increment inserts. If we want to use mutexes later we can easily update all
// occurrences here.
2022-05-24 20:18:55 +00:00
func ( m * BeaconClientMetrics ) IncrementSlotInserts ( inc uint64 ) {
2022-06-09 21:32:46 +00:00
log . Debug ( "Incrementing Slot Insert" )
2022-05-24 20:18:55 +00:00
atomic . AddUint64 ( & m . SlotInserts , inc )
2022-05-12 13:52:13 +00:00
}
// Wrapper function to increment reorgs. If we want to use mutexes later we can easily update all
// occurrences here.
2022-05-24 20:18:55 +00:00
func ( m * BeaconClientMetrics ) IncrementReorgsInsert ( inc uint64 ) {
atomic . AddUint64 ( & m . ReorgInserts , inc )
2022-05-12 13:52:13 +00:00
}
2022-05-17 20:05:15 +00:00
// Wrapper function to increment known gaps. If we want to use mutexes later we can easily update all
// occurrences here.
2022-05-24 20:18:55 +00:00
func ( m * BeaconClientMetrics ) IncrementKnownGapsInserts ( inc uint64 ) {
atomic . AddUint64 ( & m . KnownGapsInserts , inc )
}
// Wrapper function to increment known gaps processed. If we want to use mutexes later we can easily update all
// occurrences here.
func ( m * BeaconClientMetrics ) IncrementKnownGapsProcessed ( inc uint64 ) {
2022-06-09 21:32:46 +00:00
atomic . AddUint64 ( & m . KnownGapsProcessed , inc )
2022-05-17 20:05:15 +00:00
}
// Wrapper function to increment head errors. If we want to use mutexes later we can easily update all
// occurrences here.
func ( m * BeaconClientMetrics ) IncrementHeadError ( inc uint64 ) {
atomic . AddUint64 ( & m . HeadError , inc )
}
// Wrapper function to increment reorg errors. If we want to use mutexes later we can easily update all
// occurrences here.
2022-05-24 20:18:55 +00:00
func ( m * BeaconClientMetrics ) IncrementReorgError ( inc uint64 ) {
2022-05-17 20:05:15 +00:00
atomic . AddUint64 ( & m . HeadReorgError , inc )
}
2022-06-09 21:32:46 +00:00
// Wrapper function to increment the number of knownGaps that were updated with reprocessing errors.
//If we want to use mutexes later we can easily update all occurrences here.
func ( m * BeaconClientMetrics ) IncrementKnownGapsReprocessError ( inc uint64 ) {
log . Debug ( "Incrementing Known Gap Reprocessing: " , & m . KnownGapsReprocessError )
atomic . AddUint64 ( & m . KnownGapsReprocessError , inc )
}