-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
297 lines (255 loc) · 7.86 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kafka"
"github.com/aws/aws-sdk-go-v2/service/kafka/types"
"gopkg.in/yaml.v2"
)
const (
jmxExporterPort = 11001
nodeExporterPort = 11002
)
type tags map[string]string
var (
outFile = flag.String("output", "msk_file_sd.yml", "path of the file to write MSK discovery information to")
interval = flag.Duration("scrape-interval", 5*time.Minute, "interval at which to scrape the AWS API for MSK cluster information when in file_sd mode")
jobPrefix = flag.String("job-prefix", "msk", "string with which to prefix each job label")
clusterFilter = flag.String("filter", "", "a regex pattern to filter cluster names from the results")
awsRegion = flag.String("region", "", "the aws region in which to scan for MSK clusters")
httpSDEnabled = flag.Bool("http-sd", false, "expose http_sd interface rather than writing a file")
listenAddress = flag.String("listen-address", ":8080", "Address to listen on for http service discovery")
)
type kafkaClient interface {
ListClusters(ctx context.Context, params *kafka.ListClustersInput, optFns ...func(*kafka.Options)) (*kafka.ListClustersOutput, error)
ListNodes(ctx context.Context, params *kafka.ListNodesInput, optFns ...func(*kafka.Options)) (*kafka.ListNodesOutput, error)
}
type labels struct {
Job string `yaml:"job" json:"job"`
ClusterName string `yaml:"cluster_name" json:"cluster_name"`
ClusterArn string `yaml:"cluster_arn" json:"cluster_arn"`
}
// PrometheusStaticConfig is the final structure of a single static config that
// will be outputted to the Prometheus file/http service discovery config
type PrometheusStaticConfig struct {
Targets []string `yaml:"targets" json:"targets"`
Labels labels `yaml:"labels" json:"labels"`
}
// clusterDetails holds details of cluster, each broker, and which OpenMetrics endpoints are enabled
type clusterDetails struct {
ClusterName string
ClusterArn string
Brokers []string
JmxExporter bool
NodeExporter bool
}
type Filter struct {
NameFilter regexp.Regexp
TagFilter tags
}
func (i *tags) String() string {
return fmt.Sprint(*i)
}
func (i *tags) Set(value string) error {
split := strings.Split(value, "=")
(*i)[split[0]] = split[1]
return nil
}
// (ClusterDetails).StaticConfig generates a PrometheusStaticConfig based on the cluster's details
func (c clusterDetails) StaticConfig() PrometheusStaticConfig {
ret := PrometheusStaticConfig{}
ret.Labels = labels{
Job: strings.Join([]string{*jobPrefix, c.ClusterName}, "-"),
ClusterName: c.ClusterName,
ClusterArn: c.ClusterArn,
}
var targets []string
for _, b := range c.Brokers {
if c.JmxExporter {
targets = append(targets, fmt.Sprintf("%s:%d", b, jmxExporterPort))
}
if c.NodeExporter {
targets = append(targets, fmt.Sprintf("%s:%d", b, nodeExporterPort))
}
}
ret.Targets = targets
return ret
}
// getClusters returns a ListClusterOutput of MSK cluster details
func getClusters(svc kafkaClient) (*kafka.ListClustersOutput, error) {
input := &kafka.ListClustersInput{}
output := &kafka.ListClustersOutput{}
p := kafka.NewListClustersPaginator(svc, input)
for p.HasMorePages() {
page, err := p.NextPage(context.TODO())
if err != nil {
return &kafka.ListClustersOutput{}, err
}
output.ClusterInfoList = append(output.ClusterInfoList, page.ClusterInfoList...)
}
return output, nil
}
// getBrokers returns a slice of broker hosts without ports
func getBrokers(svc kafkaClient, arn string) ([]string, error) {
input := kafka.ListNodesInput{ClusterArn: &arn}
var brokers []string
p := kafka.NewListNodesPaginator(svc, &input)
for p.HasMorePages() {
page, err := p.NextPage(context.Background())
if err != nil {
return nil, err
}
for _, b := range page.NodeInfoList {
brokers = append(brokers, b.BrokerNodeInfo.Endpoints...)
}
}
return brokers, nil
}
// buildClusterDetails extracts the relevant details from a ClusterInfo and returns a ClusterDetails
func buildClusterDetails(svc kafkaClient, c types.ClusterInfo) (clusterDetails, error) {
brokers, err := getBrokers(svc, *c.ClusterArn)
if err != nil {
fmt.Println(err)
return clusterDetails{}, err
}
cluster := clusterDetails{
ClusterName: *c.ClusterName,
ClusterArn: *c.ClusterArn,
Brokers: brokers,
JmxExporter: c.OpenMonitoring.Prometheus.JmxExporter.EnabledInBroker,
NodeExporter: c.OpenMonitoring.Prometheus.NodeExporter.EnabledInBroker,
}
return cluster, nil
}
func filterClusters(clusters kafka.ListClustersOutput, filter Filter) *kafka.ListClustersOutput {
var filteredClusters []types.ClusterInfo
var tagMatch bool
for _, cluster := range clusters.ClusterInfoList {
if len(filter.TagFilter) == 0 {
tagMatch = true
} else {
tagMatch = false
}
for tagKey, tagValue := range filter.TagFilter {
if cluster.Tags[tagKey] == tagValue {
tagMatch = true
break
}
}
if filter.NameFilter.MatchString(*cluster.ClusterName) && tagMatch {
filteredClusters = append(filteredClusters, cluster)
}
}
return &kafka.ListClustersOutput{ClusterInfoList: filteredClusters}
}
// GetStaticConfigs pulls a list of MSK clusters and brokers and returns a slice of PrometheusStaticConfigs
func GetStaticConfigs(svc kafkaClient, opt_filter ...Filter) ([]PrometheusStaticConfig, error) {
clusters, err := getClusters(svc)
if err != nil {
return []PrometheusStaticConfig{}, err
}
staticConfigs := []PrometheusStaticConfig{}
// Assign a default Filter, if none is passed.
defaultNameRegex, _ := regexp.Compile(``)
filter := Filter{
NameFilter: *defaultNameRegex,
}
if len(opt_filter) > 0 {
filter = opt_filter[0]
}
clusters = filterClusters(*clusters, filter)
for _, cluster := range clusters.ClusterInfoList {
clusterDetails, err := buildClusterDetails(svc, cluster)
if err != nil {
return []PrometheusStaticConfig{}, err
}
if !clusterDetails.JmxExporter && !clusterDetails.NodeExporter {
continue
}
staticConfigs = append(staticConfigs, clusterDetails.StaticConfig())
}
return staticConfigs, nil
}
func fileSD(client *kafka.Client, filter Filter) {
work := func() {
staticConfigs, err := GetStaticConfigs(client, filter)
if err != nil {
fmt.Println(err)
return
}
m, err := yaml.Marshal(staticConfigs)
if err != nil {
fmt.Println(err)
return
}
log.Printf("Writing %d discovered exporters to %s", len(staticConfigs), *outFile)
err = ioutil.WriteFile(*outFile, m, 0644)
if err != nil {
fmt.Println(err)
return
}
}
s := time.NewTimer(1 * time.Millisecond)
t := time.NewTicker(*interval)
for {
select {
case <-s.C:
case <-t.C:
}
work()
}
}
func httpSD(client *kafka.Client, filter Filter) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
staticConfigs, err := GetStaticConfigs(client, filter)
if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", 500)
return
}
m, err := json.Marshal(staticConfigs)
if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(m)
return
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func main() {
var tagFilters tags = make(tags)
flag.Var(&tagFilters, "tag", "A key=value for filtering by tags. Flag can be specified multiple times, resulting OR expression.")
flag.Parse()
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(*awsRegion), config.WithEC2IMDSRegion())
if err != nil {
fmt.Println(err)
return
}
client := kafka.NewFromConfig(cfg)
regexpFilter, err := regexp.Compile(*clusterFilter)
if err != nil {
fmt.Println(err)
return
}
filter := Filter{
NameFilter: *regexpFilter,
TagFilter: tagFilters,
}
if *httpSDEnabled {
httpSD(client, filter)
} else {
fileSD(client, filter)
}
}