metrics.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package sarama
  2. import (
  3. "fmt"
  4. "strings"
  5. "sync"
  6. "github.com/rcrowley/go-metrics"
  7. )
  8. // Use exponentially decaying reservoir for sampling histograms with the same defaults as the Java library:
  9. // 1028 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution,
  10. // and an alpha factor of 0.015, which heavily biases the reservoir to the past 5 minutes of measurements.
  11. // See https://github.com/dropwizard/metrics/blob/v3.1.0/metrics-core/src/main/java/com/codahale/metrics/ExponentiallyDecayingReservoir.java#L38
  12. const (
  13. metricsReservoirSize = 1028
  14. metricsAlphaFactor = 0.015
  15. )
  16. func getOrRegisterHistogram(name string, r metrics.Registry) metrics.Histogram {
  17. return r.GetOrRegister(name, func() metrics.Histogram {
  18. return metrics.NewHistogram(metrics.NewExpDecaySample(metricsReservoirSize, metricsAlphaFactor))
  19. }).(metrics.Histogram)
  20. }
  21. func getMetricNameForBroker(name string, broker *Broker) string {
  22. // Use broker id like the Java client as it does not contain '.' or ':' characters that
  23. // can be interpreted as special character by monitoring tool (e.g. Graphite)
  24. return fmt.Sprintf(name+"-for-broker-%d", broker.ID())
  25. }
  26. func getMetricNameForTopic(name string, topic string) string {
  27. // Convert dot to _ since reporters like Graphite typically use dot to represent hierarchy
  28. // cf. KAFKA-1902 and KAFKA-2337
  29. return fmt.Sprintf(name+"-for-topic-%s", strings.ReplaceAll(topic, ".", "_"))
  30. }
  31. func getOrRegisterTopicMeter(name string, topic string, r metrics.Registry) metrics.Meter {
  32. return metrics.GetOrRegisterMeter(getMetricNameForTopic(name, topic), r)
  33. }
  34. func getOrRegisterTopicHistogram(name string, topic string, r metrics.Registry) metrics.Histogram {
  35. return getOrRegisterHistogram(getMetricNameForTopic(name, topic), r)
  36. }
  37. // cleanupRegistry is an implementation of metrics.Registry that allows
  38. // to unregister from the parent registry only those metrics
  39. // that have been registered in cleanupRegistry
  40. type cleanupRegistry struct {
  41. parent metrics.Registry
  42. metrics map[string]struct{}
  43. mutex sync.RWMutex
  44. }
  45. func newCleanupRegistry(parent metrics.Registry) metrics.Registry {
  46. return &cleanupRegistry{
  47. parent: parent,
  48. metrics: map[string]struct{}{},
  49. }
  50. }
  51. func (r *cleanupRegistry) Each(fn func(string, interface{})) {
  52. r.mutex.RLock()
  53. defer r.mutex.RUnlock()
  54. wrappedFn := func(name string, iface interface{}) {
  55. if _, ok := r.metrics[name]; ok {
  56. fn(name, iface)
  57. }
  58. }
  59. r.parent.Each(wrappedFn)
  60. }
  61. func (r *cleanupRegistry) Get(name string) interface{} {
  62. r.mutex.RLock()
  63. defer r.mutex.RUnlock()
  64. if _, ok := r.metrics[name]; ok {
  65. return r.parent.Get(name)
  66. }
  67. return nil
  68. }
  69. func (r *cleanupRegistry) GetOrRegister(name string, metric interface{}) interface{} {
  70. r.mutex.Lock()
  71. defer r.mutex.Unlock()
  72. r.metrics[name] = struct{}{}
  73. return r.parent.GetOrRegister(name, metric)
  74. }
  75. func (r *cleanupRegistry) Register(name string, metric interface{}) error {
  76. r.mutex.Lock()
  77. defer r.mutex.Unlock()
  78. r.metrics[name] = struct{}{}
  79. return r.parent.Register(name, metric)
  80. }
  81. func (r *cleanupRegistry) RunHealthchecks() {
  82. r.parent.RunHealthchecks()
  83. }
  84. func (r *cleanupRegistry) GetAll() map[string]map[string]interface{} {
  85. return r.parent.GetAll()
  86. }
  87. func (r *cleanupRegistry) Unregister(name string) {
  88. r.mutex.Lock()
  89. defer r.mutex.Unlock()
  90. if _, ok := r.metrics[name]; ok {
  91. delete(r.metrics, name)
  92. r.parent.Unregister(name)
  93. }
  94. }
  95. func (r *cleanupRegistry) UnregisterAll() {
  96. r.mutex.Lock()
  97. defer r.mutex.Unlock()
  98. for name := range r.metrics {
  99. delete(r.metrics, name)
  100. r.parent.Unregister(name)
  101. }
  102. }