-
Notifications
You must be signed in to change notification settings - Fork 14
/
collector.go
89 lines (74 loc) · 2.51 KB
/
collector.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
package kafka
import (
"github.com/ansrivas/fiberprometheus/v2"
"github.com/gofiber/fiber/v2"
"github.com/prometheus/client_golang/prometheus"
)
const Name = "kafka_konsumer"
type MetricCollector struct {
consumerMetric *ConsumerMetric
totalUnprocessedMessagesCounter *prometheus.Desc
totalProcessedMessagesCounter *prometheus.Desc
totalErrorCountDuringFetchingMessage *prometheus.Desc
}
func NewMetricCollector(metricPrefix string, consumerMetric *ConsumerMetric) *MetricCollector {
if metricPrefix == "" {
metricPrefix = Name
}
return &MetricCollector{
consumerMetric: consumerMetric,
totalProcessedMessagesCounter: prometheus.NewDesc(
prometheus.BuildFQName(metricPrefix, "processed_messages_total", "current"),
"Total number of processed messages.",
emptyStringList,
nil,
),
totalUnprocessedMessagesCounter: prometheus.NewDesc(
prometheus.BuildFQName(metricPrefix, "unprocessed_messages_total", "current"),
"Total number of unprocessed messages.",
emptyStringList,
nil,
),
totalErrorCountDuringFetchingMessage: prometheus.NewDesc(
prometheus.BuildFQName(metricPrefix, "error_count_during_fetching_message_total", "current"),
"Total number of error during fetching message.",
emptyStringList,
nil,
),
}
}
func (s *MetricCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(s, ch)
}
var emptyStringList []string
func (s *MetricCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
s.totalProcessedMessagesCounter,
prometheus.CounterValue,
float64(s.consumerMetric.TotalProcessedMessagesCounter),
emptyStringList...,
)
ch <- prometheus.MustNewConstMetric(
s.totalUnprocessedMessagesCounter,
prometheus.CounterValue,
float64(s.consumerMetric.TotalUnprocessedMessagesCounter),
emptyStringList...,
)
ch <- prometheus.MustNewConstMetric(
s.totalErrorCountDuringFetchingMessage,
prometheus.CounterValue,
float64(s.consumerMetric.TotalErrorCountDuringFetchingMessage),
emptyStringList...,
)
}
func NewMetricMiddleware(cfg *ConsumerConfig,
app *fiber.App,
consumerMetric *ConsumerMetric,
metricCollectors ...prometheus.Collector,
) (func(ctx *fiber.Ctx) error, error) {
prometheus.DefaultRegisterer.MustRegister(NewMetricCollector(cfg.MetricPrefix, consumerMetric))
prometheus.DefaultRegisterer.MustRegister(metricCollectors...)
fiberPrometheus := fiberprometheus.New(cfg.Reader.GroupID)
fiberPrometheus.RegisterAt(app, *cfg.MetricConfiguration.Path)
return fiberPrometheus.Middleware, nil
}