registry.go 2.8 KB

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