blob: 90e5a87f4972662f48078ae30f6b13fc206300ee [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -07001package sarama
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/rcrowley/go-metrics"
8)
9
10// Use exponentially decaying reservoir for sampling histograms with the same defaults as the Java library:
11// 1028 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution,
12// and an alpha factor of 0.015, which heavily biases the reservoir to the past 5 minutes of measurements.
13// See https://github.com/dropwizard/metrics/blob/v3.1.0/metrics-core/src/main/java/com/codahale/metrics/ExponentiallyDecayingReservoir.java#L38
14const (
15 metricsReservoirSize = 1028
16 metricsAlphaFactor = 0.015
17)
18
19func getOrRegisterHistogram(name string, r metrics.Registry) metrics.Histogram {
20 return r.GetOrRegister(name, func() metrics.Histogram {
21 return metrics.NewHistogram(metrics.NewExpDecaySample(metricsReservoirSize, metricsAlphaFactor))
22 }).(metrics.Histogram)
23}
24
25func getMetricNameForBroker(name string, broker *Broker) string {
26 // Use broker id like the Java client as it does not contain '.' or ':' characters that
27 // can be interpreted as special character by monitoring tool (e.g. Graphite)
28 return fmt.Sprintf(name+"-for-broker-%d", broker.ID())
29}
30
Scott Baker2d897982019-09-24 11:50:08 -070031func getMetricNameForTopic(name string, topic string) string {
32 // Convert dot to _ since reporters like Graphite typically use dot to represent hierarchy
33 // cf. KAFKA-1902 and KAFKA-2337
34 return fmt.Sprintf(name+"-for-topic-%s", strings.Replace(topic, ".", "_", -1))
35}
36
37func getOrRegisterTopicMeter(name string, topic string, r metrics.Registry) metrics.Meter {
38 return metrics.GetOrRegisterMeter(getMetricNameForTopic(name, topic), r)
39}
40
41func getOrRegisterTopicHistogram(name string, topic string, r metrics.Registry) metrics.Histogram {
42 return getOrRegisterHistogram(getMetricNameForTopic(name, topic), r)
43}