Skip to content

Commit 9577892

Browse files
committed
Register VM and snowman metrics after chain creation
Metrics of VM and snowman should be registered only after the objects they are measuring/monitoring have been constructed in the first place. Signed-off-by: Yacov Manevich <yacov.manevich@avalabs.org>
1 parent dcb2f70 commit 9577892

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

chains/manager.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
"sync"
1414
"time"
1515

16+
"github.com/prometheus/client_golang/prometheus"
17+
18+
"github.com/prometheus/client_golang/prometheus"
1619
"go.uber.org/zap"
1720

1821
"github.com/ava-labs/avalanchego/api/health"
@@ -490,15 +493,15 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c
490493
return nil, fmt.Errorf("error while creating chain's log %w", err)
491494
}
492495

493-
snowmanMetrics, err := metrics.MakeAndRegister(
496+
snowmanMetrics, registerSnowmanMetrics := makeAndRegister(
494497
m.snowmanGatherer,
495498
primaryAlias,
496499
)
497500
if err != nil {
498501
return nil, err
499502
}
500503

501-
vmMetrics, err := m.getOrMakeVMRegisterer(chainParams.VMID, primaryAlias)
504+
vmMetrics, registerChainMetrics, err := m.getOrMakeVMRegisterer(chainParams.VMID, primaryAlias)
502505
if err != nil {
503506
return nil, err
504507
}
@@ -601,7 +604,7 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c
601604
return nil, err
602605
}
603606

604-
return chain, nil
607+
return chain, errors.Join(registerChainMetrics(), registerSnowmanMetrics())
605608
}
606609

607610
func (m *manager) AddRegistrant(r Registrant) {
@@ -1558,7 +1561,7 @@ func (m *manager) getChainConfig(id ids.ID) (ChainConfig, error) {
15581561
return ChainConfig{}, nil
15591562
}
15601563

1561-
func (m *manager) getOrMakeVMRegisterer(vmID ids.ID, chainAlias string) (metrics.MultiGatherer, error) {
1564+
func (m *manager) getOrMakeVMRegisterer(vmID ids.ID, chainAlias string) (metrics.MultiGatherer, func() error, error) {
15621565
vmGatherer, ok := m.vmGatherer[vmID]
15631566
if !ok {
15641567
vmName := constants.VMName(vmID)
@@ -1569,15 +1572,27 @@ func (m *manager) getOrMakeVMRegisterer(vmID ids.ID, chainAlias string) (metrics
15691572
vmGatherer,
15701573
)
15711574
if err != nil {
1572-
return nil, err
1575+
return nil, nil, err
15731576
}
15741577
m.vmGatherer[vmID] = vmGatherer
15751578
}
15761579

15771580
chainReg := metrics.NewPrefixGatherer()
1578-
err := vmGatherer.Register(
1579-
chainAlias,
1580-
chainReg,
1581-
)
1582-
return chainReg, err
1581+
1582+
return chainReg, func() error {
1583+
return vmGatherer.Register(
1584+
chainAlias,
1585+
chainReg,
1586+
)
1587+
}, nil
1588+
}
1589+
1590+
func makeAndRegister(gatherer metrics.MultiGatherer, name string) (*prometheus.Registry, func() error) {
1591+
reg := prometheus.NewRegistry()
1592+
return reg, func() error {
1593+
if err := gatherer.Register(name, reg); err != nil {
1594+
return fmt.Errorf("couldn't register %q metrics: %w", name, err)
1595+
}
1596+
return nil
1597+
}
15831598
}

0 commit comments

Comments
 (0)