-
Notifications
You must be signed in to change notification settings - Fork 478
/
lazycollector.go
41 lines (31 loc) · 1.01 KB
/
lazycollector.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
package lazycollector
import (
"sync"
"github.com/prometheus/client_golang/prometheus"
)
// Collector is a lazy Prometheus collector. The zero value is ready for use.
type Collector struct {
mut sync.RWMutex
inner prometheus.Collector
}
var _ prometheus.Collector = (*Collector)(nil)
// New returns a new lazy Prometheus collector.
func New() *Collector { return &Collector{} }
// Describe implements prometheus.Collector. Because Collector is a
// lazycollector, it is unchecked, so Describe is a no-op.
func (c *Collector) Describe(chan<- *prometheus.Desc) {}
// Collect implements prometheus.Collector. If the inner collector is set, its
// Collect method is called. Otherwise, Collect is a no-op.
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
c.mut.RLock()
defer c.mut.RUnlock()
if c.inner != nil {
c.inner.Collect(ch)
}
}
// Set updates the inner collector used by the lazy collector.
func (c *Collector) Set(inner prometheus.Collector) {
c.mut.Lock()
defer c.mut.Unlock()
c.inner = inner
}