registry.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package metrics
  2. import "sync"
  3. // A Registry holds references to a set of metrics by name and can iterate
  4. // over them, calling callback functions provided by the user.
  5. //
  6. // This is an interface so as to encourage other structs to implement
  7. // the Registry API as appropriate.
  8. type Registry interface {
  9. EachCounter(func(string, Counter))
  10. EachGauge(func(string, Gauge))
  11. EachHealthcheck(func(string, Healthcheck))
  12. EachHistogram(func(string, Histogram))
  13. EachMeter(func(string, Meter))
  14. EachTimer(func(string, Timer))
  15. GetCounter(string) Counter
  16. GetGauge(string) Gauge
  17. GetHealthcheck(string) Healthcheck
  18. GetHistogram(string) Histogram
  19. GetMeter(string) Meter
  20. GetTimer(string) Timer
  21. RegisterCounter(string, Counter)
  22. RegisterGauge(string, Gauge)
  23. RegisterHealthcheck(string, Healthcheck)
  24. RegisterHistogram(string, Histogram)
  25. RegisterMeter(string, Meter)
  26. RegisterTimer(string, Timer)
  27. RunHealthchecks()
  28. UnregisterCounter(string)
  29. UnregisterGauge(string)
  30. UnregisterHealthcheck(string)
  31. UnregisterHistogram(string)
  32. UnregisterMeter(string)
  33. UnregisterTimer(string)
  34. }
  35. // The standard implementation of a Registry is a set of mutex-protected
  36. // maps of names to metrics.
  37. type registry struct {
  38. mutex *sync.Mutex
  39. counters map[string]Counter
  40. gauges map[string]Gauge
  41. healthchecks map[string]Healthcheck
  42. histograms map[string]Histogram
  43. meters map[string]Meter
  44. timers map[string]Timer
  45. }
  46. // Create a new registry.
  47. func NewRegistry() Registry {
  48. return &registry {
  49. &sync.Mutex{},
  50. make(map[string]Counter),
  51. make(map[string]Gauge),
  52. make(map[string]Healthcheck),
  53. make(map[string]Histogram),
  54. make(map[string]Meter),
  55. make(map[string]Timer),
  56. }
  57. }
  58. // Call the given function for each registered counter.
  59. func (r *registry) EachCounter(f func(string, Counter)) {
  60. r.mutex.Lock()
  61. for name, c := range r.counters { f(name, c) }
  62. r.mutex.Unlock()
  63. }
  64. // Call the given function for each registered gauge.
  65. func (r *registry) EachGauge(f func(string, Gauge)) {
  66. r.mutex.Lock()
  67. for name, g := range r.gauges { f(name, g) }
  68. r.mutex.Unlock()
  69. }
  70. // Call the given function for each registered healthcheck.
  71. func (r *registry) EachHealthcheck(f func(string, Healthcheck)) {
  72. r.mutex.Lock()
  73. for name, h := range r.healthchecks { f(name, h) }
  74. r.mutex.Unlock()
  75. }
  76. // Call the given function for each registered histogram.
  77. func (r *registry) EachHistogram(f func(string, Histogram)) {
  78. r.mutex.Lock()
  79. for name, h := range r.histograms { f(name, h) }
  80. r.mutex.Unlock()
  81. }
  82. // Call the given function for each registered meter.
  83. func (r *registry) EachMeter(f func(string, Meter)) {
  84. r.mutex.Lock()
  85. for name, m := range r.meters { f(name, m) }
  86. r.mutex.Unlock()
  87. }
  88. // Call the given function for each registered timer.
  89. func (r *registry) EachTimer(f func(string, Timer)) {
  90. r.mutex.Lock()
  91. for name, t := range r.timers { f(name, t) }
  92. r.mutex.Unlock()
  93. }
  94. // Get the Counter by the given name or nil if none is registered.
  95. func (r *registry) GetCounter(name string) Counter {
  96. r.mutex.Lock()
  97. c := r.counters[name]
  98. r.mutex.Unlock()
  99. return c
  100. }
  101. // Get the Gauge by the given name or nil if none is registered.
  102. func (r *registry) GetGauge(name string) Gauge {
  103. r.mutex.Lock()
  104. g := r.gauges[name]
  105. r.mutex.Unlock()
  106. return g
  107. }
  108. // Get the Healthcheck by the given name or nil if none is registered.
  109. func (r *registry) GetHealthcheck(name string) Healthcheck {
  110. r.mutex.Lock()
  111. h := r.healthchecks[name]
  112. r.mutex.Unlock()
  113. return h
  114. }
  115. // Get the Histogram by the given name or nil if none is registered.
  116. func (r *registry) GetHistogram(name string) Histogram {
  117. r.mutex.Lock()
  118. h := r.histograms[name]
  119. r.mutex.Unlock()
  120. return h
  121. }
  122. // Get the Meter by the given name or nil if none is registered.
  123. func (r *registry) GetMeter(name string) Meter {
  124. r.mutex.Lock()
  125. m := r.meters[name]
  126. r.mutex.Unlock()
  127. return m
  128. }
  129. // Get the Timer by the given name or nil if none is registered.
  130. func (r *registry) GetTimer(name string) Timer {
  131. r.mutex.Lock()
  132. t := r.timers[name]
  133. r.mutex.Unlock()
  134. return t
  135. }
  136. // Register the given Counter under the given name.
  137. func (r *registry) RegisterCounter(name string, c Counter) {
  138. r.mutex.Lock()
  139. r.counters[name] = c
  140. r.mutex.Unlock()
  141. }
  142. // Register the given Gauge under the given name.
  143. func (r *registry) RegisterGauge(name string, g Gauge) {
  144. r.mutex.Lock()
  145. r.gauges[name] = g
  146. r.mutex.Unlock()
  147. }
  148. // Register the given Healthcheck under the given name.
  149. func (r *registry) RegisterHealthcheck(name string, h Healthcheck) {
  150. r.mutex.Lock()
  151. r.healthchecks[name] = h
  152. r.mutex.Unlock()
  153. }
  154. // Register the given Histogram under the given name.
  155. func (r *registry) RegisterHistogram(name string, h Histogram) {
  156. r.mutex.Lock()
  157. r.histograms[name] = h
  158. r.mutex.Unlock()
  159. }
  160. // Register the given Meter under the given name.
  161. func (r *registry) RegisterMeter(name string, m Meter) {
  162. r.mutex.Lock()
  163. r.meters[name] = m
  164. r.mutex.Unlock()
  165. }
  166. // Register the given Timer under the given name.
  167. func (r *registry) RegisterTimer(name string, t Timer) {
  168. r.mutex.Lock()
  169. r.timers[name] = t
  170. r.mutex.Unlock()
  171. }
  172. // Run all registered healthchecks.
  173. func (r *registry) RunHealthchecks() {
  174. r.mutex.Lock()
  175. for _, h := range r.healthchecks { h.Check() }
  176. r.mutex.Unlock()
  177. }
  178. // Unregister the given Counter with the given name.
  179. func (r *registry) UnregisterCounter(name string) {
  180. r.mutex.Lock()
  181. r.counters[name] = nil, false
  182. r.mutex.Unlock()
  183. }
  184. // Unregister the given Gauge with the given name.
  185. func (r *registry) UnregisterGauge(name string) {
  186. r.mutex.Lock()
  187. r.gauges[name] = nil, false
  188. r.mutex.Unlock()
  189. }
  190. // Unregister the given Healthcheck with the given name.
  191. func (r *registry) UnregisterHealthcheck(name string) {
  192. r.mutex.Lock()
  193. r.healthchecks[name] = nil, false
  194. r.mutex.Unlock()
  195. }
  196. // Unregister the given Histogram with the given name.
  197. func (r *registry) UnregisterHistogram(name string) {
  198. r.mutex.Lock()
  199. r.histograms[name] = nil, false
  200. r.mutex.Unlock()
  201. }
  202. // Unregister the given Meter with the given name.
  203. func (r *registry) UnregisterMeter(name string) {
  204. r.mutex.Lock()
  205. r.meters[name] = nil, false
  206. r.mutex.Unlock()
  207. }
  208. // Unregister the given Timer with the given name.
  209. func (r *registry) UnregisterTimer(name string) {
  210. r.mutex.Lock()
  211. r.timers[name] = nil, false
  212. r.mutex.Unlock()
  213. }