/
metrics.go
104 lines (90 loc) · 2.92 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package kafka
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
bytesProcessed *prometheus.CounterVec
messagesProcessed *prometheus.CounterVec
messagesMarked *prometheus.CounterVec
errorsCount *prometheus.CounterVec
offsetMetrics *prometheus.GaugeVec
lagMetrics *prometheus.GaugeVec
adminLatency *prometheus.GaugeVec
adminErr *prometheus.CounterVec
)
const (
adminMethodLabel = "method"
adminErrLabel = "error"
)
func init() {
bytesProcessed = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kafka_message_processed_bytes",
Help: "How many bytes processed, partitioned by topic, type (consumer/writer), groupId, and partition.",
},
[]string{"topic", "type", "group", "partition"},
)
messagesProcessed = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kafka_messages_total",
Help: "How many messages processed, partitioned by topic, type (consumer/writer), groupId, and partition.",
},
[]string{"topic", "type", "group", "partition"},
)
messagesMarked = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kafka_messages_marked",
Help: "How many messages marked, partitioned by topic, type (consumer/writer), groupId, and partition.",
},
[]string{"topic", "type", "group", "partition"},
)
errorsCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kafka_errors_total",
Help: "How many errors encountered so far, partitioned by topic, type (consumer/writer), groupId, and partition.",
},
[]string{"topic", "type", "group", "partition"},
)
offsetMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "kafka_offsets_total",
Help: "Current offset - partitioned by topic, type (consumer/writer), groupId, and partition.",
},
[]string{"topic", "type", "group", "partition"},
)
lagMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "kafka_lags_total",
Help: "Current lag (how far behind) - partitioned by topic, type (comsumer/writer), groupId, and partition.",
},
[]string{"topic", "type", "group", "partition"},
)
adminLatency = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "kafka_admin_latency",
Help: "Admin request latency",
},
[]string{adminMethodLabel},
)
adminErr = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kafka_admin_error",
Help: "Admin error count",
},
[]string{adminMethodLabel, adminErrLabel},
)
prometheus.MustRegister(bytesProcessed, messagesProcessed, messagesMarked, errorsCount, offsetMetrics, lagMetrics, adminLatency, adminErr)
}
// describes metric info to emit
type requestMetric struct {
method string
startTime time.Time
err *error
}
func (metricObj *requestMetric) emit() {
adminLatency.WithLabelValues(metricObj.method).Set(float64(time.Now().Sub(metricObj.startTime) / time.Millisecond))
if *metricObj.err != nil {
adminErr.WithLabelValues(metricObj.method, (*metricObj.err).Error()).Inc()
}
}