This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
AppConfig.scala
235 lines (213 loc) · 7.45 KB
/
AppConfig.scala
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
/*
* Copyright (C) 2018-2022 Lightbend Inc. <https://www.lightbend.com>
* Copyright (C) 2022 Sean Glover <https://seanglover.com>
*/
package com.lightbend.kafkalagexporter
import java.util
import com.lightbend.kafkalagexporter.EndpointSink.ClusterGlobalLabels
import com.typesafe.config.{Config, ConfigObject}
import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.compat.java8.DurationConverters._
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.util.Try
object AppConfig {
def apply(config: Config): AppConfig = {
val c = config.getConfig("kafka-lag-exporter")
val pollInterval = c.getDuration("poll-interval").toScala
val metricWhitelist = c.getStringList("metric-whitelist").asScala.toList
val sinks =
if (c.hasPath("sinks"))
c.getStringList("sinks").asScala.toList
else
List("PrometheusEndpointSink")
val sinkConfigs: List[SinkConfig] = sinks.flatMap { sink =>
sink match {
case "PrometheusEndpointSink" =>
Some(new PrometheusEndpointSinkConfig(sink, metricWhitelist, c))
case "InfluxDBPusherSink" =>
Some(new InfluxDBPusherSinkConfig(sink, metricWhitelist, c))
case "GraphiteEndpointSink" =>
Some(new GraphiteEndpointConfig(sink, metricWhitelist, c))
case _ => None
}
}
val lookupTable = LookupTableConfig(c)
val clientGroupId = c.getString("client-group-id")
val kafkaClientTimeout = c.getDuration("kafka-client-timeout").toScala
val kafkaRetries = c.getInt("kafka-retries")
val clusters =
c.getConfigList("clusters").asScala.toList.map { clusterConfig =>
val consumerProperties =
if (clusterConfig.hasPath("consumer-properties"))
parseKafkaClientsProperties(
clusterConfig.getConfig("consumer-properties")
)
else
Map.empty[String, String]
val adminClientProperties =
if (clusterConfig.hasPath("admin-client-properties"))
parseKafkaClientsProperties(
clusterConfig.getConfig("admin-client-properties")
)
else
Map.empty[String, String]
val labels =
Try {
val labels = clusterConfig.getConfig("labels")
labels
.entrySet()
.asScala
.map(entry => (entry.getKey, entry.getValue.unwrapped().toString))
.toMap
}.getOrElse(Map.empty[String, String])
val groupWhitelist =
if (clusterConfig.hasPath("group-whitelist"))
clusterConfig.getStringList("group-whitelist").asScala.toList
else KafkaCluster.GroupWhitelistDefault
val groupBlacklist =
if (clusterConfig.hasPath("group-blacklist"))
clusterConfig.getStringList("group-blacklist").asScala.toList
else KafkaCluster.GroupBlacklistDefault
val topicWhitelist =
if (clusterConfig.hasPath("topic-whitelist"))
clusterConfig.getStringList("topic-whitelist").asScala.toList
else KafkaCluster.TopicWhitelistDefault
val topicBlacklist =
if (clusterConfig.hasPath("topic-blacklist"))
clusterConfig.getStringList("topic-blacklist").asScala.toList
else KafkaCluster.TopicBlacklistDefault
KafkaCluster(
clusterConfig.getString("name"),
clusterConfig.getString("bootstrap-brokers"),
groupWhitelist,
groupBlacklist,
topicWhitelist,
topicBlacklist,
consumerProperties,
adminClientProperties,
labels
)
}
val strimziWatcher = c.getString("watchers.strimzi").toBoolean
AppConfig(
pollInterval,
lookupTable,
sinkConfigs,
clientGroupId,
kafkaClientTimeout,
kafkaRetries,
clusters,
strimziWatcher
)
}
// Copied from Alpakka Kafka
// https://github.com/akka/alpakka-kafka/blob/v1.0.5/core/src/main/scala/akka/kafka/internal/ConfigSettings.scala
def parseKafkaClientsProperties(config: Config): Map[String, String] = {
@tailrec
def collectKeys(
c: ConfigObject,
processedKeys: Set[String],
unprocessedKeys: List[String]
): Set[String] =
if (unprocessedKeys.isEmpty) processedKeys
else {
c.toConfig.getAnyRef(unprocessedKeys.head) match {
case o: util.Map[_, _] =>
collectKeys(
c,
processedKeys,
unprocessedKeys.tail ::: o
.keySet()
.asScala
.toList
.map(unprocessedKeys.head + "." + _)
)
case _ =>
collectKeys(
c,
processedKeys + unprocessedKeys.head,
unprocessedKeys.tail
)
}
}
val keys = collectKeys(
config.root,
Set.empty[String],
config.root().keySet().asScala.toList
)
keys.map(key => key -> config.getString(key)).toMap
}
def getPotentiallyInfiniteDuration(
underlying: Config,
path: String
): Duration = underlying.getString(path) match {
case "infinite" => Duration.Inf
case _ => underlying.getDuration(path).toScala
}
}
object KafkaCluster {
val GroupWhitelistDefault = List(".*")
val GroupBlacklistDefault = List.empty[String]
val TopicWhitelistDefault = List(".*")
val TopicBlacklistDefault = List.empty[String]
}
final case class KafkaCluster(
name: String,
bootstrapBrokers: String,
groupWhitelist: List[String] = KafkaCluster.GroupWhitelistDefault,
groupBlacklist: List[String] = KafkaCluster.GroupBlacklistDefault,
topicWhitelist: List[String] = KafkaCluster.TopicWhitelistDefault,
topicBlacklist: List[String] = KafkaCluster.TopicBlacklistDefault,
consumerProperties: Map[String, String] = Map.empty,
adminClientProperties: Map[String, String] = Map.empty,
labels: Map[String, String] = Map.empty
) {
override def toString(): String = {
s"""
| Cluster name: $name
| Cluster Kafka bootstrap brokers: $bootstrapBrokers
| Consumer group whitelist: [${groupWhitelist.mkString(", ")}]
| Consumer group blacklist: [${groupBlacklist.mkString(", ")}]
| Topic whitelist: [${topicWhitelist.mkString(", ")}]
| Topic blacklist: [${topicBlacklist.mkString(", ")}]
""".stripMargin
}
}
final case class AppConfig(
pollInterval: FiniteDuration,
lookupTable: LookupTableConfig,
sinkConfigs: List[SinkConfig],
clientGroupId: String,
clientTimeout: FiniteDuration,
retries: Int,
clusters: List[KafkaCluster],
strimziWatcher: Boolean
) {
override def toString(): String = {
val clusterString =
if (clusters.isEmpty)
" (none)"
else clusters.map(_.toString).mkString("\n")
val sinksString = sinkConfigs.mkString("")
val lookupTableString = lookupTable.toString()
s"""
|Poll interval: $pollInterval
|$lookupTableString
|Metrics whitelist: [${sinkConfigs.head.metricWhitelist.mkString(", ")}]
|Admin client consumer group id: $clientGroupId
|Kafka client timeout: $clientTimeout
|Kafka retries: $retries
|$sinksString
|Statically defined Clusters:
|$clusterString
|Watchers:
| Strimzi: $strimziWatcher
""".stripMargin
}
def clustersGlobalLabels(): ClusterGlobalLabels = {
clusters.map { cluster =>
cluster.name -> cluster.labels
}.toMap
}
}