registry.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. func NewPrefixedChildRegistry(parent Registry, prefix string) Registry {
  136. return &PrefixedRegistry{
  137. underlying: parent,
  138. prefix: prefix,
  139. }
  140. }
  141. // Call the given function for each registered metric.
  142. func (r *PrefixedRegistry) Each(fn func(string, interface{})) {
  143. r.underlying.Each(fn)
  144. }
  145. // Get the metric by the given name or nil if none is registered.
  146. func (r *PrefixedRegistry) Get(name string) interface{} {
  147. return r.underlying.Get(name)
  148. }
  149. // Gets an existing metric or registers the given one.
  150. // The interface can be the metric to register if not found in registry,
  151. // or a function returning the metric for lazy instantiation.
  152. func (r *PrefixedRegistry) GetOrRegister(name string, metric interface{}) interface{} {
  153. realName := r.prefix + name
  154. return r.underlying.GetOrRegister(realName, metric)
  155. }
  156. // Register the given metric under the given name. The name will be prefixed.
  157. func (r *PrefixedRegistry) Register(name string, metric interface{}) error {
  158. realName := r.prefix + name
  159. return r.underlying.Register(realName, metric)
  160. }
  161. // Run all registered healthchecks.
  162. func (r *PrefixedRegistry) RunHealthchecks() {
  163. r.underlying.RunHealthchecks()
  164. }
  165. // Unregister the metric with the given name. The name will be prefixed.
  166. func (r *PrefixedRegistry) Unregister(name string) {
  167. realName := r.prefix + name
  168. r.underlying.Unregister(realName)
  169. }
  170. // Unregister all metrics. (Mostly for testing.)
  171. func (r *PrefixedRegistry) UnregisterAll() {
  172. r.underlying.UnregisterAll()
  173. }
  174. var DefaultRegistry Registry = NewRegistry()
  175. // Call the given function for each registered metric.
  176. func Each(f func(string, interface{})) {
  177. DefaultRegistry.Each(f)
  178. }
  179. // Get the metric by the given name or nil if none is registered.
  180. func Get(name string) interface{} {
  181. return DefaultRegistry.Get(name)
  182. }
  183. // Gets an existing metric or creates and registers a new one. Threadsafe
  184. // alternative to calling Get and Register on failure.
  185. func GetOrRegister(name string, i interface{}) interface{} {
  186. return DefaultRegistry.GetOrRegister(name, i)
  187. }
  188. // Register the given metric under the given name. Returns a DuplicateMetric
  189. // if a metric by the given name is already registered.
  190. func Register(name string, i interface{}) error {
  191. return DefaultRegistry.Register(name, i)
  192. }
  193. // Register the given metric under the given name. Panics if a metric by the
  194. // given name is already registered.
  195. func MustRegister(name string, i interface{}) {
  196. if err := Register(name, i); err != nil {
  197. panic(err)
  198. }
  199. }
  200. // Run all registered healthchecks.
  201. func RunHealthchecks() {
  202. DefaultRegistry.RunHealthchecks()
  203. }
  204. // Unregister the metric with the given name.
  205. func Unregister(name string) {
  206. DefaultRegistry.Unregister(name)
  207. }