-
Notifications
You must be signed in to change notification settings - Fork 137
/
config.go
203 lines (171 loc) · 5.24 KB
/
config.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
// Copyright 2018 Telefónica
//
// 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://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 main
import (
"fmt"
"os"
"strings"
"text/template"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"gopkg.in/yaml.v2"
"github.com/sirupsen/logrus"
)
var (
kafkaBrokerList = "kafka:9092"
kafkaTopic = "metrics"
topicTemplate *template.Template
match = make(map[string]*dto.MetricFamily, 0)
basicauth = false
basicauthUsername = ""
basicauthPassword = ""
kafkaCompression = "none"
kafkaBatchNumMessages = "10000"
kafkaSslClientCertFile = ""
kafkaSslClientKeyFile = ""
kafkaSslClientKeyPass = ""
kafkaSslCACertFile = ""
kafkaSecurityProtocol = ""
kafkaSaslMechanism = ""
kafkaSaslUsername = ""
kafkaSaslPassword = ""
serializer Serializer
kafkaAcks = "all"
)
func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetOutput(os.Stdout)
if value := os.Getenv("LOG_LEVEL"); value != "" {
logrus.SetLevel(parseLogLevel(value))
}
if value := os.Getenv("KAFKA_BROKER_LIST"); value != "" {
kafkaBrokerList = value
}
if value := os.Getenv("KAFKA_TOPIC"); value != "" {
kafkaTopic = value
}
if value := os.Getenv("BASIC_AUTH_USERNAME"); value != "" {
basicauth = true
basicauthUsername = value
}
if value := os.Getenv("BASIC_AUTH_PASSWORD"); value != "" {
basicauthPassword = value
}
if value := os.Getenv("KAFKA_COMPRESSION"); value != "" {
kafkaCompression = value
}
if value := os.Getenv("KAFKA_BATCH_NUM_MESSAGES"); value != "" {
kafkaBatchNumMessages = value
}
if value := os.Getenv("KAFKA_SSL_CLIENT_CERT_FILE"); value != "" {
kafkaSslClientCertFile = value
}
if value := os.Getenv("KAFKA_SSL_CLIENT_KEY_FILE"); value != "" {
kafkaSslClientKeyFile = value
}
if value := os.Getenv("KAFKA_SSL_CLIENT_KEY_PASS"); value != "" {
kafkaSslClientKeyPass = value
}
if value := os.Getenv("KAFKA_SSL_CA_CERT_FILE"); value != "" {
kafkaSslCACertFile = value
}
if value := os.Getenv("KAFKA_SECURITY_PROTOCOL"); value != "" {
kafkaSecurityProtocol = strings.ToLower(value)
}
if value := os.Getenv("KAFKA_SASL_MECHANISM"); value != "" {
kafkaSaslMechanism = value
}
if value := os.Getenv("KAFKA_SASL_USERNAME"); value != "" {
kafkaSaslUsername = value
}
if value := os.Getenv("KAFKA_SASL_PASSWORD"); value != "" {
kafkaSaslPassword = value
}
if value := os.Getenv("KAFKA_ACKS"); value != "" {
kafkaAcks = value
}
if value := os.Getenv("MATCH"); value != "" {
matchList, err := parseMatchList(value)
if err != nil {
logrus.WithError(err).Fatalln("couldn't parse the match rules")
}
match = matchList
}
var err error
serializer, err = parseSerializationFormat(os.Getenv("SERIALIZATION_FORMAT"))
if err != nil {
logrus.WithError(err).Fatalln("couldn't create a metrics serializer")
}
topicTemplate, err = parseTopicTemplate(kafkaTopic)
if err != nil {
logrus.WithError(err).Fatalln("couldn't parse the topic template")
}
}
func parseMatchList(text string) (map[string]*dto.MetricFamily, error) {
var matchRules []string
err := yaml.Unmarshal([]byte(text), &matchRules)
if err != nil {
return nil, err
}
var metricsList []string
for _, v := range matchRules {
metricsList = append(metricsList, fmt.Sprintf("%s 0\n", v))
}
metricsText := strings.Join(metricsList, "")
var parser expfmt.TextParser
metricFamilies, err := parser.TextToMetricFamilies(strings.NewReader(metricsText))
if err != nil {
return nil, fmt.Errorf("couldn't parse match rules: %s", err)
}
return metricFamilies, nil
}
func parseLogLevel(value string) logrus.Level {
level, err := logrus.ParseLevel(value)
if err != nil {
logrus.WithField("log-level-value", value).Warningln("invalid log level from env var, using info")
return logrus.InfoLevel
}
return level
}
func parseSerializationFormat(value string) (Serializer, error) {
switch value {
case "json":
return NewJSONSerializer()
case "avro-json":
return NewAvroJSONSerializer("schemas/metric.avsc")
default:
logrus.WithField("serialization-format-value", value).Warningln("invalid serialization format, using json")
return NewJSONSerializer()
}
}
func parseTopicTemplate(tpl string) (*template.Template, error) {
funcMap := template.FuncMap{
"replace": func(old, new, src string) string {
return strings.Replace(src, old, new, -1)
},
"substring": func(start, end int, s string) string {
if start < 0 {
start = 0
}
if end < 0 || end > len(s) {
end = len(s)
}
if start >= end {
panic("template function - substring: start is bigger (or equal) than end. That will produce an empty string.")
}
return s[start:end]
},
}
return template.New("topic").Funcs(funcMap).Parse(tpl)
}