forked from Telefonica/prometheus-kafka-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
105 lines (87 loc) · 2.64 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
// 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 (
"os"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/sirupsen/logrus"
)
var (
kafkaBrokerList = "kafka:9092"
kafkaTopic = "metrics"
basicauth = false
basicauthUsername = ""
basicauthPassword = ""
kafkaPartition = kafka.TopicPartition{
Topic: &kafkaTopic,
Partition: kafka.PartitionAny,
}
kafkaCompression = "none"
kafkaBatchNumMessages = "10000"
serializer Serializer
)
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
kafkaPartition = kafka.TopicPartition{
Topic: &kafkaTopic,
Partition: kafka.PartitionAny,
}
}
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
}
var err error
serializer, err = parseSerializationFormat(os.Getenv("SERIALIZATION_FORMAT"))
if err != nil {
logrus.WithError(err).Fatalln("couldn't create a metrics serializer")
}
}
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()
}
}