registry.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // Call the given function for each registered metric.
  10. Each(func(string, interface{}))
  11. // Get the metric by the given name or nil if none is registered.
  12. Get(string) interface{}
  13. // Register the given metric under the given name.
  14. Register(string, interface{})
  15. // Run all registered healthchecks.
  16. RunHealthchecks()
  17. // Unregister the metric with the given name.
  18. Unregister(string)
  19. }
  20. // The standard implementation of a Registry is a mutex-protected map
  21. // of names to metrics.
  22. type StandardRegistry struct {
  23. metrics map[string]interface{}
  24. mutex sync.Mutex
  25. }
  26. // Create a new registry.
  27. func NewRegistry() Registry {
  28. return &StandardRegistry{metrics: make(map[string]interface{})}
  29. }
  30. // Call the given function for each registered metric.
  31. func (r *StandardRegistry) Each(f func(string, interface{})) {
  32. for name, i := range r.registered() {
  33. f(name, i)
  34. }
  35. }
  36. // Get the metric by the given name or nil if none is registered.
  37. func (r *StandardRegistry) Get(name string) interface{} {
  38. r.mutex.Lock()
  39. defer r.mutex.Unlock()
  40. return r.metrics[name]
  41. }
  42. // Register the given metric under the given name.
  43. func (r *StandardRegistry) Register(name string, i interface{}) {
  44. switch i.(type) {
  45. case Counter, Gauge, Healthcheck, Histogram, Meter, Timer:
  46. r.mutex.Lock()
  47. defer r.mutex.Unlock()
  48. r.metrics[name] = i
  49. }
  50. }
  51. // Run all registered healthchecks.
  52. func (r *StandardRegistry) RunHealthchecks() {
  53. r.mutex.Lock()
  54. defer r.mutex.Unlock()
  55. for _, i := range r.metrics {
  56. if h, ok := i.(Healthcheck); ok {
  57. h.Check()
  58. }
  59. }
  60. }
  61. // Unregister the metric with the given name.
  62. func (r *StandardRegistry) Unregister(name string) {
  63. r.mutex.Lock()
  64. defer r.mutex.Unlock()
  65. delete(r.metrics, name)
  66. }
  67. func (r *StandardRegistry) registered() map[string]interface{} {
  68. metrics := make(map[string]interface{}, len(r.metrics))
  69. r.mutex.Lock()
  70. defer r.mutex.Unlock()
  71. for name, i := range r.metrics {
  72. metrics[name] = i
  73. }
  74. return metrics
  75. }
  76. var DefaultRegistry Registry = NewRegistry()
  77. // Call the given function for each registered metric.
  78. func Each(f func(string, interface{})) {
  79. DefaultRegistry.Each(f)
  80. }
  81. // Get the metric by the given name or nil if none is registered.
  82. func Get(name string) interface{} {
  83. return DefaultRegistry.Get(name)
  84. }
  85. // Register the given metric under the given name.
  86. func Register(name string, i interface{}) {
  87. DefaultRegistry.Register(name, i)
  88. }
  89. // Run all registered healthchecks.
  90. func RunHealthchecks() {
  91. DefaultRegistry.RunHealthchecks()
  92. }
  93. // Unregister the metric with the given name.
  94. func Unregister(name string) {
  95. DefaultRegistry.Unregister(name)
  96. }