Files
vm/api/metrics/multi_gatherer.go
zeekay 021a8edace fix: resolve proto import paths and test compilation issues
- Fix dag package imports to use vm/proto/pb/dag instead of node/proto
- Fix api/health test to use atomic.Atomic from github.com/luxfi/atomic
- Fix components/message test to use crypto.RandomBytes
- Fix chains/manager to use correct EngineType constant
2026-01-11 20:58:38 -08:00

104 lines
2.7 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metrics
import (
"fmt"
"slices"
"sort"
"sync"
"github.com/luxfi/metric"
)
// MultiGatherer extends the Gatherer interface by allowing additional gatherers
// to be registered.
type MultiGatherer interface {
metric.Gatherer
// Register adds the outputs of [gatherer] to the results of future calls to
// Gather with the provided [name] added to the metrics.
Register(name string, gatherer metric.Gatherer) error
// Deregister removes the outputs of a gatherer with [name] from the results
// of future calls to Gather. Returns true if a gatherer with [name] was
// found.
Deregister(name string) bool
}
type multiGatherer struct {
lock sync.RWMutex
names []string
gatherers []metric.Gatherer
}
// NewMultiGatherer creates and returns a new MultiGatherer that applies
// prefixes to metric names. For a MultiGatherer without prefix support, use
// the multiGatherer struct directly.
func NewMultiGatherer() MultiGatherer {
return NewPrefixGatherer()
}
func (g *multiGatherer) Gather() ([]*metric.MetricFamily, error) {
g.lock.RLock()
defer g.lock.RUnlock()
var allFamilies []*metric.MetricFamily
for _, gatherer := range g.gatherers {
families, err := gatherer.Gather()
if err != nil {
return allFamilies, err
}
allFamilies = append(allFamilies, families...)
}
// Sort metrics by name for consistent ordering
sort.Slice(allFamilies, func(i, j int) bool {
return allFamilies[i].Name < allFamilies[j].Name
})
return allFamilies, nil
}
// Register adds the outputs of gatherer to the results of future calls to
// Gather with the provided name added to the metrics.
func (g *multiGatherer) Register(name string, gatherer metric.Gatherer) error {
g.lock.Lock()
defer g.lock.Unlock()
if slices.Contains(g.names, name) {
return fmt.Errorf("gatherer with name %q already registered", name)
}
g.register(name, gatherer)
return nil
}
func (g *multiGatherer) register(name string, gatherer metric.Gatherer) {
g.names = append(g.names, name)
g.gatherers = append(g.gatherers, gatherer)
}
func (g *multiGatherer) Deregister(name string) bool {
g.lock.Lock()
defer g.lock.Unlock()
index := slices.Index(g.names, name)
if index == -1 {
return false
}
g.names = append(g.names[:index], g.names[index+1:]...)
g.gatherers = append(g.gatherers[:index], g.gatherers[index+1:]...)
return true
}
func MakeAndRegister(gatherer MultiGatherer, name string) (metric.Registry, error) {
reg := metric.NewRegistry()
if err := gatherer.Register(name, reg); err != nil {
return nil, fmt.Errorf("couldn't register %q metrics: %w", name, err)
}
return reg, nil
}