Path: blob/main/component/otelcol/internal/lazycollector/lazycollector.go
4096 views
package lazycollector12import (3"sync"45"github.com/prometheus/client_golang/prometheus"6)78// Collector is a lazy Prometheus collector. The zero value is ready for use.9type Collector struct {10mut sync.RWMutex11inner prometheus.Collector12}1314var _ prometheus.Collector = (*Collector)(nil)1516// New returns a new lazy Prometheus collector.17func New() *Collector { return &Collector{} }1819// Describe implements prometheus.Collector. Because Collector is a20// lazycollector, it is unchecked, so Describe is a no-op.21func (c *Collector) Describe(chan<- *prometheus.Desc) {}2223// Collect implements prometheus.Collector. If the inner collector is set, its24// Collect method is called. Otherwise, Collect is a no-op.25func (c *Collector) Collect(ch chan<- prometheus.Metric) {26c.mut.RLock()27defer c.mut.RUnlock()2829if c.inner != nil {30c.inner.Collect(ch)31}32}3334// Set updates the inner collector used by the lazy collector.35func (c *Collector) Set(inner prometheus.Collector) {36c.mut.Lock()37defer c.mut.Unlock()3839c.inner = inner40}414243