grafana/pkg/metrics/registry.go

38 lines
772 B
Go
Raw Normal View History

2015-03-22 14:14:00 -05:00
package metrics
import "sync"
2015-03-22 14:14:00 -05:00
type Registry interface {
GetSnapshots() []Metric
Register(metric Metric)
2015-03-22 14:14:00 -05:00
}
// The standard implementation of a Registry is a mutex-protected map
// of names to metrics.
type StandardRegistry struct {
metrics []Metric
2015-03-22 14:14:00 -05:00
mutex sync.Mutex
}
// Create a new registry.
func NewRegistry() Registry {
return &StandardRegistry{
metrics: make([]Metric, 0),
2015-03-22 14:14:00 -05:00
}
}
func (r *StandardRegistry) Register(metric Metric) {
2015-03-22 14:14:00 -05:00
r.mutex.Lock()
defer r.mutex.Unlock()
r.metrics = append(r.metrics, metric)
2015-03-22 14:14:00 -05:00
}
// Call the given function for each registered metric.
func (r *StandardRegistry) GetSnapshots() []Metric {
metrics := make([]Metric, len(r.metrics))
for i, metric := range r.metrics {
metrics[i] = metric.Snapshot()
2015-03-22 14:14:00 -05:00
}
return metrics
}