registry.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // Unregister all metrics. (Mostly for testing.)
  35. UnregisterAll()
  36. }
  37. // The standard implementation of a Registry is a mutex-protected map
  38. // of names to metrics.
  39. type StandardRegistry struct {
  40. metrics map[string]interface{}
  41. mutex sync.Mutex
  42. }
  43. // Create a new registry.
  44. func NewRegistry() Registry {
  45. return &StandardRegistry{metrics: make(map[string]interface{})}
  46. }
  47. // Call the given function for each registered metric.
  48. func (r *StandardRegistry) Each(f func(string, interface{})) {
  49. for name, i := range r.registered() {
  50. f(name, i)
  51. }
  52. }
  53. // Get the metric by the given name or nil if none is registered.
  54. func (r *StandardRegistry) Get(name string) interface{} {
  55. r.mutex.Lock()
  56. defer r.mutex.Unlock()
  57. return r.metrics[name]
  58. }
  59. // Gets an existing metric or creates and registers a new one. Threadsafe
  60. // alternative to calling Get and Register on failure.
  61. // The interface can be the metric to register if not found in registry,
  62. // or a function returning the metric for lazy instantiation.
  63. func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
  64. r.mutex.Lock()
  65. defer r.mutex.Unlock()
  66. if metric, ok := r.metrics[name]; ok {
  67. return metric
  68. }
  69. if v := reflect.ValueOf(i); v.Kind() == reflect.Func {
  70. i = v.Call(nil)[0].Interface()
  71. }
  72. r.register(name, i)
  73. return i
  74. }
  75. // Register the given metric under the given name. Returns a DuplicateMetric
  76. // if a metric by the given name is already registered.
  77. func (r *StandardRegistry) Register(name string, i interface{}) error {
  78. r.mutex.Lock()
  79. defer r.mutex.Unlock()
  80. return r.register(name, i)
  81. }
  82. // Run all registered healthchecks.
  83. func (r *StandardRegistry) RunHealthchecks() {
  84. r.mutex.Lock()
  85. defer r.mutex.Unlock()
  86. for _, i := range r.metrics {
  87. if h, ok := i.(Healthcheck); ok {
  88. h.Check()
  89. }
  90. }
  91. }
  92. // Unregister the metric with the given name.
  93. func (r *StandardRegistry) Unregister(name string) {
  94. r.mutex.Lock()
  95. defer r.mutex.Unlock()
  96. delete(r.metrics, name)
  97. }
  98. // Unregister all metrics. (Mostly for testing.)
  99. func (r *StandardRegistry) UnregisterAll() {
  100. r.mutex.Lock()
  101. defer r.mutex.Unlock()
  102. for name, _ := range r.metrics {
  103. delete(r.metrics, name)
  104. }
  105. }
  106. func (r *StandardRegistry) register(name string, i interface{}) error {
  107. if _, ok := r.metrics[name]; ok {
  108. return DuplicateMetric(name)
  109. }
  110. switch i.(type) {
  111. case Counter, Gauge, GaugeFloat64, Healthcheck, Histogram, Meter, Timer:
  112. r.metrics[name] = i
  113. }
  114. return nil
  115. }
  116. func (r *StandardRegistry) registered() map[string]interface{} {
  117. r.mutex.Lock()
  118. defer r.mutex.Unlock()
  119. metrics := make(map[string]interface{}, len(r.metrics))
  120. for name, i := range r.metrics {
  121. metrics[name] = i
  122. }
  123. return metrics
  124. }
  125. type PrefixedRegistry struct {
  126. underlying Registry
  127. prefix string
  128. }
  129. func NewPrefixedRegistry(prefix string) Registry {
  130. return &PrefixedRegistry{
  131. underlying: NewRegistry(),
  132. prefix: prefix,
  133. }
  134. }
  135. // Call the given function for each registered metric.
  136. func (r *PrefixedRegistry) Each(fn func(string, interface{})) {
  137. r.underlying.Each(fn)
  138. }
  139. // Get the metric by the given name or nil if none is registered.
  140. func (r *PrefixedRegistry) Get(name string) interface{} {
  141. return r.underlying.Get(name)
  142. }
  143. // Gets an existing metric or registers the given one.
  144. // The interface can be the metric to register if not found in registry,
  145. // or a function returning the metric for lazy instantiation.
  146. func (r *PrefixedRegistry) GetOrRegister(name string, metric interface{}) interface{} {
  147. realName := r.prefix + name
  148. return r.underlying.GetOrRegister(realName, metric)
  149. }
  150. // Register the given metric under the given name. The name will be prefixed.
  151. func (r *PrefixedRegistry) Register(name string, metric interface{}) error {
  152. realName := r.prefix + name
  153. return r.underlying.Register(realName, metric)
  154. }
  155. // Run all registered healthchecks.
  156. func (r *PrefixedRegistry) RunHealthchecks() {
  157. r.underlying.RunHealthchecks()
  158. }
  159. // Unregister the metric with the given name. The name will be prefixed.
  160. func (r *PrefixedRegistry) Unregister(name string) {
  161. realName := r.prefix + name
  162. r.underlying.Unregister(realName)
  163. }
  164. // Unregister all metrics. (Mostly for testing.)
  165. func (r *PrefixedRegistry) UnregisterAll() {
  166. r.underlying.UnregisterAll()
  167. }
  168. var DefaultRegistry Registry = NewRegistry()
  169. // Call the given function for each registered metric.
  170. func Each(f func(string, interface{})) {
  171. DefaultRegistry.Each(f)
  172. }
  173. // Get the metric by the given name or nil if none is registered.
  174. func Get(name string) interface{} {
  175. return DefaultRegistry.Get(name)
  176. }
  177. // Gets an existing metric or creates and registers a new one. Threadsafe
  178. // alternative to calling Get and Register on failure.
  179. func GetOrRegister(name string, i interface{}) interface{} {
  180. return DefaultRegistry.GetOrRegister(name, i)
  181. }
  182. // Register the given metric under the given name. Returns a DuplicateMetric
  183. // if a metric by the given name is already registered.
  184. func Register(name string, i interface{}) error {
  185. return DefaultRegistry.Register(name, i)
  186. }
  187. // Register the given metric under the given name. Panics if a metric by the
  188. // given name is already registered.
  189. func MustRegister(name string, i interface{}) {
  190. if err := Register(name, i); err != nil {
  191. panic(err)
  192. }
  193. }
  194. // Run all registered healthchecks.
  195. func RunHealthchecks() {
  196. DefaultRegistry.RunHealthchecks()
  197. }
  198. // Unregister the metric with the given name.
  199. func Unregister(name string) {
  200. DefaultRegistry.Unregister(name)
  201. }