Skip to content

Commit e426976

Browse files
committed
logging the kafka-cluster name in every log line
Signed-off-by: Laszlo Bende <L.Bende@mwam.com>
1 parent 191ffcd commit e426976

File tree

10 files changed

+41
-22
lines changed

10 files changed

+41
-22
lines changed

buildSrc/src/main/kotlin/buildlogic.kotlin-application-common-conventions.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ val prometheus_version: String by project
99
val koin_version: String by project
1010
val kotlin_kafka_version: String by project
1111
val kafka_version: String by project
12+
val hoplite_version: String by project
1213

1314
plugins {
1415
// Apply the common convention plugin for shared build configuration between library and application projects.
@@ -39,4 +40,6 @@ dependencies {
3940
implementation("io.github.nomisrev:kotlin-kafka:$kotlin_kafka_version")
4041
implementation("org.apache.kafka:kafka-clients:$kafka_version")
4142
testImplementation("io.ktor:ktor-server-test-host-jvm:$ktor_version")
43+
implementation("com.sksamuel.hoplite:hoplite-core:$hoplite_version")
44+
implementation("com.sksamuel.hoplite:hoplite-yaml:$hoplite_version")
4245
}

buildSrc/src/main/kotlin/buildlogic.kotlin-application-conventions.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
* This file was generated by the Gradle 'init' task.
44
*/
55

6-
val hoplite_version: String by project
7-
86
plugins {
97
// Apply the application-common convention plugin for shared build configuration between application projects.
108
id("buildlogic.kotlin-application-common-conventions")
@@ -21,6 +19,4 @@ application {
2119
}
2220

2321
dependencies {
24-
implementation("com.sksamuel.hoplite:hoplite-core:$hoplite_version")
25-
implementation("com.sksamuel.hoplite:hoplite-yaml:$hoplite_version")
2622
}

kafkakewl-common/src/main/kotlin/com/mwam/kafkakewl/common/config/Config.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
package com.mwam.kafkakewl.common.config
88

9+
import com.sksamuel.hoplite.ConfigLoaderBuilder
10+
import com.sksamuel.hoplite.addFileSource
11+
import com.sksamuel.hoplite.addResourceSource
12+
913
data class HttpConfig(
1014
val host: String = "0.0.0.0",
1115
val port: Int = 8080
@@ -29,5 +33,16 @@ data class KafkaClientConfig(
2933
)
3034

3135
data class KafkaClusterConfig(
36+
val name: String,
3237
val client: KafkaClientConfig
3338
)
39+
40+
inline fun <reified A : Any> loadConfig(overrideFileName: String, applicationConfigResource: String = "/application.yaml"): A {
41+
return ConfigLoaderBuilder.default()
42+
// TODO this fails currently because there are weird env vars on my linux box
43+
//.addEnvironmentSource(useUnderscoresAsSeparator = true)
44+
.addFileSource(overrideFileName)
45+
.addResourceSource(applicationConfigResource)
46+
.build()
47+
.loadConfigOrThrow<A>()
48+
}

kafkakewl-common/src/main/kotlin/com/mwam/kafkakewl/common/plugins/Configure.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import io.micrometer.prometheus.*
2828
import kotlinx.serialization.json.Json
2929
import org.slf4j.event.Level
3030

31+
/** initializes the logging, sets some logback variables */
32+
fun initializeLogging(kafkaClusterName: String) {
33+
System.setProperty("LOGBACK_KAFKA_CLUSTER", kafkaClusterName)
34+
}
35+
3136
fun Application.configureMonitoring() {
3237
val appMicrometerRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
3338

kafkakewl-deploy/src/main/kotlin/com/mwam/kafkakewl/deploy/Application.kt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@
66

77
package com.mwam.kafkakewl.deploy
88

9+
import com.mwam.kafkakewl.common.config.loadConfig
910
import com.mwam.kafkakewl.common.plugins.*
1011
import com.mwam.kafkakewl.deploy.plugins.*
11-
import com.mwam.kafkakewl.deploy.services.TopologyDeploymentsService
12-
import com.sksamuel.hoplite.*
1312
import io.ktor.server.application.*
1413
import io.ktor.server.engine.*
1514
import io.ktor.server.netty.*
16-
import org.koin.ktor.ext.inject
17-
import kotlin.getValue
1815

1916
fun main() {
20-
val config = ConfigLoaderBuilder.default()
21-
// TODO this fails currently because there are weird env vars on my linux box
22-
//.addEnvironmentSource(useUnderscoresAsSeparator = true)
23-
.addFileSource(".kafkakewl-deploy-overrides-application.yaml")
24-
.addResourceSource("/application.yaml")
25-
.build()
26-
.loadConfigOrThrow<Config>()
17+
val config = loadConfig<Config>(".kafkakewl-deploy-overrides-application.yaml", "/application.yaml")
18+
initializeLogging(config.kafkaCluster.name)
2719

2820
embeddedServer(Netty, port = config.http.port, host = config.http.host, module = { module(config) })
2921
.start(wait = true)

kafkakewl-deploy/src/main/resources/application.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ http:
33
port: 8080
44

55
kafkaCluster:
6+
name: test
67
client:
78
brokers: broker1
89
config: {}

kafkakewl-deploy/src/main/resources/logback.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<variable name="LOGBACK_STDOUT_APPENDER" value="${LOGBACK_STDOUT_APPENDER:-stdout-json}" />
44
<variable name="LOGBACK_LOCALFILE_ROOT" value="${LOGBACK_LOCALFILE_ROOT:-./logs}" />
55
<variable name="LOGBACK_LOCALFILE_NAME" value="${LOGBACK_LOCALFILE_NAME:-./com.mwam.kafkakewl.deploy}" />
6+
<variable name="LOGBACK_KAFKA_CLUSTER" value="${LOGBACK_KAFKA_CLUSTER:-na}" />
67

78
<!-- changing a kafka-log messages' levels, etc... to reduce warn/error logs when it's unnecessary -->
89
<turboFilter class="com.mwam.kafkakewl.utils.logging.LogFilter">
@@ -26,6 +27,10 @@
2627
<version>${MW_APP_TAG_VERSION:-0.0.0}</version>
2728
<includeMDC>true</includeMDC>
2829
<customFields>
30+
<field>
31+
<name>kafkaCluster</name>
32+
<value>${LOGBACK_KAFKA_CLUSTER}</value>
33+
</field>
2934
<field>
3035
<name>stacktrace</name>
3136
<value>%ex</value>

kafkakewl-metrics/src/main/kotlin/com/mwam/kafkakewl/metrics/Application.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package com.mwam.kafkakewl.metrics
88

9+
import com.mwam.kafkakewl.common.config.loadConfig
910
import com.mwam.kafkakewl.common.plugins.*
1011
import com.mwam.kafkakewl.metrics.plugins.*
1112
import com.mwam.kafkakewl.metrics.services.KafkaTopicInfoSource
@@ -17,13 +18,8 @@ import org.koin.ktor.ext.inject
1718
import kotlin.getValue
1819

1920
fun main() {
20-
val config = ConfigLoaderBuilder.default()
21-
// TODO this fails currently because there are weird env vars on my linux box
22-
//.addEnvironmentSource(useUnderscoresAsSeparator = true)
23-
.addFileSource(".kafkakewl-metrics-overrides-application.yaml")
24-
.addResourceSource("/application.yaml")
25-
.build()
26-
.loadConfigOrThrow<Config>()
21+
val config = loadConfig<Config>(".kafkakewl-metrics-overrides-application.yaml", "/application.yaml")
22+
initializeLogging(config.kafkaCluster.name)
2723

2824
embeddedServer(Netty, port = config.http.port, host = config.http.host, module = { module(config) })
2925
.start(wait = true)

kafkakewl-metrics/src/main/resources/application.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ http:
33
port: 8080
44

55
kafkaCluster:
6+
name: test
67
client:
78
brokers: broker1
89
config: {}

kafkakewl-metrics/src/main/resources/logback.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<variable name="LOGBACK_STDOUT_APPENDER" value="${LOGBACK_STDOUT_APPENDER:-stdout-json}" />
44
<variable name="LOGBACK_LOCALFILE_ROOT" value="${LOGBACK_LOCALFILE_ROOT:-./logs}" />
55
<variable name="LOGBACK_LOCALFILE_NAME" value="${LOGBACK_LOCALFILE_NAME:-./com.mwam.kafkakewl.metrics}" />
6+
<variable name="LOGBACK_KAFKA_CLUSTER" value="${LOGBACK_KAFKA_CLUSTER:-na}" />
67

78
<!-- changing a kafka-log messages' levels, etc... to reduce warn/error logs when it's unnecessary -->
89
<turboFilter class="com.mwam.kafkakewl.utils.logging.LogFilter">
@@ -26,6 +27,10 @@
2627
<version>${MW_APP_TAG_VERSION:-0.0.0}</version>
2728
<includeMDC>true</includeMDC>
2829
<customFields>
30+
<field>
31+
<name>kafkaCluster</name>
32+
<value>${LOGBACK_KAFKA_CLUSTER}</value>
33+
</field>
2934
<field>
3035
<name>stacktrace</name>
3136
<value>%ex</value>

0 commit comments

Comments
 (0)