mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
The module already called itself github.com/luxfi/node while containing no node in Go: cmd/luxd was twenty-one lines that printed the size of a few types, and the Go column of the matrix measured a VM plugin. A repo that fetches the thing it claims to be has not replaced it, so the Go sources move in — 1515 files, the daemon and everything under it — and the build reaches outside this module for nothing. Rust and C++ sit beside it unchanged and are still measured against it by the differential. That is the point of one repo: the reference implementation and the two ports are the same checkout, so parity is a thing you can run rather than a thing you assert. Verified: builds, and the closure carries no lux-private — checked at build time rather than believed. The daemon starts twelve chains on a local network (P X C Q Z A B G K M F, plus D through its plugin) and reports healthy.
105 lines
2.7 KiB
Go
105 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"
|
|
"github.com/luxfi/util"
|
|
)
|
|
|
|
// 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 = utils.DeleteIndex(g.names, index)
|
|
g.gatherers = utils.DeleteIndex(g.gatherers, index)
|
|
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
|
|
}
|