Skip to content

Commit

Permalink
Merge pull request #4 from samcm/metrics-jobs
Browse files Browse the repository at this point in the history
chore(jobs): Break metrics up in to jobs
  • Loading branch information
samcm committed May 9, 2022
2 parents 6cd4b69 + f6109de commit 8600cb0
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 203 deletions.
6 changes: 1 addition & 5 deletions pkg/exporter/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ func (c *node) SyncStatus(ctx context.Context) (*SyncStatus, error) {
EstimatedHeadSlot: uint64(status.HeadSlot + status.SyncDistance),
}

c.metrics.ObserveSyncPercentage(syncStatus.Percent())
c.metrics.ObserveSyncEstimatedHighestSlot(syncStatus.EstimatedHeadSlot)
c.metrics.ObserveSyncHeadSlot(syncStatus.HeadSlot)
c.metrics.ObserveSyncDistance(syncStatus.SyncDistance)
c.metrics.ObserveSyncIsSyncing(syncStatus.IsSyncing)
c.metrics.ObserveSyncStatus(*syncStatus)

return syncStatus, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package consensus
package jobs

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cast"
)

type SpecMetrics struct {
type Spec struct {
SafeSlotsToUpdateJustified prometheus.Gauge
DepositChainID prometheus.Gauge
ConfigName prometheus.GaugeVec
Expand All @@ -31,9 +31,9 @@ type SpecMetrics struct {
PresetBase prometheus.GaugeVec
}

func NewSpecMetrics(namespace string, constLabels map[string]string) SpecMetrics {
func NewSpec(namespace string, constLabels map[string]string) Spec {
namespace = namespace + "_spec"
return SpecMetrics{
return Spec{
SafeSlotsToUpdateJustified: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Expand Down Expand Up @@ -223,97 +223,97 @@ func NewSpecMetrics(namespace string, constLabels map[string]string) SpecMetrics
}
}

func (c *SpecMetrics) Update(spec map[string]interface{}) {
func (s *Spec) Update(spec map[string]interface{}) {
if safeSlotsToUpdateJustified, exists := spec["SAFE_SLOTS_TO_UPDATE_JUSTIFIED"]; exists {
c.SafeSlotsToUpdateJustified.Set(cast.ToFloat64(safeSlotsToUpdateJustified))
s.SafeSlotsToUpdateJustified.Set(cast.ToFloat64(safeSlotsToUpdateJustified))
}

if depositChainId, exists := spec["DEPOSIT_CHAIN_ID"]; exists {
c.DepositChainID.Set(cast.ToFloat64(depositChainId))
s.DepositChainID.Set(cast.ToFloat64(depositChainId))
}

if configName, exists := spec["CONFIG_NAME"]; exists {
c.ConfigName.WithLabelValues(cast.ToString(configName)).Set(1)
s.ConfigName.WithLabelValues(cast.ToString(configName)).Set(1)
}

if maxValidatorsPerCommittee, exists := spec["MAX_VALIDATORS_PER_COMMITTEE"]; exists {
c.MaxValidatorsPerCommittee.Set(cast.ToFloat64(maxValidatorsPerCommittee))
s.MaxValidatorsPerCommittee.Set(cast.ToFloat64(maxValidatorsPerCommittee))
}

if secondsPerEth1Block, exists := spec["SECONDS_PER_ETH1_BLOCK"]; exists {
c.SecondsPerEth1Block.Set(float64(cast.ToDuration(secondsPerEth1Block)))
s.SecondsPerEth1Block.Set(float64(cast.ToDuration(secondsPerEth1Block)))
}

if baseRewardFactor, exists := spec["BASE_REWARD_FACTOR"]; exists {
c.BaseRewardFactor.Set(cast.ToFloat64(baseRewardFactor))
s.BaseRewardFactor.Set(cast.ToFloat64(baseRewardFactor))
}

if epochsPerSyncComitteePeriod, exists := spec["EPOCHS_PER_SYNC_COMMITTEE_PERIOD"]; exists {
c.EpochsPerSyncCommitteePeriod.Set(cast.ToFloat64(epochsPerSyncComitteePeriod))
s.EpochsPerSyncCommitteePeriod.Set(cast.ToFloat64(epochsPerSyncComitteePeriod))
}

if effectiveBalanceIncrement, exists := spec["EFFECTIVE_BALANCE_INCREMENT"]; exists {
c.EffectiveBalanceIncrement.Set(cast.ToFloat64(effectiveBalanceIncrement))
s.EffectiveBalanceIncrement.Set(cast.ToFloat64(effectiveBalanceIncrement))
}

if maxAttestations, exists := spec["MAX_ATTESTATIONS"]; exists {
c.MaxAttestations.Set(cast.ToFloat64(maxAttestations))
s.MaxAttestations.Set(cast.ToFloat64(maxAttestations))
}

if minSyncCommitteeParticipants, exists := spec["MIN_SYNC_COMMITTEE_PARTICIPANTS"]; exists {
c.MinSyncCommitteeParticipants.Set(cast.ToFloat64(minSyncCommitteeParticipants))
s.MinSyncCommitteeParticipants.Set(cast.ToFloat64(minSyncCommitteeParticipants))
}

if genesisDelay, exists := spec["GENESIS_DELAY"]; exists {
c.GenesisDelay.Set(float64(cast.ToDuration(genesisDelay)))
s.GenesisDelay.Set(float64(cast.ToDuration(genesisDelay)))
}

if secondsPerSlot, exists := spec["SECONDS_PER_SLOT"]; exists {
c.SecondsPerSlot.Set(float64(cast.ToDuration(secondsPerSlot)))
s.SecondsPerSlot.Set(float64(cast.ToDuration(secondsPerSlot)))
}

if maxEffectiveBalance, exists := spec["MAX_EFFECTIVE_BALANCE"]; exists {
c.MaxEffectiveBalance.Set(cast.ToFloat64(maxEffectiveBalance))
s.MaxEffectiveBalance.Set(cast.ToFloat64(maxEffectiveBalance))
}

if terminalTotalDifficulty, exists := spec["TERMINAL_TOTAL_DIFFICULTY"]; exists {
c.TerminalTotalDifficulty.Set(cast.ToFloat64(terminalTotalDifficulty))
s.TerminalTotalDifficulty.Set(cast.ToFloat64(terminalTotalDifficulty))
}

if maxDeposits, exists := spec["MAX_DEPOSITS"]; exists {
c.MaxDeposits.Set(cast.ToFloat64((maxDeposits)))
s.MaxDeposits.Set(cast.ToFloat64((maxDeposits)))
}

if minGenesisActiveValidatorCount, exists := spec["MIN_GENESIS_ACTIVE_VALIDATOR_COUNT"]; exists {
c.MinGenesisActiveValidatorCount.Set(cast.ToFloat64(minGenesisActiveValidatorCount))
s.MinGenesisActiveValidatorCount.Set(cast.ToFloat64(minGenesisActiveValidatorCount))
}

if targetCommitteeSize, exists := spec["TARGET_COMMITTEE_SIZE"]; exists {
c.TargetCommitteeSize.Set(cast.ToFloat64(targetCommitteeSize))
s.TargetCommitteeSize.Set(cast.ToFloat64(targetCommitteeSize))
}

if syncCommitteeSize, exists := spec["SYNC_COMMITTEE_SIZE"]; exists {
c.SyncCommitteeSize.Set(cast.ToFloat64(syncCommitteeSize))
s.SyncCommitteeSize.Set(cast.ToFloat64(syncCommitteeSize))
}

if eth1FollowDistance, exists := spec["ETH1_FOLLOW_DISTANCE"]; exists {
c.Eth1FollowDistance.Set(cast.ToFloat64(eth1FollowDistance))
s.Eth1FollowDistance.Set(cast.ToFloat64(eth1FollowDistance))
}

if terminalBlockHashActivationEpoch, exists := spec["TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH"]; exists {
c.TerminalBlockHashActivationEpoch.Set(cast.ToFloat64(terminalBlockHashActivationEpoch))
s.TerminalBlockHashActivationEpoch.Set(cast.ToFloat64(terminalBlockHashActivationEpoch))
}

if minDepositAmount, exists := spec["MIN_DEPOSIT_AMOUNT"]; exists {
c.MinDepositAmount.Set(cast.ToFloat64(minDepositAmount))
s.MinDepositAmount.Set(cast.ToFloat64(minDepositAmount))
}

if slotsPerEpoch, exists := spec["SLOTS_PER_EPOCH"]; exists {
c.SlotsPerEpoch.Set(cast.ToFloat64(slotsPerEpoch))
s.SlotsPerEpoch.Set(cast.ToFloat64(slotsPerEpoch))
}

if presetBase, exists := spec["PRESET_BASE"]; exists {
c.PresetBase.WithLabelValues(cast.ToString(presetBase)).Set(1)
s.PresetBase.WithLabelValues(cast.ToString(presetBase)).Set(1)
}

}
84 changes: 84 additions & 0 deletions pkg/exporter/consensus/jobs/syncstatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package jobs

import (
"github.com/prometheus/client_golang/prometheus"
)

type SyncStatus struct {
Percentage prometheus.Gauge
EstimatedHighestSlot prometheus.Gauge
HeadSlot prometheus.Gauge
Distance prometheus.Gauge
IsSyncing prometheus.Gauge
}

func NewSyncStatus(namespace string, constLabels map[string]string) SyncStatus {
namespace = namespace + "_sync"
return SyncStatus{
Percentage: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "percentage",
Help: "How synced the node is with the network (0-100%).",
ConstLabels: constLabels,
},
),
EstimatedHighestSlot: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "estimated_highest_slot",
Help: "The estimated highest slot of the network.",
ConstLabels: constLabels,
},
),
HeadSlot: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "head_slot",
Help: "The current slot of the node.",
ConstLabels: constLabels,
},
),
Distance: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "distance",
Help: "The sync distance of the node.",
ConstLabels: constLabels,
},
),
IsSyncing: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "is_syncing",
Help: "1 if the node is in syncing state.",
ConstLabels: constLabels,
},
),
}
}

func (s *SyncStatus) ObserveSyncPercentage(percent float64) {
s.Percentage.Set(percent)
}

func (s *SyncStatus) ObserveSyncEstimatedHighestSlot(slot uint64) {
s.EstimatedHighestSlot.Set(float64(slot))
}

func (s *SyncStatus) ObserveSyncHeadSlot(slot uint64) {
s.HeadSlot.Set(float64(slot))
}

func (s *SyncStatus) ObserveSyncDistance(slots uint64) {
s.Distance.Set(float64(slots))
}

func (s *SyncStatus) ObserveSyncIsSyncing(syncing bool) {
if syncing {
s.IsSyncing.Set(1)
return
}

s.IsSyncing.Set(0)
}
108 changes: 24 additions & 84 deletions pkg/exporter/consensus/metrics.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package consensus

import "github.com/prometheus/client_golang/prometheus"
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/consensus/jobs"
)

type Metrics interface {
ObserveSyncPercentage(percent float64)
ObserveSyncEstimatedHighestSlot(slot uint64)
ObserveSyncHeadSlot(slot uint64)
ObserveSyncDistance(slots uint64)
ObserveSyncIsSyncing(syncing bool)
ObserveSyncStatus(status SyncStatus)
ObserveNodeVersion(version string)
ObserveSpec(spec map[string]interface{})
}

type metrics struct {
syncPercentage prometheus.Gauge
syncEstimatedHighestSlot prometheus.Gauge
syncHeadSlot prometheus.Gauge
syncDistance prometheus.Gauge
syncIsSyncing prometheus.Gauge
nodeVersion *prometheus.GaugeVec
nodeVersion *prometheus.GaugeVec

specMetrics SpecMetrics
syncMetrics jobs.SyncStatus
specMetrics jobs.Spec
}

func NewMetrics(nodeName, namespace string) Metrics {
Expand All @@ -29,46 +24,7 @@ func NewMetrics(nodeName, namespace string) Metrics {
constLabels["node_name"] = nodeName

m := &metrics{
syncPercentage: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "sync_percentage",
Help: "How synced the node is with the network (0-100%).",
ConstLabels: constLabels,
},
),
syncEstimatedHighestSlot: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "sync_estimated_highest_slot",
Help: "The estimated highest slot of the network.",
ConstLabels: constLabels,
},
),
syncHeadSlot: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "sync_head_slot",
Help: "The current slot of the node.",
ConstLabels: constLabels,
},
),
syncDistance: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "sync_distance",
Help: "The sync distance of the node.",
ConstLabels: constLabels,
},
),
syncIsSyncing: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "sync_is_syncing",
Help: "1 if the node is in syncing state.",
ConstLabels: constLabels,
},
),

nodeVersion: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -80,14 +36,15 @@ func NewMetrics(nodeName, namespace string) Metrics {
"version",
},
),
specMetrics: NewSpecMetrics(namespace, constLabels),
specMetrics: jobs.NewSpec(namespace, constLabels),
syncMetrics: jobs.NewSyncStatus(namespace, constLabels),
}

prometheus.MustRegister(m.syncPercentage)
prometheus.MustRegister(m.syncEstimatedHighestSlot)
prometheus.MustRegister(m.syncHeadSlot)
prometheus.MustRegister(m.syncDistance)
prometheus.MustRegister(m.syncIsSyncing)
prometheus.MustRegister(m.syncMetrics.Percentage)
prometheus.MustRegister(m.syncMetrics.EstimatedHighestSlot)
prometheus.MustRegister(m.syncMetrics.HeadSlot)
prometheus.MustRegister(m.syncMetrics.Distance)
prometheus.MustRegister(m.syncMetrics.IsSyncing)
prometheus.MustRegister(m.nodeVersion)

prometheus.MustRegister(m.specMetrics.SafeSlotsToUpdateJustified)
Expand Down Expand Up @@ -117,35 +74,18 @@ func NewMetrics(nodeName, namespace string) Metrics {
return m
}

func (m *metrics) ObserveSyncPercentage(percent float64) {
m.syncPercentage.Set(percent)
}

func (m *metrics) ObserveSyncEstimatedHighestSlot(slot uint64) {
m.syncEstimatedHighestSlot.Set(float64(slot))
}

func (m *metrics) ObserveSyncHeadSlot(slot uint64) {
m.syncHeadSlot.Set(float64(slot))
}

func (m *metrics) ObserveSyncDistance(slots uint64) {
m.syncDistance.Set(float64(slots))
}

func (m *metrics) ObserveSyncIsSyncing(syncing bool) {
if syncing {
m.syncIsSyncing.Set(1)
return
}

m.syncIsSyncing.Set(0)
}

func (m *metrics) ObserveNodeVersion(version string) {
m.nodeVersion.WithLabelValues(version).Set(float64(1))
}

func (m *metrics) ObserveSpec(spec map[string]interface{}) {
m.specMetrics.Update(spec)
}

func (m *metrics) ObserveSyncStatus(status SyncStatus) {
m.syncMetrics.ObserveSyncDistance(status.SyncDistance)
m.syncMetrics.ObserveSyncEstimatedHighestSlot(status.EstimatedHeadSlot)
m.syncMetrics.ObserveSyncHeadSlot(status.HeadSlot)
m.syncMetrics.ObserveSyncIsSyncing(status.IsSyncing)
m.syncMetrics.ObserveSyncPercentage(status.Percent())
}
File renamed without changes.
Loading

0 comments on commit 8600cb0

Please sign in to comment.