Skip to content

Commit

Permalink
Minimal changes to enable tracing context in MDC with coroutines
Browse files Browse the repository at this point in the history
Uses latest Spring Framework 6.1 snapshots that propagate CoroutineContext in CoWebFilter[1].

[1] spring-projects/spring-framework#27522
  • Loading branch information
shakuzen committed Oct 12, 2023
1 parent 198eb9b commit 9897e5d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES

plugins {
id("org.springframework.boot") version "3.1.4"
id("org.springframework.boot") version "3.2.0-SNAPSHOT"
id("io.spring.dependency-management") version "1.1.3"
kotlin("jvm") version "1.9.10"
kotlin("plugin.spring") version "1.9.10"
}
Expand All @@ -16,12 +16,12 @@ java {

repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
}

dependencies {
implementation(platform(BOM_COORDINATES))
implementation(platform("org.zalando:logbook-bom:3.4.0"))
implementation(platform("io.micrometer:micrometer-tracing-bom:1.1.5"))
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.boot:spring-boot-starter-security")
runtimeOnly("org.springframework.boot:spring-boot-starter-actuator")
Expand All @@ -35,7 +35,7 @@ dependencies {
implementation("org.zalando:logbook-spring-boot-starter")
implementation("org.zalando:logbook-spring-boot-webflux-autoconfigure")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:1.7.3")
compileOnly("org.springframework.boot:spring-boot-devtools")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@ import io.netty.channel.ChannelDuplexHandler
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
import jakarta.annotation.PostConstruct
import kotlinx.coroutines.slf4j.MDCContext
import kotlinx.coroutines.withContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.server.CoWebFilter
import org.springframework.web.server.CoWebFilterChain
import org.springframework.web.server.ServerWebExchange
import org.springframework.web.server.WebFilter
import reactor.core.publisher.Hooks
import reactor.netty.Metrics

@Configuration
class TracingConfiguration(private val observationRegistry: ObservationRegistry, private val tracer: Tracer) {

@Bean
fun coroutineWebFilter(): WebFilter {
return object : CoWebFilter() {
override suspend fun filter(exchange: ServerWebExchange, chain: CoWebFilterChain)
= withContext(MDCContext()) {
chain.filter(exchange)
}
}
}

@Bean
fun contextSnapshotFactory() = ContextSnapshotFactory.builder().build()
fun contextSnapshotFactory() = ContextSnapshotFactory.builder().build();

@PostConstruct
fun postConstruct() {
Expand Down
8 changes: 8 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
pluginManagement {
repositories {
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
gradlePluginPortal()
}
}

rootProject.name = "spring-boot-3-tracing-coroutine"
include("app")

0 comments on commit 9897e5d

Please sign in to comment.