Skip to content

Commit

Permalink
metrics: fix Label type
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Dec 20, 2024
1 parent cbf1d85 commit a7367a9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 48 deletions.
3 changes: 0 additions & 3 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
params.FixedTurnLength = ctx.Uint64(utils.OverrideFixedTurnLength.Name)
}

// Start metrics export if enabled
utils.SetupMetrics(&cfg.Metrics)

backend, eth := utils.RegisterEthService(stack, &cfg.Eth)

// Create gauge with geth system and build information
Expand Down
8 changes: 4 additions & 4 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2332,7 +2332,7 @@ type SetupMetricsOption func()
func EnableBuildInfo(gitCommit, gitDate string) SetupMetricsOption {
return func() {
// register build info into metrics
metrics.NewRegisteredLabel("build-info", nil).Mark(map[string]interface{}{
metrics.GetOrRegisterLabel("build-info", nil).Mark(map[string]interface{}{
"version": version.WithMeta,
"git-commit": gitCommit,
"git-commit-date": gitDate,
Expand All @@ -2349,7 +2349,7 @@ func EnableMinerInfo(ctx *cli.Context, minerConfig *minerconfig.Config) SetupMet
// register miner info into metrics
minerInfo := structs.Map(minerConfig)
minerInfo[UnlockedAccountFlag.Name] = ctx.String(UnlockedAccountFlag.Name)
metrics.NewRegisteredLabel("miner-info", nil).Mark(minerInfo)
metrics.GetOrRegisterLabel("miner-info", nil).Mark(minerInfo)
}
}
}
Expand All @@ -2369,7 +2369,7 @@ func RegisterFilterAPI(stack *node.Node, backend ethapi.Backend, ethcfg *ethconf
func EnableNodeInfo(poolConfig *legacypool.Config, nodeInfo *p2p.NodeInfo) SetupMetricsOption {
return func() {
// register node info into metrics
metrics.NewRegisteredLabel("node-info", nil).Mark(map[string]interface{}{
metrics.GetOrRegisterLabel("node-info", nil).Mark(map[string]interface{}{
"Enode": nodeInfo.Enode,
"ENR": nodeInfo.ENR,
"ID": nodeInfo.ID,
Expand All @@ -2389,7 +2389,7 @@ func EnableNodeTrack(ctx *cli.Context, cfg *ethconfig.Config, stack *node.Node)
nodeInfo := stack.Server().NodeInfo()
return func() {
// register node info into metrics
metrics.NewRegisteredLabel("node-stats", nil).Mark(map[string]interface{}{
metrics.GetOrRegisterLabel("node-stats", nil).Mark(map[string]interface{}{
"NodeType": parseNodeType(),
"ENR": nodeInfo.ENR,
"Mining": ctx.Bool(MiningEnabledFlag.Name),
Expand Down
11 changes: 1 addition & 10 deletions core/vote/vote_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"math/big"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -71,6 +70,7 @@ func NewVoteManager(eth Backend, chain *core.BlockChain, pool *VotePool, journal
}
log.Info("Create voteSigner successfully")
voteManager.signer = voteSigner
metrics.GetOrRegisterLabel("miner-info", nil).Mark(map[string]interface{}{"VoteKey": common.Bytes2Hex(voteManager.signer.PubKey[:])})

// Create voteJournal
voteJournal, err := NewVoteJournal(journalPath)
Expand Down Expand Up @@ -107,7 +107,6 @@ func (voteManager *VoteManager) loop() {

startVote := true
blockCountSinceMining := 0
var once sync.Once
for {
select {
case ev := <-dlEventCh:
Expand Down Expand Up @@ -166,14 +165,6 @@ func (voteManager *VoteManager) loop() {
continue
}

// Add VoteKey to `miner-info`
once.Do(func() {
minerInfo := metrics.Get("miner-info")
if minerInfo != nil {
minerInfo.(metrics.Label).Value()["VoteKey"] = common.Bytes2Hex(voteManager.signer.PubKey[:])
}
})

// Vote for curBlockHeader block.
vote := &types.VoteData{
TargetNumber: curHead.Number.Uint64(),
Expand Down
6 changes: 0 additions & 6 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import (
"github.com/ethereum/go-ethereum/internal/shutdowncheck"
"github.com/ethereum/go-ethereum/internal/version"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
Expand Down Expand Up @@ -510,11 +509,6 @@ func (s *Ethereum) StartMining() error {
return fmt.Errorf("signer missing: %v", err)
}
parlia.Authorize(eb, wallet.SignData, wallet.SignTx)

minerInfo := metrics.Get("miner-info")
if minerInfo != nil {
minerInfo.(metrics.Label).Value()["Etherbase"] = eb.String()
}
}
// If mining is started, we can disable the transaction rejection mechanism
// introduced to speed sync times.
Expand Down
4 changes: 2 additions & 2 deletions metrics/exp/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (exp *exp) publishResettingTimer(name string, metric *metrics.ResettingTime
exp.getFloat(name + ".99-percentile").Set(ps[3])
}

func (exp *exp) publishLabel(name string, metric metrics.Label) {
func (exp *exp) publishLabel(name string, metric *metrics.Label) {
labels := metric.Value()
for k, v := range labels {
exp.getMap(name).Set(k, exp.interfaceToExpVal(v))
Expand Down Expand Up @@ -274,7 +274,7 @@ func (exp *exp) syncToExpvar() {
exp.publishTimer(name, i)
case *metrics.ResettingTimer:
exp.publishResettingTimer(name, i)
case metrics.Label:
case *metrics.Label:
exp.publishLabel(name, i)
default:
panic(fmt.Sprintf("unsupported type for '%s': %T", name, i))
Expand Down
37 changes: 16 additions & 21 deletions metrics/label.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
package metrics

// Label hold an map[string]interface{} value that can be set arbitrarily.
type Label interface {
Value() map[string]interface{}
Mark(map[string]interface{})
// Label is the standard implementation of a Label.
type Label struct {
value map[string]interface{}
}

// NewRegisteredLabel constructs and registers a new StandardLabel.
func NewRegisteredLabel(name string, r Registry) Label {
c := NewStandardLabel()
if nil == r {
// GetOrRegisterLabel returns an existing Label or constructs and registers a
// new Label.
func GetOrRegisterLabel(name string, r Registry) *Label {
if r == nil {
r = DefaultRegistry
}
r.Register(name, c)
return c
return r.GetOrRegister(name, NewLabel).(*Label)
}

// NewStandardLabel constructs a new StandardLabel.
func NewStandardLabel() *StandardLabel {
return &StandardLabel{}
}

// StandardLabel is the standard implementation of a Label.
type StandardLabel struct {
value map[string]interface{}
// NewLabel constructs a new Label.
func NewLabel() *Label {
return &Label{value: make(map[string]interface{})}
}

// Value returns label values.
func (l *StandardLabel) Value() map[string]interface{} {
func (l *Label) Value() map[string]interface{} {
return l.value
}

// Mark records the label.
func (l *StandardLabel) Mark(value map[string]interface{}) {
l.value = value
func (l *Label) Mark(value map[string]interface{}) {
for k, v := range value {
l.value[k] = v
}
}
4 changes: 2 additions & 2 deletions metrics/prometheus/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *collector) Add(name string, i any) error {
c.addTimer(name, m.Snapshot())
case *metrics.ResettingTimer:
c.addResettingTimer(name, m.Snapshot())
case metrics.Label:
case *metrics.Label:
c.addLabel(name, m)
default:
return fmt.Errorf("unknown prometheus metric type %T", i)
Expand Down Expand Up @@ -138,7 +138,7 @@ func (c *collector) addResettingTimer(name string, m *metrics.ResettingTimerSnap
c.buff.WriteRune('\n')
}

func (c *collector) addLabel(name string, m metrics.Label) {
func (c *collector) addLabel(name string, m *metrics.Label) {
labels := make([]string, 0, len(m.Value()))
for k, v := range m.Value() {
labels = append(labels, fmt.Sprintf(`%s="%s"`, mutateKey(k), fmt.Sprint(v)))
Expand Down

0 comments on commit a7367a9

Please sign in to comment.