-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
consumer_scraper.go
189 lines (169 loc) · 5.61 KB
/
consumer_scraper.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package kafkametricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkametricsreceiver"
import (
"context"
"fmt"
"regexp"
"time"
"github.com/IBM/sarama"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkametricsreceiver/internal/metadata"
)
type consumerScraper struct {
client sarama.Client
settings receiver.Settings
groupFilter *regexp.Regexp
topicFilter *regexp.Regexp
clusterAdmin sarama.ClusterAdmin
saramaConfig *sarama.Config
config Config
mb *metadata.MetricsBuilder
}
func (s *consumerScraper) start(_ context.Context, _ component.Host) error {
s.mb = metadata.NewMetricsBuilder(s.config.MetricsBuilderConfig, s.settings)
return nil
}
func (s *consumerScraper) shutdown(_ context.Context) error {
if s.client != nil && !s.client.Closed() {
return s.client.Close()
}
return nil
}
func (s *consumerScraper) scrape(context.Context) (pmetric.Metrics, error) {
if s.client == nil {
client, err := newSaramaClient(s.config.Brokers, s.saramaConfig)
if err != nil {
return pmetric.Metrics{}, fmt.Errorf("failed to create client in consumer scraper: %w", err)
}
clusterAdmin, err := newClusterAdmin(s.config.Brokers, s.saramaConfig)
if err != nil {
if client != nil {
_ = client.Close()
}
return pmetric.Metrics{}, fmt.Errorf("failed to create cluster admin in consumer scraper: %w", err)
}
s.client = client
s.clusterAdmin = clusterAdmin
}
cgs, listErr := s.clusterAdmin.ListConsumerGroups()
if listErr != nil {
return pmetric.Metrics{}, listErr
}
var matchedGrpIDs []string
for grpID := range cgs {
if s.groupFilter.MatchString(grpID) {
matchedGrpIDs = append(matchedGrpIDs, grpID)
}
}
allTopics, listErr := s.clusterAdmin.ListTopics()
if listErr != nil {
return pmetric.Metrics{}, listErr
}
matchedTopics := map[string]sarama.TopicDetail{}
for t, d := range allTopics {
if s.topicFilter.MatchString(t) {
matchedTopics[t] = d
}
}
var scrapeError error
// partitionIds in matchedTopics
topicPartitions := map[string][]int32{}
// currentOffset for each partition in matchedTopics
topicPartitionOffset := map[string]map[int32]int64{}
for topic := range matchedTopics {
topicPartitionOffset[topic] = map[int32]int64{}
partitions, err := s.client.Partitions(topic)
if err != nil {
scrapeError = multierr.Append(scrapeError, err)
continue
}
for _, p := range partitions {
var offset int64
offset, err = s.client.GetOffset(topic, p, sarama.OffsetNewest)
if err != nil {
scrapeError = multierr.Append(scrapeError, err)
continue
}
topicPartitions[topic] = append(topicPartitions[topic], p)
topicPartitionOffset[topic][p] = offset
}
}
consumerGroups, listErr := s.clusterAdmin.DescribeConsumerGroups(matchedGrpIDs)
if listErr != nil {
return pmetric.Metrics{}, listErr
}
now := pcommon.NewTimestampFromTime(time.Now())
for _, group := range consumerGroups {
s.mb.RecordKafkaConsumerGroupMembersDataPoint(now, int64(len(group.Members)), group.GroupId)
groupOffsetFetchResponse, err := s.clusterAdmin.ListConsumerGroupOffsets(group.GroupId, topicPartitions)
if err != nil {
scrapeError = multierr.Append(scrapeError, err)
continue
}
for topic, partitions := range groupOffsetFetchResponse.Blocks {
// tracking matchedTopics consumed by this group
// by checking if any of the blocks has an offset
isConsumed := false
for _, block := range partitions {
if block.Offset != -1 {
isConsumed = true
break
}
}
if isConsumed {
var lagSum int64
var offsetSum int64
for partition, block := range partitions {
consumerOffset := block.Offset
offsetSum += consumerOffset
s.mb.RecordKafkaConsumerGroupOffsetDataPoint(now, consumerOffset, group.GroupId, topic, int64(partition))
// default -1 to indicate no lag measured.
var consumerLag int64 = -1
if partitionOffset, ok := topicPartitionOffset[topic][partition]; ok {
// only consider partitions with an offset
if block.Offset != -1 {
consumerLag = partitionOffset - consumerOffset
lagSum += consumerLag
}
}
s.mb.RecordKafkaConsumerGroupLagDataPoint(now, consumerLag, group.GroupId, topic, int64(partition))
}
s.mb.RecordKafkaConsumerGroupOffsetSumDataPoint(now, offsetSum, group.GroupId, topic)
s.mb.RecordKafkaConsumerGroupLagSumDataPoint(now, lagSum, group.GroupId, topic)
}
}
}
rb := s.mb.NewResourceBuilder()
rb.SetKafkaClusterAlias(s.config.ClusterAlias)
return s.mb.Emit(metadata.WithResource(rb.Emit())), scrapeError
}
func createConsumerScraper(_ context.Context, cfg Config, saramaConfig *sarama.Config,
settings receiver.Settings) (scraperhelper.Scraper, error) {
groupFilter, err := regexp.Compile(cfg.GroupMatch)
if err != nil {
return nil, fmt.Errorf("failed to compile group_match: %w", err)
}
topicFilter, err := regexp.Compile(cfg.TopicMatch)
if err != nil {
return nil, fmt.Errorf("failed to compile topic filter: %w", err)
}
s := consumerScraper{
settings: settings,
groupFilter: groupFilter,
topicFilter: topicFilter,
config: cfg,
saramaConfig: saramaConfig,
}
return scraperhelper.NewScraper(
consumersScraperType,
s.scrape,
scraperhelper.WithStart(s.start),
scraperhelper.WithShutdown(s.shutdown),
)
}