metrics.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package sarama
  2. import (
  3. "fmt"
  4. "github.com/rcrowley/go-metrics"
  5. )
  6. // Use exponentially decaying reservoir for sampling histograms with the same defaults as the Java library:
  7. // 1028 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution,
  8. // and an alpha factor of 0.015, which heavily biases the reservoir to the past 5 minutes of measurements.
  9. // See https://github.com/dropwizard/metrics/blob/v3.1.0/metrics-core/src/main/java/com/codahale/metrics/ExponentiallyDecayingReservoir.java#L38
  10. const (
  11. metricsReservoirSize = 1028
  12. metricsAlphaFactor = 0.015
  13. )
  14. func getOrRegisterHistogram(name string, r metrics.Registry) metrics.Histogram {
  15. return r.GetOrRegister(name, func() metrics.Histogram {
  16. return metrics.NewHistogram(metrics.NewExpDecaySample(metricsReservoirSize, metricsAlphaFactor))
  17. }).(metrics.Histogram)
  18. }
  19. func getMetricNameForBroker(name string, broker *Broker) string {
  20. // Use broker id like the Java client as it does not contain '.' or ':' characters that
  21. // can be interpreted as special character by monitoring tool (e.g. Graphite)
  22. return fmt.Sprintf(name+"-for-broker-%d", broker.ID())
  23. }
  24. func getOrRegisterBrokerMeter(name string, broker *Broker, r metrics.Registry) metrics.Meter {
  25. return metrics.GetOrRegisterMeter(getMetricNameForBroker(name, broker), r)
  26. }
  27. func getOrRegisterBrokerHistogram(name string, broker *Broker, r metrics.Registry) metrics.Histogram {
  28. return getOrRegisterHistogram(getMetricNameForBroker(name, broker), r)
  29. }