Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/otelcol/internal/lazycollector/lazycollector.go
4096 views
1
package lazycollector
2
3
import (
4
"sync"
5
6
"github.com/prometheus/client_golang/prometheus"
7
)
8
9
// Collector is a lazy Prometheus collector. The zero value is ready for use.
10
type Collector struct {
11
mut sync.RWMutex
12
inner prometheus.Collector
13
}
14
15
var _ prometheus.Collector = (*Collector)(nil)
16
17
// New returns a new lazy Prometheus collector.
18
func New() *Collector { return &Collector{} }
19
20
// Describe implements prometheus.Collector. Because Collector is a
21
// lazycollector, it is unchecked, so Describe is a no-op.
22
func (c *Collector) Describe(chan<- *prometheus.Desc) {}
23
24
// Collect implements prometheus.Collector. If the inner collector is set, its
25
// Collect method is called. Otherwise, Collect is a no-op.
26
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
27
c.mut.RLock()
28
defer c.mut.RUnlock()
29
30
if c.inner != nil {
31
c.inner.Collect(ch)
32
}
33
}
34
35
// Set updates the inner collector used by the lazy collector.
36
func (c *Collector) Set(inner prometheus.Collector) {
37
c.mut.Lock()
38
defer c.mut.Unlock()
39
40
c.inner = inner
41
}
42
43