registry.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package metrics
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "sync"
  7. )
  8. // DuplicateMetric is the error returned by Registry.Register when a metric
  9. // already exists. If you mean to Register that metric you must first
  10. // Unregister the existing metric.
  11. type DuplicateMetric string
  12. func (err DuplicateMetric) Error() string {
  13. return fmt.Sprintf("duplicate metric: %s", string(err))
  14. }
  15. // A Registry holds references to a set of metrics by name and can iterate
  16. // over them, calling callback functions provided by the user.
  17. //
  18. // This is an interface so as to encourage other structs to implement
  19. // the Registry API as appropriate.
  20. type Registry interface {
  21. // Call the given function for each registered metric.
  22. Each(func(string, interface{}))
  23. // Get the metric by the given name or nil if none is registered.
  24. Get(string) interface{}
  25. // Gets an existing metric or registers the given one.
  26. // The interface can be the metric to register if not found in registry,
  27. // or a function returning the metric for lazy instantiation.
  28. GetOrRegister(string, interface{}) interface{}
  29. // Register the given metric under the given name.
  30. Register(string, interface{}) error
  31. // Run all registered healthchecks.
  32. RunHealthchecks()
  33. // Unregister the metric with the given name.
  34. Unregister(string)
  35. // Unregister all metrics. (Mostly for testing.)
  36. UnregisterAll()
  37. }
  38. // The standard implementation of a Registry is a mutex-protected map
  39. // of names to metrics.
  40. type StandardRegistry struct {
  41. metrics map[string]interface{}
  42. mutex sync.Mutex
  43. }
  44. // Create a new registry.
  45. func NewRegistry() Registry {
  46. return &StandardRegistry{metrics: make(map[string]interface{})}
  47. }
  48. // Call the given function for each registered metric.
  49. func (r *StandardRegistry) Each(f func(string, interface{})) {
  50. for name, i := range r.registered() {
  51. f(name, i)
  52. }
  53. }
  54. // Get the metric by the given name or nil if none is registered.
  55. func (r *StandardRegistry) Get(name string) interface{} {
  56. r.mutex.Lock()
  57. defer r.mutex.Unlock()
  58. return r.metrics[name]
  59. }
  60. // Gets an existing metric or creates and registers a new one. Threadsafe
  61. // alternative to calling Get and Register on failure.
  62. // The interface can be the metric to register if not found in registry,
  63. // or a function returning the metric for lazy instantiation.
  64. func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
  65. r.mutex.Lock()
  66. defer r.mutex.Unlock()
  67. if metric, ok := r.metrics[name]; ok {
  68. return metric
  69. }
  70. if v := reflect.ValueOf(i); v.Kind() == reflect.Func {
  71. i = v.Call(nil)[0].Interface()
  72. }
  73. r.register(name, i)
  74. return i
  75. }
  76. // Register the given metric under the given name. Returns a DuplicateMetric
  77. // if a metric by the given name is already registered.
  78. func (r *StandardRegistry) Register(name string, i interface{}) error {
  79. r.mutex.Lock()
  80. defer r.mutex.Unlock()
  81. return r.register(name, i)
  82. }
  83. // Run all registered healthchecks.
  84. func (r *StandardRegistry) RunHealthchecks() {
  85. r.mutex.Lock()
  86. defer r.mutex.Unlock()
  87. for _, i := range r.metrics {
  88. if h, ok := i.(Healthcheck); ok {
  89. h.Check()
  90. }
  91. }
  92. }
  93. // Unregister the metric with the given name.
  94. func (r *StandardRegistry) Unregister(name string) {
  95. r.mutex.Lock()
  96. defer r.mutex.Unlock()
  97. delete(r.metrics, name)
  98. }
  99. // Unregister all metrics. (Mostly for testing.)
  100. func (r *StandardRegistry) UnregisterAll() {
  101. r.mutex.Lock()
  102. defer r.mutex.Unlock()
  103. for name, _ := range r.metrics {
  104. delete(r.metrics, name)
  105. }
  106. }
  107. func (r *StandardRegistry) register(name string, i interface{}) error {
  108. if _, ok := r.metrics[name]; ok {
  109. return DuplicateMetric(name)
  110. }
  111. switch i.(type) {
  112. case Counter, Gauge, GaugeFloat64, Healthcheck, Histogram, Meter, Timer:
  113. r.metrics[name] = i
  114. }
  115. return nil
  116. }
  117. func (r *StandardRegistry) registered() map[string]interface{} {
  118. r.mutex.Lock()
  119. defer r.mutex.Unlock()
  120. metrics := make(map[string]interface{}, len(r.metrics))
  121. for name, i := range r.metrics {
  122. metrics[name] = i
  123. }
  124. return metrics
  125. }
  126. type PrefixedRegistry struct {
  127. underlying Registry
  128. prefix string
  129. }
  130. func NewPrefixedRegistry(prefix string) Registry {
  131. return &PrefixedRegistry{
  132. underlying: NewRegistry(),
  133. prefix: prefix,
  134. }
  135. }
  136. func NewPrefixedChildRegistry(parent Registry, prefix string) Registry {
  137. return &PrefixedRegistry{
  138. underlying: parent,
  139. prefix: prefix,
  140. }
  141. }
  142. // Call the given function for each registered metric.
  143. func (r *PrefixedRegistry) Each(fn func(string, interface{})) {
  144. wrappedFn := func(name string, iface interface{}) {
  145. if strings.HasPrefix(name, r.prefix) {
  146. fn(name, iface)
  147. } else {
  148. return
  149. }
  150. }
  151. r.underlying.Each(wrappedFn)
  152. }
  153. // Get the metric by the given name or nil if none is registered.
  154. func (r *PrefixedRegistry) Get(name string) interface{} {
  155. realName := r.prefix + name
  156. return r.underlying.Get(realName)
  157. }
  158. // Gets an existing metric or registers the given one.
  159. // The interface can be the metric to register if not found in registry,
  160. // or a function returning the metric for lazy instantiation.
  161. func (r *PrefixedRegistry) GetOrRegister(name string, metric interface{}) interface{} {
  162. realName := r.prefix + name
  163. return r.underlying.GetOrRegister(realName, metric)
  164. }
  165. // Register the given metric under the given name. The name will be prefixed.
  166. func (r *PrefixedRegistry) Register(name string, metric interface{}) error {
  167. realName := r.prefix + name
  168. return r.underlying.Register(realName, metric)
  169. }
  170. // Run all registered healthchecks.
  171. func (r *PrefixedRegistry) RunHealthchecks() {
  172. r.underlying.RunHealthchecks()
  173. }
  174. // Unregister the metric with the given name. The name will be prefixed.
  175. func (r *PrefixedRegistry) Unregister(name string) {
  176. realName := r.prefix + name
  177. r.underlying.Unregister(realName)
  178. }
  179. // Unregister all metrics. (Mostly for testing.)
  180. func (r *PrefixedRegistry) UnregisterAll() {
  181. r.underlying.UnregisterAll()
  182. }
  183. var DefaultRegistry Registry = NewRegistry()
  184. // Call the given function for each registered metric.
  185. func Each(f func(string, interface{})) {
  186. DefaultRegistry.Each(f)
  187. }
  188. // Get the metric by the given name or nil if none is registered.
  189. func Get(name string) interface{} {
  190. return DefaultRegistry.Get(name)
  191. }
  192. // Gets an existing metric or creates and registers a new one. Threadsafe
  193. // alternative to calling Get and Register on failure.
  194. func GetOrRegister(name string, i interface{}) interface{} {
  195. return DefaultRegistry.GetOrRegister(name, i)
  196. }
  197. // Register the given metric under the given name. Returns a DuplicateMetric
  198. // if a metric by the given name is already registered.
  199. func Register(name string, i interface{}) error {
  200. return DefaultRegistry.Register(name, i)
  201. }
  202. // Register the given metric under the given name. Panics if a metric by the
  203. // given name is already registered.
  204. func MustRegister(name string, i interface{}) {
  205. if err := Register(name, i); err != nil {
  206. panic(err)
  207. }
  208. }
  209. // Run all registered healthchecks.
  210. func RunHealthchecks() {
  211. DefaultRegistry.RunHealthchecks()
  212. }
  213. // Unregister the metric with the given name.
  214. func Unregister(name string) {
  215. DefaultRegistry.Unregister(name)
  216. }