registry.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package metrics
  2. import (
  3. "fmt"
  4. "reflect"
  5. "sync"
  6. )
  7. // DuplicateMetric is the error returned by Registry.Register when a metric
  8. // already exists. If you mean to Register that metric you must first
  9. // Unregister the existing metric.
  10. type DuplicateMetric string
  11. func (err DuplicateMetric) Error() string {
  12. return fmt.Sprintf("duplicate metric: %s", string(err))
  13. }
  14. // A Registry holds references to a set of metrics by name and can iterate
  15. // over them, calling callback functions provided by the user.
  16. //
  17. // This is an interface so as to encourage other structs to implement
  18. // the Registry API as appropriate.
  19. type Registry interface {
  20. // Call the given function for each registered metric.
  21. Each(func(string, interface{}))
  22. // Get the metric by the given name or nil if none is registered.
  23. Get(string) interface{}
  24. // Gets an existing metric or registers the given one.
  25. // The interface can be the metric to register if not found in registry,
  26. // or a function returning the metric for lazy instantiation.
  27. GetOrRegister(string, interface{}) interface{}
  28. // Register the given metric under the given name.
  29. Register(string, interface{}) error
  30. // Run all registered healthchecks.
  31. RunHealthchecks()
  32. // Unregister the metric with the given name.
  33. Unregister(string)
  34. }
  35. // The standard implementation of a Registry is a mutex-protected map
  36. // of names to metrics.
  37. type StandardRegistry struct {
  38. metrics map[string]interface{}
  39. mutex sync.Mutex
  40. }
  41. // Create a new registry.
  42. func NewRegistry() Registry {
  43. return &StandardRegistry{metrics: make(map[string]interface{})}
  44. }
  45. // Call the given function for each registered metric.
  46. func (r *StandardRegistry) Each(f func(string, interface{})) {
  47. for name, i := range r.registered() {
  48. f(name, i)
  49. }
  50. }
  51. // Get the metric by the given name or nil if none is registered.
  52. func (r *StandardRegistry) Get(name string) interface{} {
  53. r.mutex.Lock()
  54. defer r.mutex.Unlock()
  55. return r.metrics[name]
  56. }
  57. // Gets an existing metric or creates and registers a new one. Threadsafe
  58. // alternative to calling Get and Register on failure.
  59. // The interface can be the metric to register if not found in registry,
  60. // or a function returning the metric for lazy instantiation.
  61. func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
  62. r.mutex.Lock()
  63. defer r.mutex.Unlock()
  64. if metric, ok := r.metrics[name]; ok {
  65. return metric
  66. }
  67. if v := reflect.ValueOf(i); v.Kind() == reflect.Func {
  68. i = v.Call(nil)[0].Interface()
  69. }
  70. r.register(name, i)
  71. return i
  72. }
  73. // Register the given metric under the given name. Returns a DuplicateMetric
  74. // if a metric by the given name is already registered.
  75. func (r *StandardRegistry) Register(name string, i interface{}) error {
  76. r.mutex.Lock()
  77. defer r.mutex.Unlock()
  78. return r.register(name, i)
  79. }
  80. // Run all registered healthchecks.
  81. func (r *StandardRegistry) RunHealthchecks() {
  82. r.mutex.Lock()
  83. defer r.mutex.Unlock()
  84. for _, i := range r.metrics {
  85. if h, ok := i.(Healthcheck); ok {
  86. h.Check()
  87. }
  88. }
  89. }
  90. // Unregister the metric with the given name.
  91. func (r *StandardRegistry) Unregister(name string) {
  92. r.mutex.Lock()
  93. defer r.mutex.Unlock()
  94. delete(r.metrics, name)
  95. }
  96. func (r *StandardRegistry) register(name string, i interface{}) error {
  97. if _, ok := r.metrics[name]; ok {
  98. return DuplicateMetric(name)
  99. }
  100. switch i.(type) {
  101. case Counter, Gauge, GaugeFloat64, Healthcheck, Histogram, Meter, Timer:
  102. r.metrics[name] = i
  103. }
  104. return nil
  105. }
  106. func (r *StandardRegistry) registered() map[string]interface{} {
  107. metrics := make(map[string]interface{}, len(r.metrics))
  108. r.mutex.Lock()
  109. defer r.mutex.Unlock()
  110. for name, i := range r.metrics {
  111. metrics[name] = i
  112. }
  113. return metrics
  114. }
  115. var DefaultRegistry Registry = NewRegistry()
  116. // Call the given function for each registered metric.
  117. func Each(f func(string, interface{})) {
  118. DefaultRegistry.Each(f)
  119. }
  120. // Get the metric by the given name or nil if none is registered.
  121. func Get(name string) interface{} {
  122. return DefaultRegistry.Get(name)
  123. }
  124. // Gets an existing metric or creates and registers a new one. Threadsafe
  125. // alternative to calling Get and Register on failure.
  126. func GetOrRegister(name string, i interface{}) interface{} {
  127. return DefaultRegistry.GetOrRegister(name, i)
  128. }
  129. // Register the given metric under the given name. Returns a DuplicateMetric
  130. // if a metric by the given name is already registered.
  131. func Register(name string, i interface{}) error {
  132. return DefaultRegistry.Register(name, i)
  133. }
  134. // Run all registered healthchecks.
  135. func RunHealthchecks() {
  136. DefaultRegistry.RunHealthchecks()
  137. }
  138. // Unregister the metric with the given name.
  139. func Unregister(name string) {
  140. DefaultRegistry.Unregister(name)
  141. }