forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic_scraper.go
136 lines (121 loc) · 4.17 KB
/
topic_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
// Copyright OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kafkametricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkametricsreceiver"
import (
"context"
"fmt"
"regexp"
"time"
"github.com/Shopify/sarama"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/scrapererror"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkametricsreceiver/internal/metadata"
)
type topicScraper struct {
client sarama.Client
settings component.ReceiverCreateSettings
topicFilter *regexp.Regexp
saramaConfig *sarama.Config
config Config
mb *metadata.MetricsBuilder
}
func (s *topicScraper) Name() string {
return topicsScraperName
}
func (s *topicScraper) shutdown(context.Context) error {
if s.client != nil && !s.client.Closed() {
return s.client.Close()
}
return nil
}
func (s *topicScraper) start(_ context.Context, _ component.Host) error {
s.mb = metadata.NewMetricsBuilder(s.config.Metrics, s.settings.BuildInfo)
return nil
}
func (s *topicScraper) 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 topics scraper: %w", err)
}
s.client = client
}
topics, err := s.client.Topics()
if err != nil {
s.settings.Logger.Error("Error fetching cluster topics ", zap.Error(err))
return pmetric.Metrics{}, err
}
var scrapeErrors = scrapererror.ScrapeErrors{}
now := pcommon.NewTimestampFromTime(time.Now())
for _, topic := range topics {
if !s.topicFilter.MatchString(topic) {
continue
}
partitions, err := s.client.Partitions(topic)
if err != nil {
scrapeErrors.Add(err)
continue
}
s.mb.RecordKafkaTopicPartitionsDataPoint(now, int64(len(partitions)), topic)
for _, partition := range partitions {
currentOffset, err := s.client.GetOffset(topic, partition, sarama.OffsetNewest)
if err != nil {
scrapeErrors.AddPartial(1, err)
} else {
s.mb.RecordKafkaPartitionCurrentOffsetDataPoint(now, currentOffset, topic, int64(partition))
}
oldestOffset, err := s.client.GetOffset(topic, partition, sarama.OffsetOldest)
if err != nil {
scrapeErrors.AddPartial(1, err)
} else {
s.mb.RecordKafkaPartitionOldestOffsetDataPoint(now, oldestOffset, topic, int64(partition))
}
replicas, err := s.client.Replicas(topic, partition)
if err != nil {
scrapeErrors.AddPartial(1, err)
} else {
s.mb.RecordKafkaPartitionReplicasDataPoint(now, int64(len(replicas)), topic, int64(partition))
}
replicasInSync, err := s.client.InSyncReplicas(topic, partition)
if err != nil {
scrapeErrors.AddPartial(1, err)
} else {
s.mb.RecordKafkaPartitionReplicasInSyncDataPoint(now, int64(len(replicasInSync)), topic, int64(partition))
}
}
}
return s.mb.Emit(), scrapeErrors.Combine()
}
func createTopicsScraper(_ context.Context, cfg Config, saramaConfig *sarama.Config, settings component.ReceiverCreateSettings) (scraperhelper.Scraper, error) {
topicFilter, err := regexp.Compile(cfg.TopicMatch)
if err != nil {
return nil, fmt.Errorf("failed to compile topic filter: %w", err)
}
s := topicScraper{
settings: settings,
topicFilter: topicFilter,
saramaConfig: saramaConfig,
config: cfg,
}
return scraperhelper.NewScraper(
s.Name(),
s.scrape,
scraperhelper.WithStart(s.start),
scraperhelper.WithShutdown(s.shutdown),
)
}