registry.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Call the given function for each registered metric.
  10. Each(func(string, interface{}))
  11. // Get the metric by the given name or nil if none is registered.
  12. Get(string) interface{}
  13. // Gets an existing metric or creates and registers a new one.
  14. GetOrRegister(string, interface{}) interface{}
  15. // Register the given metric under the given name.
  16. Register(string, interface{})
  17. // Run all registered healthchecks.
  18. RunHealthchecks()
  19. // Unregister the metric with the given name.
  20. Unregister(string)
  21. }
  22. // The standard implementation of a Registry is a mutex-protected map
  23. // of names to metrics.
  24. type StandardRegistry struct {
  25. metrics map[string]interface{}
  26. mutex sync.Mutex
  27. }
  28. // Create a new registry.
  29. func NewRegistry() Registry {
  30. return &StandardRegistry{metrics: make(map[string]interface{})}
  31. }
  32. // Call the given function for each registered metric.
  33. func (r *StandardRegistry) Each(f func(string, interface{})) {
  34. for name, i := range r.registered() {
  35. f(name, i)
  36. }
  37. }
  38. // Get the metric by the given name or nil if none is registered.
  39. func (r *StandardRegistry) Get(name string) interface{} {
  40. r.mutex.Lock()
  41. defer r.mutex.Unlock()
  42. return r.metrics[name]
  43. }
  44. // Gets an existing metric or creates and registers a new one. Threadsafe
  45. // alternative to calling Get and Register on failure.
  46. func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
  47. r.mutex.Lock()
  48. defer r.mutex.Unlock()
  49. if metric, ok := r.metrics[name]; ok {
  50. return metric
  51. }
  52. r.register(name, i)
  53. return i
  54. }
  55. // Register the given metric under the given name.
  56. func (r *StandardRegistry) Register(name string, i interface{}) {
  57. r.mutex.Lock()
  58. defer r.mutex.Unlock()
  59. r.register(name, i)
  60. }
  61. // Run all registered healthchecks.
  62. func (r *StandardRegistry) RunHealthchecks() {
  63. r.mutex.Lock()
  64. defer r.mutex.Unlock()
  65. for _, i := range r.metrics {
  66. if h, ok := i.(Healthcheck); ok {
  67. h.Check()
  68. }
  69. }
  70. }
  71. // Unregister the metric with the given name.
  72. func (r *StandardRegistry) Unregister(name string) {
  73. r.mutex.Lock()
  74. defer r.mutex.Unlock()
  75. delete(r.metrics, name)
  76. }
  77. func (r *StandardRegistry) register(name string, i interface{}) {
  78. switch i.(type) {
  79. case Counter, Gauge, Healthcheck, Histogram, Meter, Timer:
  80. r.metrics[name] = i
  81. }
  82. }
  83. func (r *StandardRegistry) registered() map[string]interface{} {
  84. metrics := make(map[string]interface{}, len(r.metrics))
  85. r.mutex.Lock()
  86. defer r.mutex.Unlock()
  87. for name, i := range r.metrics {
  88. metrics[name] = i
  89. }
  90. return metrics
  91. }
  92. var DefaultRegistry Registry = NewRegistry()
  93. // Call the given function for each registered metric.
  94. func Each(f func(string, interface{})) {
  95. DefaultRegistry.Each(f)
  96. }
  97. // Get the metric by the given name or nil if none is registered.
  98. func Get(name string) interface{} {
  99. return DefaultRegistry.Get(name)
  100. }
  101. // Gets an existing metric or creates and registers a new one. Threadsafe
  102. // alternative to calling Get and Register on failure.
  103. func GetOrRegister(name string, i interface{}) interface{} {
  104. return DefaultRegistry.GetOrRegister(name, i)
  105. }
  106. // Register the given metric under the given name.
  107. func Register(name string, i interface{}) {
  108. DefaultRegistry.Register(name, i)
  109. }
  110. // Run all registered healthchecks.
  111. func RunHealthchecks() {
  112. DefaultRegistry.RunHealthchecks()
  113. }
  114. // Unregister the metric with the given name.
  115. func Unregister(name string) {
  116. DefaultRegistry.Unregister(name)
  117. }