Skip to content

Commit 9897e5d

Browse files
committed
Minimal changes to enable tracing context in MDC with coroutines
Uses latest Spring Framework 6.1 snapshots that propagate CoroutineContext in CoWebFilter[1]. [1] spring-projects/spring-framework#27522
1 parent 198eb9b commit 9897e5d

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

app/build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
import org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
32

43
plugins {
5-
id("org.springframework.boot") version "3.1.4"
4+
id("org.springframework.boot") version "3.2.0-SNAPSHOT"
5+
id("io.spring.dependency-management") version "1.1.3"
66
kotlin("jvm") version "1.9.10"
77
kotlin("plugin.spring") version "1.9.10"
88
}
@@ -16,12 +16,12 @@ java {
1616

1717
repositories {
1818
mavenCentral()
19+
maven { url = uri("https://repo.spring.io/milestone") }
20+
maven { url = uri("https://repo.spring.io/snapshot") }
1921
}
2022

2123
dependencies {
22-
implementation(platform(BOM_COORDINATES))
2324
implementation(platform("org.zalando:logbook-bom:3.4.0"))
24-
implementation(platform("io.micrometer:micrometer-tracing-bom:1.1.5"))
2525
implementation("org.springframework.boot:spring-boot-starter-webflux")
2626
implementation("org.springframework.boot:spring-boot-starter-security")
2727
runtimeOnly("org.springframework.boot:spring-boot-starter-actuator")
@@ -35,7 +35,7 @@ dependencies {
3535
implementation("org.zalando:logbook-spring-boot-starter")
3636
implementation("org.zalando:logbook-spring-boot-webflux-autoconfigure")
3737
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:1.7.3")
38-
compileOnly("org.springframework.boot:spring-boot-devtools")
38+
developmentOnly("org.springframework.boot:spring-boot-devtools")
3939
testImplementation("org.springframework.boot:spring-boot-starter-test")
4040
}
4141

app/src/main/kotlin/com/grassehh/app/configuration/TracingConfiguration.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,32 @@ import io.netty.channel.ChannelDuplexHandler
1010
import io.netty.channel.ChannelHandlerContext
1111
import io.netty.channel.ChannelPromise
1212
import jakarta.annotation.PostConstruct
13+
import kotlinx.coroutines.slf4j.MDCContext
14+
import kotlinx.coroutines.withContext
1315
import org.springframework.context.annotation.Bean
1416
import org.springframework.context.annotation.Configuration
17+
import org.springframework.web.server.CoWebFilter
18+
import org.springframework.web.server.CoWebFilterChain
19+
import org.springframework.web.server.ServerWebExchange
20+
import org.springframework.web.server.WebFilter
1521
import reactor.core.publisher.Hooks
1622
import reactor.netty.Metrics
1723

1824
@Configuration
1925
class TracingConfiguration(private val observationRegistry: ObservationRegistry, private val tracer: Tracer) {
26+
27+
@Bean
28+
fun coroutineWebFilter(): WebFilter {
29+
return object : CoWebFilter() {
30+
override suspend fun filter(exchange: ServerWebExchange, chain: CoWebFilterChain)
31+
= withContext(MDCContext()) {
32+
chain.filter(exchange)
33+
}
34+
}
35+
}
36+
2037
@Bean
21-
fun contextSnapshotFactory() = ContextSnapshotFactory.builder().build()
38+
fun contextSnapshotFactory() = ContextSnapshotFactory.builder().build();
2239

2340
@PostConstruct
2441
fun postConstruct() {

settings.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1+
pluginManagement {
2+
repositories {
3+
maven { url = uri("https://repo.spring.io/milestone") }
4+
maven { url = uri("https://repo.spring.io/snapshot") }
5+
gradlePluginPortal()
6+
}
7+
}
8+
19
rootProject.name = "spring-boot-3-tracing-coroutine"
210
include("app")

0 commit comments

Comments
 (0)