registry.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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(prefix string) func(string, interface{}) {
  145. return func(name string, iface interface{}) {
  146. if strings.HasPrefix(name, prefix) {
  147. fn(name, iface)
  148. } else {
  149. return
  150. }
  151. }
  152. }
  153. baseRegistry, prefix := findPrefix(r, "")
  154. baseRegistry.Each(wrappedFn(prefix))
  155. }
  156. func findPrefix(registry Registry, prefix string) (Registry, string) {
  157. switch r := registry.(type) {
  158. case *PrefixedRegistry:
  159. return findPrefix(r.underlying, r.prefix+prefix)
  160. case *StandardRegistry:
  161. return r, prefix
  162. }
  163. return nil, ""
  164. }
  165. // Get the metric by the given name or nil if none is registered.
  166. func (r *PrefixedRegistry) Get(name string) interface{} {
  167. realName := r.prefix + name
  168. return r.underlying.Get(realName)
  169. }
  170. // Gets an existing metric or registers the given one.
  171. // The interface can be the metric to register if not found in registry,
  172. // or a function returning the metric for lazy instantiation.
  173. func (r *PrefixedRegistry) GetOrRegister(name string, metric interface{}) interface{} {
  174. realName := r.prefix + name
  175. return r.underlying.GetOrRegister(realName, metric)
  176. }
  177. // Register the given metric under the given name. The name will be prefixed.
  178. func (r *PrefixedRegistry) Register(name string, metric interface{}) error {
  179. realName := r.prefix + name
  180. return r.underlying.Register(realName, metric)
  181. }
  182. // Run all registered healthchecks.
  183. func (r *PrefixedRegistry) RunHealthchecks() {
  184. r.underlying.RunHealthchecks()
  185. }
  186. // Unregister the metric with the given name. The name will be prefixed.
  187. func (r *PrefixedRegistry) Unregister(name string) {
  188. realName := r.prefix + name
  189. r.underlying.Unregister(realName)
  190. }
  191. // Unregister all metrics. (Mostly for testing.)
  192. func (r *PrefixedRegistry) UnregisterAll() {
  193. r.underlying.UnregisterAll()
  194. }
  195. var DefaultRegistry Registry = NewRegistry()
  196. // Call the given function for each registered metric.
  197. func Each(f func(string, interface{})) {
  198. DefaultRegistry.Each(f)
  199. }
  200. // Get the metric by the given name or nil if none is registered.
  201. func Get(name string) interface{} {
  202. return DefaultRegistry.Get(name)
  203. }
  204. // Gets an existing metric or creates and registers a new one. Threadsafe
  205. // alternative to calling Get and Register on failure.
  206. func GetOrRegister(name string, i interface{}) interface{} {
  207. return DefaultRegistry.GetOrRegister(name, i)
  208. }
  209. // Register the given metric under the given name. Returns a DuplicateMetric
  210. // if a metric by the given name is already registered.
  211. func Register(name string, i interface{}) error {
  212. return DefaultRegistry.Register(name, i)
  213. }
  214. // Register the given metric under the given name. Panics if a metric by the
  215. // given name is already registered.
  216. func MustRegister(name string, i interface{}) {
  217. if err := Register(name, i); err != nil {
  218. panic(err)
  219. }
  220. }
  221. // Run all registered healthchecks.
  222. func RunHealthchecks() {
  223. DefaultRegistry.RunHealthchecks()
  224. }
  225. // Unregister the metric with the given name.
  226. func Unregister(name string) {
  227. DefaultRegistry.Unregister(name)
  228. }