github.com/MetalBlockchain/metalgo@v1.11.9/api/metrics/multi_gatherer.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package metrics 5 6 import ( 7 "fmt" 8 "sync" 9 10 "github.com/prometheus/client_golang/prometheus" 11 12 dto "github.com/prometheus/client_model/go" 13 ) 14 15 // MultiGatherer extends the Gatherer interface by allowing additional gatherers 16 // to be registered. 17 type MultiGatherer interface { 18 prometheus.Gatherer 19 20 // Register adds the outputs of [gatherer] to the results of future calls to 21 // Gather with the provided [name] added to the metrics. 22 Register(name string, gatherer prometheus.Gatherer) error 23 } 24 25 // Deprecated: Use NewPrefixGatherer instead. 26 // 27 // TODO: Remove once coreth is updated. 28 func NewMultiGatherer() MultiGatherer { 29 return NewPrefixGatherer() 30 } 31 32 type multiGatherer struct { 33 lock sync.RWMutex 34 names []string 35 gatherers prometheus.Gatherers 36 } 37 38 func (g *multiGatherer) Gather() ([]*dto.MetricFamily, error) { 39 g.lock.RLock() 40 defer g.lock.RUnlock() 41 42 return g.gatherers.Gather() 43 } 44 45 func MakeAndRegister(gatherer MultiGatherer, name string) (*prometheus.Registry, error) { 46 reg := prometheus.NewRegistry() 47 if err := gatherer.Register(name, reg); err != nil { 48 return nil, fmt.Errorf("couldn't register %q metrics: %w", name, err) 49 } 50 return reg, nil 51 }