blob: 4869708e9449a41b511e93151935d5b10285dd1f [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001package 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
31func getOrRegisterBrokerMeter(name string, broker *Broker, r metrics.Registry) metrics.Meter {
32 return metrics.GetOrRegisterMeter(getMetricNameForBroker(name, broker), r)
33}
34
35func getOrRegisterBrokerHistogram(name string, broker *Broker, r metrics.Registry) metrics.Histogram {
36 return getOrRegisterHistogram(getMetricNameForBroker(name, broker), r)
37}
38
39func getMetricNameForTopic(name string, topic string) string {
40 // Convert dot to _ since reporters like Graphite typically use dot to represent hierarchy
41 // cf. KAFKA-1902 and KAFKA-2337
42 return fmt.Sprintf(name+"-for-topic-%s", strings.Replace(topic, ".", "_", -1))
43}
44
45func getOrRegisterTopicMeter(name string, topic string, r metrics.Registry) metrics.Meter {
46 return metrics.GetOrRegisterMeter(getMetricNameForTopic(name, topic), r)
47}
48
49func getOrRegisterTopicHistogram(name string, topic string, r metrics.Registry) metrics.Histogram {
50 return getOrRegisterHistogram(getMetricNameForTopic(name, topic), r)
51}