forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
multi_gatherer.go
51 lines (40 loc) · 1.25 KB
/
multi_gatherer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metrics
import (
"fmt"
"sync"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
// MultiGatherer extends the Gatherer interface by allowing additional gatherers
// to be registered.
type MultiGatherer interface {
prometheus.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 prometheus.Gatherer) error
}
// Deprecated: Use NewPrefixGatherer instead.
//
// TODO: Remove once coreth is updated.
func NewMultiGatherer() MultiGatherer {
return NewPrefixGatherer()
}
type multiGatherer struct {
lock sync.RWMutex
names []string
gatherers prometheus.Gatherers
}
func (g *multiGatherer) Gather() ([]*dto.MetricFamily, error) {
g.lock.RLock()
defer g.lock.RUnlock()
return g.gatherers.Gather()
}
func MakeAndRegister(gatherer MultiGatherer, name string) (*prometheus.Registry, error) {
reg := prometheus.NewRegistry()
if err := gatherer.Register(name, reg); err != nil {
return nil, fmt.Errorf("couldn't register %q metrics: %w", name, err)
}
return reg, nil
}