Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/util/unchecked_collector.go
4093 views
1
package util
2
3
import (
4
"sync"
5
6
"github.com/prometheus/client_golang/prometheus"
7
)
8
9
// UncheckedCollector is a prometheus.Collector which stores a set of unchecked
10
// metrics.
11
type UncheckedCollector struct {
12
mut sync.RWMutex
13
inner prometheus.Collector
14
}
15
16
var _ prometheus.Collector = (*UncheckedCollector)(nil)
17
18
// NewUncheckedCollector creates a new UncheckedCollector. If inner is nil,
19
// UncheckedCollector returns no metrics.
20
func NewUncheckedCollector(inner prometheus.Collector) *UncheckedCollector {
21
return &UncheckedCollector{inner: inner}
22
}
23
24
// SetCollector replaces the inner collector.
25
func (uc *UncheckedCollector) SetCollector(inner prometheus.Collector) {
26
uc.mut.Lock()
27
defer uc.mut.Unlock()
28
29
uc.inner = inner
30
}
31
32
// Describe implements [prometheus.Collector], but is a no-op to be considered
33
// an "unchecked" collector by Prometheus. See [prometheus.Collector] for more
34
// information.
35
func (uc *UncheckedCollector) Describe(_ chan<- *prometheus.Desc) {
36
// No-op: do not send any descriptions of metrics to avoid having them be
37
// checked.
38
}
39
40
// Collector implements [prometheus.Collector]. If the UncheckedCollector has a
41
// non-nil inner collector, metrics will be collected from it.
42
func (uc *UncheckedCollector) Collect(ch chan<- prometheus.Metric) {
43
uc.mut.RLock()
44
defer uc.mut.RUnlock()
45
46
if uc.inner != nil {
47
uc.inner.Collect(ch)
48
}
49
}
50
51