registry.go 3.0 KB

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