registry.go 772 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package metrics
  2. import "sync"
  3. type Registry interface {
  4. GetSnapshots() []Metric
  5. Register(metric Metric)
  6. }
  7. // The standard implementation of a Registry is a mutex-protected map
  8. // of names to metrics.
  9. type StandardRegistry struct {
  10. metrics []Metric
  11. mutex sync.Mutex
  12. }
  13. // Create a new registry.
  14. func NewRegistry() Registry {
  15. return &StandardRegistry{
  16. metrics: make([]Metric, 0),
  17. }
  18. }
  19. func (r *StandardRegistry) Register(metric Metric) {
  20. r.mutex.Lock()
  21. defer r.mutex.Unlock()
  22. r.metrics = append(r.metrics, metric)
  23. }
  24. // Call the given function for each registered metric.
  25. func (r *StandardRegistry) GetSnapshots() []Metric {
  26. metrics := make([]Metric, len(r.metrics))
  27. for i, metric := range r.metrics {
  28. metrics[i] = metric.Snapshot()
  29. }
  30. return metrics
  31. }