registry.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package metrics
  2. import "sync"
  3. // A Registry holds references to a set of metrics by name and can iterate
  4. // over them, calling callback functions provided by the user.
  5. //
  6. // This is an interface so as to encourage other structs to implement
  7. // the Registry API as appropriate.
  8. type Registry interface {
  9. Each(func(string, interface{}))
  10. Get(string) interface{}
  11. Register(string, interface{})
  12. RunHealthchecks()
  13. Unregister(string)
  14. }
  15. // The standard implementation of a Registry is a mutex-protected map
  16. // of names to metrics.
  17. type StandardRegistry struct {
  18. mutex *sync.Mutex
  19. metrics map[string]interface{}
  20. }
  21. // Create a new registry.
  22. func NewRegistry() *StandardRegistry {
  23. return &StandardRegistry{
  24. &sync.Mutex{},
  25. make(map[string]interface{}),
  26. }
  27. }
  28. // Call the given function for each registered metric.
  29. func (r *StandardRegistry) Each(f func(string, interface{})) {
  30. r.mutex.Lock()
  31. defer r.mutex.Unlock()
  32. for name, i := range r.metrics {
  33. f(name, i)
  34. }
  35. }
  36. // Get the metric by the given name or nil if none is registered.
  37. func (r *StandardRegistry) Get(name string) interface{} {
  38. r.mutex.Lock()
  39. defer r.mutex.Unlock()
  40. return r.metrics[name]
  41. }
  42. // Register the given metric under the given name.
  43. func (r *StandardRegistry) Register(name string, i interface{}) {
  44. switch i.(type) {
  45. case Counter, Gauge, Healthcheck, Histogram, Meter, Timer:
  46. r.mutex.Lock()
  47. defer r.mutex.Unlock()
  48. r.metrics[name] = i
  49. }
  50. }
  51. // Run all registered healthchecks.
  52. func (r *StandardRegistry) RunHealthchecks() {
  53. r.mutex.Lock()
  54. defer r.mutex.Unlock()
  55. for _, i := range r.metrics {
  56. if h, ok := i.(Healthcheck); ok {
  57. h.Check()
  58. }
  59. }
  60. }
  61. // Unregister the metric with the given name.
  62. func (r *StandardRegistry) Unregister(name string) {
  63. r.mutex.Lock()
  64. defer r.mutex.Unlock()
  65. delete(r.metrics, name)
  66. }
  67. var DefaultRegistry *StandardRegistry
  68. // Call the given function for each registered metric.
  69. func Each(f func(string, interface{})) {
  70. DefaultRegistry.Each(f)
  71. }
  72. // Get the metric by the given name or nil if none is registered.
  73. func Get(name string) interface{} {
  74. return DefaultRegistry.Get(name)
  75. }
  76. // Register the given metric under the given name.
  77. func Register(name string, i interface{}) {
  78. DefaultRegistry.Register(name, i)
  79. }
  80. // Run all registered healthchecks.
  81. func RunHealthchecks() {
  82. DefaultRegistry.RunHealthchecks()
  83. }
  84. // Unregister the metric with the given name.
  85. func Unregister(name string) {
  86. DefaultRegistry.Unregister(name)
  87. }
  88. func init() {
  89. DefaultRegistry = NewRegistry()
  90. }