registry.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // Register the given metric under the given name.
  14. Register(string, interface{})
  15. // Gets an existing metric or creates and registers a new one.
  16. GetOrRegister(string, interface{}) 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. // Register the given metric under the given name.
  45. func (r *StandardRegistry) Register(name string, i interface{}) {
  46. switch i.(type) {
  47. case Counter, Gauge, Healthcheck, Histogram, Meter, Timer:
  48. r.mutex.Lock()
  49. defer r.mutex.Unlock()
  50. r.metrics[name] = i
  51. }
  52. }
  53. // Gets an existing metric or creates and registers a new one. Threadsafe
  54. // alternative to calling Get and Register on failure.
  55. func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
  56. r.mutex.Lock()
  57. defer r.mutex.Unlock()
  58. if metric, ok := r.metrics[name]; ok {
  59. // Found existing metric, return it instead of the new instance
  60. return metric
  61. }
  62. // No existing metric, register the new instance
  63. switch i.(type) {
  64. case Counter, Gauge, Healthcheck, Histogram, Meter, Timer:
  65. r.metrics[name] = i
  66. }
  67. return i
  68. }
  69. // Run all registered healthchecks.
  70. func (r *StandardRegistry) RunHealthchecks() {
  71. r.mutex.Lock()
  72. defer r.mutex.Unlock()
  73. for _, i := range r.metrics {
  74. if h, ok := i.(Healthcheck); ok {
  75. h.Check()
  76. }
  77. }
  78. }
  79. // Unregister the metric with the given name.
  80. func (r *StandardRegistry) Unregister(name string) {
  81. r.mutex.Lock()
  82. defer r.mutex.Unlock()
  83. delete(r.metrics, name)
  84. }
  85. func (r *StandardRegistry) registered() map[string]interface{} {
  86. metrics := make(map[string]interface{}, len(r.metrics))
  87. r.mutex.Lock()
  88. defer r.mutex.Unlock()
  89. for name, i := range r.metrics {
  90. metrics[name] = i
  91. }
  92. return metrics
  93. }
  94. var DefaultRegistry Registry = NewRegistry()
  95. // Call the given function for each registered metric.
  96. func Each(f func(string, interface{})) {
  97. DefaultRegistry.Each(f)
  98. }
  99. // Get the metric by the given name or nil if none is registered.
  100. func Get(name string) interface{} {
  101. return DefaultRegistry.Get(name)
  102. }
  103. // Register the given metric under the given name.
  104. func Register(name string, i interface{}) {
  105. DefaultRegistry.Register(name, i)
  106. }
  107. // Run all registered healthchecks.
  108. func RunHealthchecks() {
  109. DefaultRegistry.RunHealthchecks()
  110. }
  111. // Unregister the metric with the given name.
  112. func Unregister(name string) {
  113. DefaultRegistry.Unregister(name)
  114. }