From 993d1690b4f214ebd1d5fd28171d65322c9aa8cb Mon Sep 17 00:00:00 2001 From: yacovm Date: Thu, 24 Oct 2024 16:21:30 +0200 Subject: [PATCH] Register VM and snowman metrics after chain creation (#3489) Signed-off-by: Yacov Manevich Co-authored-by: Stephen Buttolph --- chains/manager.go | 60 ++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/chains/manager.go b/chains/manager.go index 906c6f136dfa..8d1eb4feea76 100644 --- a/chains/manager.go +++ b/chains/manager.go @@ -13,6 +13,7 @@ import ( "sync" "time" + "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" "github.com/ava-labs/avalanchego/api/health" @@ -490,19 +491,6 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c return nil, fmt.Errorf("error while creating chain's log %w", err) } - snowmanMetrics, err := metrics.MakeAndRegister( - m.snowmanGatherer, - primaryAlias, - ) - if err != nil { - return nil, err - } - - vmMetrics, err := m.getOrMakeVMRegisterer(chainParams.VMID, primaryAlias) - if err != nil { - return nil, err - } - ctx := &snow.ConsensusContext{ Context: &snow.Context{ NetworkID: m.NetworkID, @@ -520,7 +508,7 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c Keystore: m.Keystore.NewBlockchainKeyStore(chainParams.ID), SharedMemory: m.AtomicMemory.NewSharedMemory(chainParams.ID), BCLookup: m, - Metrics: vmMetrics, + Metrics: metrics.NewPrefixGatherer(), WarpSigner: warp.NewSigner(m.StakingBLSKey, m.NetworkID, chainParams.ID), @@ -528,7 +516,7 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c ChainDataDir: chainDataDir, }, PrimaryAlias: primaryAlias, - Registerer: snowmanMetrics, + Registerer: prometheus.NewRegistry(), BlockAcceptor: m.BlockAcceptorGroup, TxAcceptor: m.TxAcceptorGroup, VertexAcceptor: m.VertexAcceptorGroup, @@ -601,7 +589,15 @@ func (m *manager) buildChain(chainParams ChainParameters, sb subnets.Subnet) (*c return nil, err } - return chain, nil + vmGatherer, err := m.getOrMakeVMGatherer(chainParams.VMID) + if err != nil { + return nil, err + } + + return chain, errors.Join( + m.snowmanGatherer.Register(primaryAlias, ctx.Registerer), + vmGatherer.Register(primaryAlias, ctx.Metrics), + ) } func (m *manager) AddRegistrant(r Registrant) { @@ -1556,26 +1552,22 @@ func (m *manager) getChainConfig(id ids.ID) (ChainConfig, error) { return ChainConfig{}, nil } -func (m *manager) getOrMakeVMRegisterer(vmID ids.ID, chainAlias string) (metrics.MultiGatherer, error) { +func (m *manager) getOrMakeVMGatherer(vmID ids.ID) (metrics.MultiGatherer, error) { vmGatherer, ok := m.vmGatherer[vmID] - if !ok { - vmName := constants.VMName(vmID) - vmNamespace := metric.AppendNamespace(constants.PlatformName, vmName) - vmGatherer = metrics.NewLabelGatherer(ChainLabel) - err := m.Metrics.Register( - vmNamespace, - vmGatherer, - ) - if err != nil { - return nil, err - } - m.vmGatherer[vmID] = vmGatherer + if ok { + return vmGatherer, nil } - chainReg := metrics.NewPrefixGatherer() - err := vmGatherer.Register( - chainAlias, - chainReg, + vmName := constants.VMName(vmID) + vmNamespace := metric.AppendNamespace(constants.PlatformName, vmName) + vmGatherer = metrics.NewLabelGatherer(ChainLabel) + err := m.Metrics.Register( + vmNamespace, + vmGatherer, ) - return chainReg, err + if err != nil { + return nil, err + } + m.vmGatherer[vmID] = vmGatherer + return vmGatherer, nil }