registry.go 3.0 KB

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