Skip to content

Commit

Permalink
Merge pull request #10 from samcm/feat/current-epoch
Browse files Browse the repository at this point in the history
Feat/current epoch
  • Loading branch information
samcm authored May 16, 2022
2 parents a6072a7 + cedad7d commit cd6b96c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
101 changes: 91 additions & 10 deletions pkg/exporter/consensus/jobs/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ import (

type Forks struct {
MetricExporter
Epochs prometheus.GaugeVec
client eth2client.Service
log logrus.FieldLogger
Epochs prometheus.GaugeVec
Activated prometheus.GaugeVec
Current prometheus.GaugeVec
client eth2client.Service
log logrus.FieldLogger
previousCurrentFork string
}

const (
Expand All @@ -28,8 +31,9 @@ func NewForksJob(client eth2client.Service, log logrus.FieldLogger, namespace st
constLabels["module"] = NameFork
namespace = namespace + "_fork"
return Forks{
client: client,
log: log,
client: client,
log: log,
previousCurrentFork: "",
Epochs: *prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -41,6 +45,28 @@ func NewForksJob(client eth2client.Service, log logrus.FieldLogger, namespace st
"fork",
},
),
Activated: *prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "activated",
Help: "The activation status of the fork (1 for activated).",
ConstLabels: constLabels,
},
[]string{
"fork",
},
),
Current: *prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "current",
Help: "The current fork.",
ConstLabels: constLabels,
},
[]string{
"fork",
},
),
}
}

Expand All @@ -64,29 +90,84 @@ func (f *Forks) tick(ctx context.Context) {
if err := f.ForkEpochs(ctx); err != nil {
f.log.WithError(err).Error("Failed to fetch fork epochs")
}
if err := f.GetCurrent(ctx); err != nil {
f.log.WithError(err).Error("Failed to fetch current fork")
}
}

func (f *Forks) ForkEpochs(ctx context.Context) error {
// Extract the forks out of the spec.
provider, isProvider := f.client.(eth2client.SpecProvider)
spec, err := f.getSpec(ctx)
if err != nil {
return err
}

for k, v := range spec {
if strings.Contains(k, "_FORK_EPOCH") {
f.ObserveForkEpoch(strings.Replace(k, "_FORK_EPOCH", "", -1), cast.ToUint64(v))
}
}

return nil
}

func (f *Forks) GetCurrent(ctx context.Context) error {
// Get the current head slot.
provider, isProvider := f.client.(eth2client.BeaconBlockHeadersProvider)
if !isProvider {
return errors.New("client does not implement eth2client.SpecProvider")
return errors.New("client does not implement eth2client.BeaconBlockHeadersProvider")
}

headSlot, err := provider.BeaconBlockHeader(ctx, "head")
if err != nil {
return err
}

spec, err := provider.Spec(ctx)
spec, err := f.getSpec(ctx)
if err != nil {
return err
}

slotsPerEpoch := 32
if v, ok := spec["SLOTS_PER_EPOCH"]; ok {
slotsPerEpoch = cast.ToInt(v)
}

current := ""
currentSlot := 0
for k, v := range spec {
if strings.Contains(k, "_FORK_EPOCH") {
f.ObserveForkEpoch(strings.Replace(k, "_FORK_EPOCH", "", -1), cast.ToUint64(v))
forkName := strings.Replace(k, "_FORK_EPOCH", "", -1)
if int(headSlot.Header.Message.Slot)/slotsPerEpoch > cast.ToInt(v) {
f.Activated.WithLabelValues(forkName).Set(1)
} else {
f.Activated.WithLabelValues(forkName).Set(0)
}

if currentSlot < cast.ToInt(v) {
current = forkName
currentSlot = cast.ToInt(v)
}
}
}

if current != f.previousCurrentFork {
f.Current.WithLabelValues(current).Set(1)
f.Current.WithLabelValues(f.previousCurrentFork).Set(0)
f.previousCurrentFork = current
}

return nil
}

func (f *Forks) getSpec(ctx context.Context) (map[string]interface{}, error) {
provider, isProvider := f.client.(eth2client.SpecProvider)
if !isProvider {
return nil, errors.New("client does not implement eth2client.SpecProvider")
}

return provider.Spec(ctx)
}

func (f *Forks) ObserveForkEpoch(name string, epoch uint64) {
f.Epochs.WithLabelValues(name).Set(float64(epoch))
}
2 changes: 2 additions & 0 deletions pkg/exporter/consensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func NewMetrics(client eth2client.Service, log logrus.FieldLogger, nodeName, nam
prometheus.MustRegister(m.specMetrics.PresetBase)

prometheus.MustRegister(m.forkMetrics.Epochs)
prometheus.MustRegister(m.forkMetrics.Current)
prometheus.MustRegister(m.forkMetrics.Activated)
return m
}

Expand Down

0 comments on commit cd6b96c

Please sign in to comment.