Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1 | package sarama |
| 2 | |
| 3 | import ( |
| 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 |
| 14 | const ( |
| 15 | metricsReservoirSize = 1028 |
| 16 | metricsAlphaFactor = 0.015 |
| 17 | ) |
| 18 | |
| 19 | func 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 | |
| 25 | func 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 Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 31 | func 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 | |
| 37 | func getOrRegisterTopicMeter(name string, topic string, r metrics.Registry) metrics.Meter { |
| 38 | return metrics.GetOrRegisterMeter(getMetricNameForTopic(name, topic), r) |
| 39 | } |
| 40 | |
| 41 | func getOrRegisterTopicHistogram(name string, topic string, r metrics.Registry) metrics.Histogram { |
| 42 | return getOrRegisterHistogram(getMetricNameForTopic(name, topic), r) |
| 43 | } |